From fa20c74c223308a50d4bbcaff4cee2c0e4317b4f Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Tue, 26 Jul 2022 17:43:59 +0200 Subject: [PATCH] xml,csv,interp: Handle JQValue when string normalizing Otherwise nested array/object JQValue:s could end up being JSON marshalled --- internal/gojqextra/types.go | 10 ++++++---- pkg/interp/testdata/gojq.fqtest | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/internal/gojqextra/types.go b/internal/gojqextra/types.go index 91feed00..382b6406 100644 --- a/internal/gojqextra/types.go +++ b/internal/gojqextra/types.go @@ -171,15 +171,15 @@ func Cast[T any](v any) (T, bool) { func NormalizeFn(v any, fn func(v any) any) any { switch v := v.(type) { case map[string]any: - for i, e := range v { - v[i] = NormalizeFn(e, fn) + for k, e := range v { + v[k] = NormalizeFn(e, fn) } return v case map[any]any: // for gopkg.in/yaml.v2 vm := map[string]any{} - for i, e := range v { - switch i := i.(type) { + for k, e := range v { + switch i := k.(type) { case string: vm[i] = NormalizeFn(e, fn) case int: @@ -198,6 +198,8 @@ func NormalizeFn(v any, fn func(v any) any) any { v[i] = NormalizeFn(e, fn) } return v + case gojq.JQValue: + return NormalizeFn(v.JQValueToGoJQ(), fn) default: return fn(v) } diff --git a/pkg/interp/testdata/gojq.fqtest b/pkg/interp/testdata/gojq.fqtest index e05088fe..7be91d8b 100644 --- a/pkg/interp/testdata/gojq.fqtest +++ b/pkg/interp/testdata/gojq.fqtest @@ -252,3 +252,38 @@ $ fq -d mp3 '.frames[0] | to_entries[].key' test.mp3 "xing" "padding" "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 + + + false + false + false + 0 + + + + false + false + false + false + false + false + false + false + 0 + 0 + 0 + + TSSE + 15 + Lavf58.45.100 + utf8 + + ID3 + ���������� + 0 + 35 + 4 +