From 88d7dd6f71cee09784a284c6a7287fe5b03c5f7f Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Thu, 30 May 2019 11:12:51 -0400 Subject: [PATCH] WIP: Messing around with a clean reflectPositionArgs impl --- jsonrpc2/params.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 jsonrpc2/params.go diff --git a/jsonrpc2/params.go b/jsonrpc2/params.go new file mode 100644 index 0000000..39d8e1a --- /dev/null +++ b/jsonrpc2/params.go @@ -0,0 +1,36 @@ +package jsonrpc2 + +import ( + "encoding/json" + "errors" + "reflect" +) + +// reflectPositionalArgs takes the params of a JSONRPC message, and asserts +// each positional argument into the reflected value of its type. It only +// supports positional arguments for params, and will give an error for other +// kinds of params. +func reflectPositionalArgs(msgParams json.RawMessage, types []reflect.Type) ([]reflect.Value, error) { + // TODO: Add error type + if len(msgParams) == 0 { + return nil, errors.New("no params given") + } + + args := make([]interface{}, 0, len(types)) + if err := json.Unmarshal(msgParams, &args); err != nil { + return nil, err + } + if len(args) > types { + return nil, errors.New("too many arguments") + } + + values := make([]reflect.Value, 0, len(types)) + for i, arg := range args { + if arg == nil { + return nil, errors.New("not enough arguments") + } + value := reflect.New(types[i]) + } + + return values, nil +}