Skip to content

Commit

Permalink
Merge pull request #638 from wader/interp-func-decode-value-struct-args
Browse files Browse the repository at this point in the history
interp: Allow and convert JQValues:s (ex decode value) in function ar…
  • Loading branch information
wader authored Apr 12, 2023
2 parents 089cf9b + 9a1ef84 commit 5c69fcc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
17 changes: 3 additions & 14 deletions internal/gojqex/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,11 @@ func CastFn[T any](v any, structFn func(input any, result any) error) (T, bool)
default:
ft := reflect.TypeOf(&t)
if ft.Elem().Kind() == reflect.Struct {
m := map[string]any{}
switch v := v.(type) {
case map[string]any:
m = v
case nil:
// nop use instantiated map
case gojq.JQValue:
if jm, ok := Cast[map[string]any](v.JQValueToGoJQ()); ok {
m = jm
} else {
return t, false
}
default:
// TODO: some way to allow decode value passthru?
m, ok := ToGoJQValue(v)
if !ok {
return t, false
}

if structFn == nil {
panic("structFn nil")
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/interp/testdata/value.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,8 @@ mp3> .headers._index
error: expected a extkey but got: _index
mp3> ._index
error: expected a extkey but got: _index
mp3> .headers[0].header.version as $r | {a:12} | tojson({indent: $r}) | println
{
"a": 12
}
mp3> ^D

0 comments on commit 5c69fcc

Please sign in to comment.