mirror of
https://github.com/wader/fq.git
synced 2024-11-26 10:33:53 +03:00
xml,csv,interp: Handle JQValue when string normalizing
Otherwise nested array/object JQValue:s could end up being JSON marshalled
This commit is contained in:
parent
9e34754fca
commit
fa20c74c22
@ -171,15 +171,15 @@ func Cast[T any](v any) (T, bool) {
|
|||||||
func NormalizeFn(v any, fn func(v any) any) any {
|
func NormalizeFn(v any, fn func(v any) any) any {
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
for i, e := range v {
|
for k, e := range v {
|
||||||
v[i] = NormalizeFn(e, fn)
|
v[k] = NormalizeFn(e, fn)
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
case map[any]any:
|
case map[any]any:
|
||||||
// for gopkg.in/yaml.v2
|
// for gopkg.in/yaml.v2
|
||||||
vm := map[string]any{}
|
vm := map[string]any{}
|
||||||
for i, e := range v {
|
for k, e := range v {
|
||||||
switch i := i.(type) {
|
switch i := k.(type) {
|
||||||
case string:
|
case string:
|
||||||
vm[i] = NormalizeFn(e, fn)
|
vm[i] = NormalizeFn(e, fn)
|
||||||
case int:
|
case int:
|
||||||
@ -198,6 +198,8 @@ func NormalizeFn(v any, fn func(v any) any) any {
|
|||||||
v[i] = NormalizeFn(e, fn)
|
v[i] = NormalizeFn(e, fn)
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
|
case gojq.JQValue:
|
||||||
|
return NormalizeFn(v.JQValueToGoJQ(), fn)
|
||||||
default:
|
default:
|
||||||
return fn(v)
|
return fn(v)
|
||||||
}
|
}
|
||||||
|
35
pkg/interp/testdata/gojq.fqtest
vendored
35
pkg/interp/testdata/gojq.fqtest
vendored
@ -252,3 +252,38 @@ $ fq -d mp3 '.frames[0] | to_entries[].key' test.mp3
|
|||||||
"xing"
|
"xing"
|
||||||
"padding"
|
"padding"
|
||||||
"crc_calculated"
|
"crc_calculated"
|
||||||
|
# xml, csv does string normalization, make sure it works with nested JQValue:s
|
||||||
|
# TODO: move this test as it depends on xml
|
||||||
|
$ fq -r '.headers[0] | toxml({indent: 2})' test.mp3
|
||||||
|
<doc>
|
||||||
|
<flags>
|
||||||
|
<experimental_indicator>false</experimental_indicator>
|
||||||
|
<extended_header>false</extended_header>
|
||||||
|
<unsynchronisation>false</unsynchronisation>
|
||||||
|
<unused>0</unused>
|
||||||
|
</flags>
|
||||||
|
<frames>
|
||||||
|
<flags>
|
||||||
|
<compression>false</compression>
|
||||||
|
<data_length_indicator>false</data_length_indicator>
|
||||||
|
<encryption>false</encryption>
|
||||||
|
<file_alter_preservation>false</file_alter_preservation>
|
||||||
|
<grouping_identity>false</grouping_identity>
|
||||||
|
<read_only>false</read_only>
|
||||||
|
<tag_alter_preservation>false</tag_alter_preservation>
|
||||||
|
<unsync>false</unsync>
|
||||||
|
<unused0>0</unused0>
|
||||||
|
<unused1>0</unused1>
|
||||||
|
<unused2>0</unused2>
|
||||||
|
</flags>
|
||||||
|
<id>TSSE</id>
|
||||||
|
<size>15</size>
|
||||||
|
<text>Lavf58.45.100</text>
|
||||||
|
<text_encoding>utf8</text_encoding>
|
||||||
|
</frames>
|
||||||
|
<magic>ID3</magic>
|
||||||
|
<padding><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></padding>
|
||||||
|
<revision>0</revision>
|
||||||
|
<size>35</size>
|
||||||
|
<version>4</version>
|
||||||
|
</doc>
|
||||||
|
Loading…
Reference in New Issue
Block a user