Fixed index over slices with struct pointers

This commit is contained in:
Leon Mika 2024-05-24 03:57:36 +00:00
parent a69fe72f23
commit da00d9c723
2 changed files with 27 additions and 8 deletions

View File

@ -4,9 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/lmika/gopkgs/fp/slices"
"reflect"
"strconv"
"github.com/lmika/gopkgs/fp/slices"
)
type object interface {
@ -156,15 +157,20 @@ func fromGoValue(v any) (object, error) {
return intObject(t), nil
}
resVal := reflect.ValueOf(v)
return fromGoReflectValue(reflect.ValueOf(v))
}
func fromGoReflectValue(resVal reflect.Value) (object, error) {
switch resVal.Kind() {
case reflect.Slice:
return listableProxyObject{resVal}, nil
case reflect.Struct:
return newStructProxyObject(resVal), nil
case reflect.Pointer:
return fromGoReflectValue(resVal.Elem())
}
return proxyObject{v}, nil
return proxyObject{resVal.Interface()}, nil
}
type macroArgs struct {

View File

@ -523,11 +523,15 @@ func TestBuiltins_Index(t *testing.T) {
{desc: "list of hash 1", expr: `index [["id":"abc"] ["id":"123"]] 0 id`, want: "abc\n"},
{desc: "list of hash 2", expr: `index [["id":"abc"] ["id":"123"]] 1 id`, want: "123\n"},
{desc: "go list 1", expr: `goInt | index 1`, want: "5\n"},
{desc: "go list 2", expr: `goInt | index 2`, want: "4\n"},
{desc: "go list 3", expr: `goInt | index 555`, want: "(nil)\n"},
{desc: "go list 4", expr: `goInt | index -12`, want: "(nil)\n"},
{desc: "go list 5", expr: `goInt | index NotAnIndex`, want: "(nil)\n"},
{desc: "go int 1", expr: `goInt | index 1`, want: "5\n"},
{desc: "go int 2", expr: `goInt | index 2`, want: "4\n"},
{desc: "go int 3", expr: `goInt | index 555`, want: "(nil)\n"},
{desc: "go int 4", expr: `goInt | index -12`, want: "(nil)\n"},
{desc: "go int 5", expr: `goInt | index NotAnIndex`, want: "(nil)\n"},
{desc: "go list 1", expr: `goList | index 0 This`, want: "thing 1\n"},
{desc: "go list 2", expr: `goList | index 1 This`, want: "thing 2\n"},
{desc: "go struct 1", expr: `goStruct | index Alpha`, want: "foo\n"},
{desc: "go struct 2", expr: `goStruct | index Beta`, want: "bar\n"},
{desc: "go struct 3", expr: `goStruct | index Gamma 1`, want: "33\n"},
@ -551,6 +555,15 @@ func TestBuiltins_Index(t *testing.T) {
inst.SetBuiltin("goInt", func(ctx context.Context, args CallArgs) (any, error) {
return []int{6, 5, 4}, nil
})
inst.SetBuiltin("goList", func(ctx context.Context, args CallArgs) (any, error) {
type nest struct {
This string
}
return []*nest{
{This: "thing 1"},
{This: "thing 2"},
}, nil
})
inst.SetBuiltin("goStruct", func(ctx context.Context, args CallArgs) (any, error) {
type nested struct {
This string