Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Sep 30, 2024
1 parent e09907d commit f4427b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
45 changes: 26 additions & 19 deletions execution/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))})
},
}))
})
Expand Down
5 changes: 3 additions & 2 deletions model/value_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"fmt"
"reflect"
"slices"
)

func NewStringValue(x string) *Value {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit f4427b6

Please sign in to comment.