Skip to content

Commit

Permalink
interp: Improve type normalization and use it for toyaml and totoml
Browse files Browse the repository at this point in the history
Use smallest int type for int64, uint6 and *big.Int

Fixes integer serialization for yaml and toml for small integers, othweise
they will end up as strings.
  • Loading branch information
wader committed May 28, 2022
1 parent c8a9cf9 commit 735c443
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions internal/gojqextra/totype.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,33 @@ func ToGoJQValue(v any) (any, bool) {
case int:
return vv, true
case int64:
if vv >= math.MinInt && vv <= math.MaxInt {
return int(vv), true
}
return big.NewInt(vv), true
case uint64:
if vv <= math.MaxInt {
return int(vv), true
}
return new(big.Int).SetUint64(vv), true
case float32:
return float64(vv), true
case float64:
return vv, true
case *big.Int:
if vv.IsInt64() {
vv := vv.Int64()
if vv >= math.MinInt && vv <= math.MaxInt {
return int(vv), true
}
return vv, true
} else if vv.IsUint64() {
vv := vv.Uint64()
if vv <= math.MaxInt {
return int(vv), true
}
return vv, true
}
return vv, true
case string:
return vv, true
Expand Down
4 changes: 2 additions & 2 deletions pkg/interp/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ func init() {
})

addFunc("_toyaml", func(c any) any {
b, err := yaml.Marshal(c)
b, err := yaml.Marshal(norm(c))
if err != nil {
return err
}
Expand All @@ -734,7 +734,7 @@ func init() {

addFunc("_totoml", func(c map[string]any) any {
b := &bytes.Buffer{}
if err := toml.NewEncoder(b).Encode(c); err != nil {
if err := toml.NewEncoder(b).Encode(norm(c)); err != nil {
return err
}
return b.String()
Expand Down
1 change: 1 addition & 0 deletions pkg/interp/testdata/encoding/toml.fqtest
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# toml does not support null in arrays
# TODO: add uint64 norm test
$ fq -rRs 'fromjson[] | (walk(if type == "array" then map(select(. != null)) end) | try (totoml | ., fromtoml) catch .), "----"' variants.json

{}
Expand Down
1 change: 1 addition & 0 deletions pkg/interp/testdata/encoding/yaml.fqtest
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: add uint64 norm test
$ fq -rRs 'fromjson[] | (try (toyaml | ., fromyaml) catch .), "----"' variants.json
null

Expand Down

0 comments on commit 735c443

Please sign in to comment.