interact: fix object arg injection

This commit is contained in:
c9s 2022-01-13 01:48:14 +08:00
parent caa50c3b04
commit 087a91aa8a
2 changed files with 21 additions and 8 deletions

View File

@ -169,6 +169,7 @@ func parseFuncArgsAndCall(f interface{}, args []string, objects ...interface{})
ft := reflect.TypeOf(f)
objectIndex := 0
argIndex := 0
var rArgs []reflect.Value
for i := 0; i < ft.NumIn(); i++ {
@ -178,7 +179,12 @@ func parseFuncArgsAndCall(f interface{}, args []string, objects ...interface{})
case reflect.Interface:
found := false
for oi := objectIndex; oi < len(objects)-1; oi++ {
if objectIndex >= len(objects) {
return fmt.Errorf("found interface type %s, but object args are empty", at)
}
for oi := objectIndex; oi < len(objects); oi++ {
obj := objects[oi]
objT := reflect.TypeOf(obj)
objV := reflect.ValueOf(obj)
@ -186,12 +192,13 @@ func parseFuncArgsAndCall(f interface{}, args []string, objects ...interface{})
fmt.Println(
at.PkgPath(),
at.Name(),
"implements", at, " = ", objT.Implements(at),
objT, "implements", at, "=", objT.Implements(at),
)
if objT.Implements(at) {
found = true
rArgs = append(rArgs, objV)
objectIndex = oi + 1
break
}
}
@ -200,34 +207,38 @@ func parseFuncArgsAndCall(f interface{}, args []string, objects ...interface{})
}
case reflect.String:
av := reflect.ValueOf(args[i])
av := reflect.ValueOf(args[argIndex])
rArgs = append(rArgs, av)
argIndex++
case reflect.Bool:
bv, err := strconv.ParseBool(args[i])
bv, err := strconv.ParseBool(args[argIndex])
if err != nil {
return err
}
av := reflect.ValueOf(bv)
rArgs = append(rArgs, av)
argIndex++
case reflect.Int64:
nf, err := strconv.ParseInt(args[i], 10, 64)
nf, err := strconv.ParseInt(args[argIndex], 10, 64)
if err != nil {
return err
}
av := reflect.ValueOf(nf)
rArgs = append(rArgs, av)
argIndex++
case reflect.Float64:
nf, err := strconv.ParseFloat(args[i], 64)
nf, err := strconv.ParseFloat(args[argIndex], 64)
if err != nil {
return err
}
av := reflect.ValueOf(nf)
rArgs = append(rArgs, av)
argIndex++
}
}

View File

@ -36,8 +36,10 @@ func Test_parseFuncArgsAndCall_InterfaceInjection(t *testing.T) {
return err
}
err := parseFuncArgsAndCall(f, []string{"BTCUSDT", "0.123"}, bytes.NewBuffer(nil))
assert.Error(t, err)
buf := bytes.NewBuffer(nil)
err := parseFuncArgsAndCall(f, []string{"BTCUSDT", "0.123"}, buf)
assert.NoError(t, err)
assert.Equal(t, "123", buf.String())
}
func Test_parseCommand(t *testing.T) {