nsmcp::Tool method jsonArguments (protected)

 <instance of nsmcp::Tool[i]> jsonArguments arguments

Defined in /usr/local/ns/tcl/nsmcp/01-tools.tcl

Convert a JSON argument object in ns_json's rooted typed-triples representation to NX named arguments, using inputSchema types. The input is a three-element Tcl list: name, type, members, where type is object and members is a sequence of name/type/value triples. It is not serialized JSON text or an ordinary Tcl dictionary. Parse JSON text first with ns_json parse -output triples. Check JSON types before Tcl/NX can coerce strings to numbers or booleans. This is not a general JSON Schema validator: NX handles defaults/required parameters; method bodies handle ranges, enums and cross-parameter constraints. Structured values retain their rooted typed triples so nested JSON types are not discarded.

Parameters:
arguments (required)
Rooted typed triples for a JSON object, as returned by ns_json parse -output triples; serialized JSON is not accepted.
Returns:
Tcl list of NX named arguments (-name value pairs).

Testcases:
No testcase defined.
Source code:
if {[llength $arguments] != 3 || [lindex $arguments 1] ne "object"
    || [llength [lindex $arguments 2]] % 3} {error "Expected a JSON arguments object"}
set properties {}
foreach {name type members} [ns_json triples getvalue -output triples -path properties -- ${:inputSchema}] {
    if {$type ne "object"} {error "Invalid property schema: $name"}
    dict set properties $name [list {} object $members]
}
set result {}; set seen {}
foreach {key type value} [lindex $arguments 2] {
    if {![dict exists $properties $key]} {error "Unknown argument: $key"}
    if {[dict exists $seen $key]} {error "Duplicate argument: $key"}
    dict set seen $key 1
    set expected [ns_json triples getvalue -output triples -path type -- [dict get $properties $key]]
    switch -- $expected {
        integer {
            if {$type ne "number" || ![regexp {^-?(?:0|[1-9][0-9]*)$} $value]} {
                error "Argument $key must be a JSON integer"
            }
        }
        string - number - boolean - object - array - null {
            if {$type ne $expected} {error "Argument $key must be a JSON $expected"}
        }
        default {error "Unsupported JSON argument type for $key: $expected"}
    }
    if {$type in {object array null}} {set value [list {} $type $value]}
    lappend result -$key $value
}
return $result
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: