Skip to content

Commit 2bc0eed

Browse files
committed
Remove duplicated dereferences
1 parent f4bbea5 commit 2bc0eed

File tree

6 files changed

+25
-31
lines changed

6 files changed

+25
-31
lines changed

Diff for: builtin/builtin.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,11 @@ var Builtins = []*Function{
644644
if len(args) != 2 {
645645
return nil, fmt.Errorf("invalid number of arguments (expected 2, got %d)", len(args))
646646
}
647-
v := deref.ValueOf(args[0])
647+
v := reflect.ValueOf(args[0])
648648
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
649649
return nil, fmt.Errorf("cannot take from %s", v.Kind())
650650
}
651-
n := deref.ValueOf(args[1])
651+
n := reflect.ValueOf(args[1])
652652
if !n.CanInt() {
653653
return nil, fmt.Errorf("cannot take %s elements", n.Kind())
654654
}
@@ -683,7 +683,7 @@ var Builtins = []*Function{
683683
if len(args) != 1 {
684684
return nil, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
685685
}
686-
v := deref.ValueOf(args[0])
686+
v := reflect.ValueOf(args[0])
687687
if v.Kind() != reflect.Map {
688688
return nil, fmt.Errorf("cannot get keys from %s", v.Kind())
689689
}
@@ -713,7 +713,7 @@ var Builtins = []*Function{
713713
if len(args) != 1 {
714714
return nil, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
715715
}
716-
v := deref.ValueOf(args[0])
716+
v := reflect.ValueOf(args[0])
717717
if v.Kind() != reflect.Map {
718718
return nil, fmt.Errorf("cannot get values from %s", v.Kind())
719719
}
@@ -743,7 +743,7 @@ var Builtins = []*Function{
743743
if len(args) != 1 {
744744
return nil, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
745745
}
746-
v := deref.ValueOf(args[0])
746+
v := reflect.ValueOf(args[0])
747747
if v.Kind() != reflect.Map {
748748
return nil, fmt.Errorf("cannot transform %s to pairs", v.Kind())
749749
}
@@ -771,7 +771,7 @@ var Builtins = []*Function{
771771
if len(args) != 1 {
772772
return nil, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
773773
}
774-
v := deref.ValueOf(args[0])
774+
v := reflect.ValueOf(args[0])
775775
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
776776
return nil, fmt.Errorf("cannot transform %s from pairs", v)
777777
}
@@ -808,7 +808,7 @@ var Builtins = []*Function{
808808
return nil, 0, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
809809
}
810810

811-
v := deref.ValueOf(args[0])
811+
v := reflect.ValueOf(args[0])
812812
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
813813
return nil, 0, fmt.Errorf("cannot reverse %s", v.Kind())
814814
}
@@ -843,7 +843,7 @@ var Builtins = []*Function{
843843
return nil, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
844844
}
845845

846-
v := deref.ValueOf(deref.Deref(args[0]))
846+
v := reflect.ValueOf(args[0])
847847
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
848848
return nil, fmt.Errorf("cannot uniq %s", v.Kind())
849849
}
@@ -897,7 +897,7 @@ var Builtins = []*Function{
897897
var arr []any
898898

899899
for _, arg := range args {
900-
v := deref.ValueOf(deref.Deref(arg))
900+
v := reflect.ValueOf(arg)
901901

902902
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
903903
return nil, 0, fmt.Errorf("cannot concat %s", v.Kind())
@@ -936,7 +936,7 @@ var Builtins = []*Function{
936936
if len(args) != 1 {
937937
return nil, 0, fmt.Errorf("invalid number of arguments (expected 1, got %d)", len(args))
938938
}
939-
v := deref.ValueOf(deref.Deref(args[0]))
939+
v := reflect.ValueOf(args[0])
940940
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
941941
return nil, size, fmt.Errorf("cannot flatten %s", v.Kind())
942942
}

Diff for: builtin/builtin_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ func TestBuiltin_with_deref(t *testing.T) {
693693
{`uniq(arr)`, []any{1, 2, 3}},
694694
{`concat(arr, arr)`, []any{1, 2, 3, 1, 2, 3}},
695695
{`flatten([arr, [arr]])`, []any{1, 2, 3, 1, 2, 3}},
696+
{`flatten(arr)`, []any{1, 2, 3}},
696697
{`toJSON(arr)`, "[\n 1,\n 2,\n 3\n]"},
697698
{`fromJSON(json)`, []any{"1"}},
698699
{`split(str, ",")`, []string{"1", "2", "3"}},

Diff for: builtin/lib.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func Len(x any) any {
15-
v := deref.ValueOf(x)
15+
v := reflect.ValueOf(x)
1616
switch v.Kind() {
1717
case reflect.Array, reflect.Slice, reflect.Map:
1818
return v.Len()
@@ -27,7 +27,7 @@ func Type(arg any) any {
2727
if arg == nil {
2828
return "nil"
2929
}
30-
v := deref.ValueOf(arg)
30+
v := reflect.ValueOf(arg)
3131
if v.Type().Name() != "" && v.Type().PkgPath() != "" {
3232
return fmt.Sprintf("%s.%s", v.Type().PkgPath(), v.Type().Name())
3333
}
@@ -58,7 +58,7 @@ func Type(arg any) any {
5858
}
5959

6060
func Abs(x any) any {
61-
switch x := deref.Deref(x).(type) {
61+
switch x := x.(type) {
6262
case float32:
6363
if x < 0 {
6464
return -x
@@ -172,7 +172,7 @@ func Round(x any) any {
172172
}
173173

174174
func Int(x any) any {
175-
switch x := deref.Deref(x).(type) {
175+
switch x := x.(type) {
176176
case float32:
177177
return int(x)
178178
case float64:
@@ -213,7 +213,7 @@ func Int(x any) any {
213213
}
214214

215215
func Float(x any) any {
216-
switch x := deref.Deref(x).(type) {
216+
switch x := x.(type) {
217217
case float32:
218218
return float64(x)
219219
case float64:
@@ -256,7 +256,7 @@ func String(arg any) any {
256256
func minMax(name string, fn func(any, any) bool, args ...any) (any, error) {
257257
var val any
258258
for _, arg := range args {
259-
rv := deref.ValueOf(arg)
259+
rv := reflect.ValueOf(arg)
260260
switch rv.Kind() {
261261
case reflect.Array, reflect.Slice:
262262
size := rv.Len()
@@ -299,7 +299,7 @@ func mean(args ...any) (int, float64, error) {
299299
var count int
300300

301301
for _, arg := range args {
302-
rv := deref.ValueOf(arg)
302+
rv := reflect.ValueOf(arg)
303303
switch rv.Kind() {
304304
case reflect.Array, reflect.Slice:
305305
size := rv.Len()
@@ -331,7 +331,7 @@ func median(args ...any) ([]float64, error) {
331331
var values []float64
332332

333333
for _, arg := range args {
334-
rv := deref.ValueOf(arg)
334+
rv := reflect.ValueOf(arg)
335335
switch rv.Kind() {
336336
case reflect.Array, reflect.Slice:
337337
size := rv.Len()
@@ -388,9 +388,6 @@ func get(params ...any) (out any, err error) {
388388
}
389389
}
390390

391-
v = deref.Value(v)
392-
i = deref.Deref(i)
393-
394391
switch v.Kind() {
395392
case reflect.Array, reflect.Slice, reflect.String:
396393
index := runtime.ToInt(i)

Diff for: internal/deref/deref.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66
)
77

8-
func Deref(p any) any {
8+
func Interface(p any) any {
99
if p == nil {
1010
return nil
1111
}
@@ -45,7 +45,3 @@ func Value(v reflect.Value) reflect.Value {
4545
}
4646
return v
4747
}
48-
49-
func ValueOf(v any) reflect.Value {
50-
return Value(reflect.ValueOf(v))
51-
}

Diff for: internal/deref/deref_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestDeref(t *testing.T) {
1515
c := &b
1616
d := &c
1717

18-
got := deref.Deref(d)
18+
got := deref.Interface(d)
1919
assert.Equal(t, uint(42), got)
2020
}
2121

@@ -25,14 +25,14 @@ func TestDeref_mix_ptr_with_interface(t *testing.T) {
2525
var c any = &b
2626
d := &c
2727

28-
got := deref.Deref(d)
28+
got := deref.Interface(d)
2929
assert.Equal(t, uint(42), got)
3030
}
3131

3232
func TestDeref_nil(t *testing.T) {
3333
var a *int
34-
assert.Nil(t, deref.Deref(a))
35-
assert.Nil(t, deref.Deref(nil))
34+
assert.Nil(t, deref.Interface(a))
35+
assert.Nil(t, deref.Interface(nil))
3636
}
3737

3838
func TestType(t *testing.T) {

Diff for: vm/vm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
457457

458458
case OpDeref:
459459
a := vm.pop()
460-
vm.push(deref.Deref(a))
460+
vm.push(deref.Interface(a))
461461

462462
case OpIncrementIndex:
463463
vm.scope().Index++

0 commit comments

Comments
 (0)