use golang default if an arg is missing (Refs #7)

This commit is contained in:
Anton Kaliaev 2017-03-08 17:16:01 +04:00
parent 6d66cc68ed
commit 2dc6ab3896
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 49 additions and 24 deletions

View File

@ -141,20 +141,20 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc) http.HandlerFunc {
// Convert a list of interfaces to properly typed values
func jsonParamsToArgs(rpcFunc *RPCFunc, params map[string]interface{}) ([]reflect.Value, error) {
if len(rpcFunc.argNames) != len(params) {
return nil, fmt.Errorf("Expected %v parameters (%v), got %v (%v)",
len(rpcFunc.argNames), rpcFunc.argNames, len(params), params)
}
values := make([]reflect.Value, len(rpcFunc.args))
values := make([]reflect.Value, len(params))
// fill each value with default
for i, argType := range rpcFunc.args {
values[i] = reflect.Zero(argType)
}
for name, param := range params {
i := indexOf(name, rpcFunc.argNames)
if -1 == i {
return nil, fmt.Errorf("%s is not an argument (args: %v)", name, rpcFunc.argNames)
}
ty := rpcFunc.args[i]
v, err := _jsonObjectToArg(ty, param)
argType := rpcFunc.args[i]
v, err := _jsonObjectToArg(argType, param)
if err != nil {
return nil, err
}
@ -176,21 +176,21 @@ func indexOf(value string, values []string) int {
// Same as above, but with the first param the websocket connection
func jsonParamsToArgsWS(rpcFunc *RPCFunc, params map[string]interface{}, wsCtx types.WSRPCContext) ([]reflect.Value, error) {
if len(rpcFunc.argNames) != len(params) {
return nil, fmt.Errorf("Expected %v parameters (%v), got %v (%v)",
len(rpcFunc.argNames)-1, rpcFunc.argNames[1:], len(params), params)
}
values := make([]reflect.Value, len(params)+1)
values := make([]reflect.Value, len(rpcFunc.args))
values[0] = reflect.ValueOf(wsCtx)
// fill each value with default
for i, argType := range rpcFunc.args {
values[i+1] = reflect.Zero(argType)
}
for name, param := range params {
i := indexOf(name, rpcFunc.argNames)
if -1 == i {
return nil, fmt.Errorf("%s is not an argument (args: %v)", name, rpcFunc.argNames)
}
ty := rpcFunc.args[i+1]
v, err := _jsonObjectToArg(ty, param)
argType := rpcFunc.args[i+1]
v, err := _jsonObjectToArg(argType, param)
if err != nil {
return nil, err
}
@ -245,16 +245,23 @@ func makeHTTPHandler(rpcFunc *RPCFunc) func(http.ResponseWriter, *http.Request)
// Covert an http query to a list of properly typed values.
// To be properly decoded the arg must be a concrete type from tendermint (if its an interface).
func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error) {
argTypes := rpcFunc.args
argNames := rpcFunc.argNames
values := make([]reflect.Value, len(rpcFunc.args))
values := make([]reflect.Value, len(argNames))
for i, name := range argNames {
ty := argTypes[i]
// fill each value with default
for i, argType := range rpcFunc.args {
values[i] = reflect.Zero(argType)
}
for i, name := range rpcFunc.argNames {
argType := rpcFunc.args[i]
arg := GetParam(r, name)
// log.Notice("param to arg", "ty", ty, "name", name, "arg", arg)
// log.Notice("param to arg", "argType", argType, "name", name, "arg", arg)
v, err, ok := nonJsonToArg(ty, arg)
if "" == arg {
continue
}
v, err, ok := nonJsonToArg(argType, arg)
if err != nil {
return nil, err
}
@ -264,11 +271,12 @@ func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error
}
// Pass values to go-wire
values[i], err = _jsonStringToArg(ty, arg)
values[i], err = _jsonStringToArg(argType, arg)
if err != nil {
return nil, err
}
}
return values, nil
}

View File

@ -28,6 +28,7 @@ if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
echo "FAIL"
exit 1
else
echo "OK"
@ -40,18 +41,33 @@ if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
echo "FAIL"
exit 1
else
echo "OK"
fi
# request with unquoted string arg
echo "==> request with missing params"
R1=$(curl -s 'http://localhost:8008/hello_world')
R2='{"jsonrpc":"2.0","id":"","result":{"Result":"hi 0"},"error":""}'
if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
echo "FAIL"
exit 1
else
echo "OK"
fi
echo "==> request with unquoted string arg"
R1=$(curl -s 'http://localhost:8008/hello_world?name=abcd&num=123')
R2="{\"jsonrpc\":\"2.0\",\"id\":\"\",\"result\":null,\"error\":\"Error converting http params to args: invalid character 'a' looking for beginning of value\"}"
if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
echo "FAIL"
exit 1
else
echo "OK"
@ -64,6 +80,7 @@ if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
echo "FAIL"
exit 1
else
echo "OK"