From f4427b6a33fbaaf612fe1ffa6ce476625806480e Mon Sep 17 00:00:00 2001 From: Tom Wright Date: Mon, 30 Sep 2024 19:53:40 +0100 Subject: [PATCH] Fix issues --- execution/execute_test.go | 45 ++++++++++++++++++++++----------------- model/value_literal.go | 5 +++-- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/execution/execute_test.go b/execution/execute_test.go index 959edb2..75cc22f 100644 --- a/execution/execute_test.go +++ b/execution/execute_test.go @@ -9,6 +9,7 @@ import ( "github.com/tomwright/dasel/v3/dencoding" "github.com/tomwright/dasel/v3/execution" "github.com/tomwright/dasel/v3/model" + "github.com/tomwright/dasel/v3/ptr" ) func TestExecuteSelector_HappyPath(t *testing.T) { @@ -184,28 +185,34 @@ func TestExecuteSelector_HappyPath(t *testing.T) { t.Run("map", func(t *testing.T) { t.Run("property from slice of maps", runTest(testCase{ inFn: func() *model.Value { - r := model.NewSliceValue() - - m1 := model.NewMapValue() - _ = m1.SetMapKey("number", model.NewIntValue(1)) - m2 := model.NewMapValue() - _ = m2.SetMapKey("number", model.NewIntValue(2)) - m3 := model.NewMapValue() - _ = m3.SetMapKey("number", model.NewIntValue(3)) - - _ = r.Append(m1) - _ = r.Append(m2) - _ = r.Append(m3) - - return r + return model.NewValue([]any{ + dencoding.NewMap().Set("number", 1), + dencoding.NewMap().Set("number", 2), + dencoding.NewMap().Set("number", 3), + }) }, s: `map(number)`, outFn: func() *model.Value { - r := model.NewSliceValue() - _ = r.Append(model.NewIntValue(1)) - _ = r.Append(model.NewIntValue(2)) - _ = r.Append(model.NewIntValue(3)) - return r + return model.NewValue([]any{1, 2, 3}) + }, + })) + t.Run("with chain of selectors", runTest(testCase{ + inFn: func() *model.Value { + return model.NewValue([]any{ + dencoding.NewMap().Set("foo", 1).Set("bar", 4), + dencoding.NewMap().Set("foo", 2).Set("bar", 5), + dencoding.NewMap().Set("foo", 3).Set("bar", 6), + }) + }, + s: ` + .map ( + { + total = add( foo, bar, 1 ) + } + ) + .map ( total )`, + outFn: func() *model.Value { + return model.NewValue([]any{ptr.To(int64(6)), ptr.To(int64(8)), ptr.To(int64(10))}) }, })) }) diff --git a/model/value_literal.go b/model/value_literal.go index 1879a0e..9be3af8 100644 --- a/model/value_literal.go +++ b/model/value_literal.go @@ -3,6 +3,7 @@ package model import ( "fmt" "reflect" + "slices" ) func NewStringValue(x string) *Value { @@ -38,7 +39,7 @@ func (v *Value) IsInt() bool { } func (v *Value) isInt() bool { - return v.Value.Kind() == reflect.Int64 + return slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64}, v.Value.Kind()) } func (v *Value) IntValue() (int64, error) { @@ -60,7 +61,7 @@ func (v *Value) IsFloat() bool { } func (v *Value) isFloat() bool { - return v.Value.Kind() == reflect.Float64 + return slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64}, v.Value.Kind()) } func (v *Value) FloatValue() (float64, error) {