mirror of
https://github.com/wader/fq.git
synced 2024-12-28 08:02:28 +03:00
Merge pull request #638 from wader/interp-func-decode-value-struct-args
interp: Allow and convert JQValues:s (ex decode value) in function ar…
This commit is contained in:
commit
5c69fcc9df
@ -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")
|
||||
}
|
||||
|
4
pkg/interp/testdata/value.fqtest
vendored
4
pkg/interp/testdata/value.fqtest
vendored
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user