Skip to content

Commit

Permalink
Fix len and keys funcs regarding new map type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Jun 1, 2023
1 parent 77eab5c commit 903359d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Nothing yet.

## [v2.3.4] - 2023-06-01

### Fixed

- `len` function now works with new map type.
- `keys` function now works with new map type.

## [v2.3.3] - 2023-05-31

### Fixed
Expand Down Expand Up @@ -587,7 +594,8 @@ See [documentation](https://daseldocs.tomwright.me) for all changes.

- Everything!

[unreleased]: https://github.com/TomWright/dasel/compare/v2.3.3...HEAD
[unreleased]: https://github.com/TomWright/dasel/compare/v2.3.4...HEAD
[v2.3.4]: https://github.com/TomWright/dasel/compare/v2.3.3...v2.3.4
[v2.3.3]: https://github.com/TomWright/dasel/compare/v2.3.2...v2.3.3
[v2.3.2]: https://github.com/TomWright/dasel/compare/v2.3.1...v2.3.2
[v2.3.1]: https://github.com/TomWright/dasel/compare/v2.3.0...v2.3.1
Expand Down
17 changes: 14 additions & 3 deletions func_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dasel

import (
"fmt"
"github.com/tomwright/dasel/v2/dencoding"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -67,9 +68,19 @@ var KeysFunc = BasicFunction{

res[i] = ValueOf(list)
default:
return nil, &ErrInvalidType{
ExpectedTypes: []string{"slice", "array", "map"},
CurrentType: val.Kind().String(),
if val.IsDencodingMap() {
dencodingMap := val.Interface().(*dencoding.Map)
mapKeys := dencodingMap.Keys()
list := make([]any, 0, len(mapKeys))
for _, k := range mapKeys {
list = append(list, k)
}
res[i] = ValueOf(list)
} else {
return nil, &ErrInvalidType{
ExpectedTypes: []string{"slice", "array", "map"},
CurrentType: val.Kind().String(),
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions func_keys_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dasel

import "testing"
import (
"github.com/tomwright/dasel/v2/dencoding"
"testing"
)

func TestKeysFunc(t *testing.T) {
testdata := map[string]any{
Expand All @@ -9,14 +12,18 @@ func TestKeysFunc(t *testing.T) {
},
"list": []any{111, 222, 333},
"string": "something",
"dencodingMap": dencoding.NewMap().
Set("a", 1).
Set("b", 2).
Set("c", 3),
}

t.Run(
"root",
selectTest(
"keys()",
testdata,
[]any{[]any{"list", "object", "string"}},
[]any{[]any{"list", "object", "string", "dencodingMap"}},
),
)

Expand All @@ -38,6 +45,15 @@ func TestKeysFunc(t *testing.T) {
),
)

t.Run(
"Dencoding Map",
selectTest(
"dencodingMap.keys()",
testdata,
[]any{[]any{"a", "b", "c"}}, // sorted
),
)

t.Run("InvalidType",
selectTestErr(
"string.keys()",
Expand Down
11 changes: 7 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (v Value) Interface() interface{} {

// Len returns v's length.
func (v Value) Len() int {
if v.IsDencodingMap() {
return len(v.Interface().(*dencoding.Map).Keys())
}
switch v.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return v.Unpack().Len()
Expand Down Expand Up @@ -104,7 +107,7 @@ func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {

var dencodingMapType = reflect.TypeOf(&dencoding.Map{})

func isdencodingMap(value reflect.Value) bool {
func isDencodingMap(value reflect.Value) bool {
return value.Kind() == reflect.Ptr && value.Type() == dencodingMapType
}

Expand All @@ -114,7 +117,7 @@ func unpackReflectValue(value reflect.Value, kinds ...reflect.Kind) reflect.Valu
}
res := value
for {
if isdencodingMap(res) {
if isDencodingMap(res) {
return res
}
if !containsKind(kinds, res.Kind()) {
Expand Down Expand Up @@ -389,7 +392,7 @@ func makeAddressableMap(value reflect.Value) reflect.Value {
func makeAddressable(value reflect.Value) reflect.Value {
unpacked := unpackReflectValue(value)

if isdencodingMap(unpacked) {
if isDencodingMap(unpacked) {
om := value.Interface().(*dencoding.Map)
for _, kv := range om.KeyValues() {
var val any
Expand Down Expand Up @@ -446,7 +449,7 @@ func derefMap(value reflect.Value) reflect.Value {
func deref(value reflect.Value) reflect.Value {
unpacked := unpackReflectValue(value)

if isdencodingMap(unpacked) {
if isDencodingMap(unpacked) {
om := value.Interface().(*dencoding.Map)
for _, kv := range om.KeyValues() {
if v := deref(reflect.ValueOf(kv.Value)); v.IsValid() {
Expand Down

0 comments on commit 903359d

Please sign in to comment.