From fee1b585b5c83fb1e8be809bbf593f5ab32ef00a Mon Sep 17 00:00:00 2001 From: despiegk Date: Sun, 14 Sep 2025 12:31:45 +0200 Subject: [PATCH] ... --- lib/hero/heromodels/openrpc/openrpc.json | 26 +++------------- lib/hero/heromodels/openrpc/rpc_calendar.v | 6 ++-- lib/hero/heromodels/openrpc/rpc_comment.v | 6 ++-- lib/schemas/jsonrpc/model_response.v | 36 +++++++++++++++++++++- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/lib/hero/heromodels/openrpc/openrpc.json b/lib/hero/heromodels/openrpc/openrpc.json index eb78e198..3e9b3a87 100644 --- a/lib/hero/heromodels/openrpc/openrpc.json +++ b/lib/hero/heromodels/openrpc/openrpc.json @@ -81,18 +81,9 @@ ], "result": { "name": "result", - "description": "Success result with deleted comment ID", + "description": "Success result", "schema": { - "type": "object", - "properties": { - "success": { - "type": "boolean" - }, - "id": { - "type": "integer", - "minimum": 0 - } - } + "type": "boolean" } } }, @@ -210,18 +201,9 @@ ], "result": { "name": "result", - "description": "Success result with deleted calendar ID", + "description": "Success result", "schema": { - "type": "object", - "properties": { - "success": { - "type": "boolean" - }, - "id": { - "type": "integer", - "minimum": 0 - } - } + "type": "boolean" } } }, diff --git a/lib/hero/heromodels/openrpc/rpc_calendar.v b/lib/hero/heromodels/openrpc/rpc_calendar.v index e4e25604..e1e64d22 100644 --- a/lib/hero/heromodels/openrpc/rpc_calendar.v +++ b/lib/hero/heromodels/openrpc/rpc_calendar.v @@ -56,9 +56,7 @@ pub fn calendar_set(request jsonrpc.Request) !jsonrpc.Response { id := mydb.calendar.set(calendar_obj)! - return jsonrpc.new_response(request.id, json.encode({ - 'id': id - })) + return jsonrpc.new_response_u32(request.id, id) } pub fn calendar_delete(request jsonrpc.Request) !jsonrpc.Response { @@ -70,7 +68,7 @@ pub fn calendar_delete(request jsonrpc.Request) !jsonrpc.Response { mydb.calendar.delete(payload.id)! // returns - return jsonrpc.new_response(request.id, 'true') + return jsonrpc.new_response_true(request.id) // return true as jsonrpc (bool) } pub fn calendar_list(request jsonrpc.Request) !jsonrpc.Response { diff --git a/lib/hero/heromodels/openrpc/rpc_comment.v b/lib/hero/heromodels/openrpc/rpc_comment.v index d5acac33..662a54e4 100644 --- a/lib/hero/heromodels/openrpc/rpc_comment.v +++ b/lib/hero/heromodels/openrpc/rpc_comment.v @@ -50,9 +50,7 @@ pub fn comment_set(request jsonrpc.Request) !jsonrpc.Response { id := mydb.comments.set(comment_obj)! - return jsonrpc.new_response(request.id, json.encode({ - 'id': id - })) + return jsonrpc.new_response_u32(request.id, id) } pub fn comment_delete(request jsonrpc.Request) !jsonrpc.Response { @@ -63,7 +61,7 @@ pub fn comment_delete(request jsonrpc.Request) !jsonrpc.Response { mut mydb := heromodels.new()! mydb.comments.delete(payload.id)! - return jsonrpc.new_response(request.id, 'true') + return jsonrpc.new_response_true(request.id) // return true as jsonrpc (bool) } pub fn comment_list(request jsonrpc.Request) !jsonrpc.Response { diff --git a/lib/schemas/jsonrpc/model_response.v b/lib/schemas/jsonrpc/model_response.v index f5119753..42ac7f39 100644 --- a/lib/schemas/jsonrpc/model_response.v +++ b/lib/schemas/jsonrpc/model_response.v @@ -40,6 +40,38 @@ pub fn new_response(id int, result string) Response { } } +pub fn new_response_true(id int) Response { + return Response{ + jsonrpc: jsonrpc_version + result: 'true' + id: id + } +} + +pub fn new_response_false(id int) Response { + return Response{ + jsonrpc: jsonrpc_version + result: 'false' + id: id + } +} + +pub fn new_response_int(id int, result int) Response { + return Response{ + jsonrpc: jsonrpc_version + result: result.str() + id: id + } +} + +pub fn new_response_u32(id int, result u32) Response { + return Response{ + jsonrpc: jsonrpc_version + result: result.str() + id: id + } +} + // new_error_response creates an error JSON-RPC response with the given error object. // // Parameters: @@ -65,7 +97,9 @@ pub fn new_error_response(id int, error RPCError) Response { // Returns: // - A Response object or an error if parsing fails or the response is invalid pub fn decode_response(data string) !Response { - raw := json2.raw_decode(data) or { return error('Failed to decode JSONRPC response ${data}\n${err}') } + raw := json2.raw_decode(data) or { + return error('Failed to decode JSONRPC response ${data}\n${err}') + } raw_map := raw.as_map() // Validate that the response contains either result or error, but not both or neither