- Remove unused `generate_example_call` and `generate_example_response` functions - Rename `example_call` to `example_request` in `DocMethod` - Update schema example extraction to use `schema.example` directly - Introduce `generate_request_example` and `generate_response_example` for dynamic example generation - Change type of `id` from string to number in schema examples PS: The work is still in progress
44 lines
1.1 KiB
V
44 lines
1.1 KiB
V
module openrpc
|
|
|
|
import json
|
|
import x.json2
|
|
import freeflowuniverse.herolib.schemas.jsonschema
|
|
|
|
// In Method struct
|
|
pub fn (method Method) example() (string, string) {
|
|
// Extract user-provided examples from the OpenRPC specification
|
|
|
|
mut example_params := map[string]json2.Any{}
|
|
for param in method.params {
|
|
if param is ContentDescriptor {
|
|
param_desc := param as ContentDescriptor
|
|
if param_desc.schema is jsonschema.Schema {
|
|
schema := param_desc.schema as jsonschema.Schema
|
|
if schema.example.str() != '' {
|
|
example_params[param_desc.name] = schema.example
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
example_call := json.encode(example_params)
|
|
|
|
example_response := if method.result is ContentDescriptor {
|
|
result_desc := method.result as ContentDescriptor
|
|
if result_desc.schema is jsonschema.Schema {
|
|
schema := result_desc.schema as jsonschema.Schema
|
|
if schema.example.str() != '' {
|
|
json.encode(schema.example)
|
|
} else {
|
|
'{"result": "success"}'
|
|
}
|
|
} else {
|
|
'{"result": "success"}'
|
|
}
|
|
} else {
|
|
'{"result": "success"}'
|
|
}
|
|
|
|
return example_call, example_response
|
|
}
|