MCPcopy Create free account
hub / github.com/CPChain/chain / parsePositionalArguments

Function parsePositionalArguments

api/rpc/json.go:286–319  ·  view source on GitHub ↗

parsePositionalArguments tries to parse the given args to an array of values with the given types. It returns the parsed values or an error when the args could not be parsed. Missing optional arguments are returned as reflect.Zero values.

(rawArgs json.RawMessage, types []reflect.Type)

Source from the content-addressed store, hash-verified

284// given types. It returns the parsed values or an error when the args could not be
285// parsed. Missing optional arguments are returned as reflect.Zero values.
286func parsePositionalArguments(rawArgs json.RawMessage, types []reflect.Type) ([]reflect.Value, Error) {
287 // Read beginning of the args array.
288 dec := json.NewDecoder(bytes.NewReader(rawArgs))
289 if tok, _ := dec.Token(); tok != json.Delim('[') {
290 return nil, &invalidParamsError{"non-array args"}
291 }
292 // Read args.
293 args := make([]reflect.Value, 0, len(types))
294 for i := 0; dec.More(); i++ {
295 if i >= len(types) {
296 return nil, &invalidParamsError{fmt.Sprintf("too many arguments, want at most %d", len(types))}
297 }
298 argval := reflect.New(types[i])
299 if err := dec.Decode(argval.Interface()); err != nil {
300 return nil, &invalidParamsError{fmt.Sprintf("invalid argument %d: %v", i, err)}
301 }
302 if argval.IsNil() && types[i].Kind() != reflect.Ptr {
303 return nil, &invalidParamsError{fmt.Sprintf("missing value for required argument %d", i)}
304 }
305 args = append(args, argval.Elem())
306 }
307 // Read end of args array.
308 if _, err := dec.Token(); err != nil {
309 return nil, &invalidParamsError{err.Error()}
310 }
311 // Set any missing args to nil.
312 for i := len(args); i < len(types); i++ {
313 if types[i].Kind() != reflect.Ptr {
314 return nil, &invalidParamsError{fmt.Sprintf("missing value for required argument %d", i)}
315 }
316 args = append(args, reflect.Zero(types[i]))
317 }
318 return args, nil
319}
320
321// CreateResponse will create a JSON-RPC success response with the given id and reply as result.
322func (c *jsonCodec) CreateResponse(id interface{}, reply interface{}) interface{} {

Callers 1

ParseRequestArgumentsMethod · 0.85

Calls 1

ErrorMethod · 0.65

Tested by

no test coverage detected