Skip to content

Commit bd7d845

Browse files
committed
make use of maps.Copy and maps.Clone
1 parent 0d62f9d commit bd7d845

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

cli/test.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,16 @@
29482948
""
29492949
""
29502950
2951+
- name: add objects
2952+
args:
2953+
- -c
2954+
- '.[0] + .[]'
2955+
input: '[{"a": 1}, {"b": 2}, {"a": 3}]'
2956+
expected: |
2957+
{"a":1}
2958+
{"a":1,"b":2}
2959+
{"a":3}
2960+
29512961
- name: multiply objects
29522962
args:
29532963
- -c

func.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"maps"
910
"math"
1011
"math/big"
1112
"net/url"
@@ -489,16 +490,10 @@ func funcAdd(v any) any {
489490
case map[string]any:
490491
switch w := v.(type) {
491492
case nil:
492-
m := make(map[string]any, len(x))
493-
for k, e := range x {
494-
m[k] = e
495-
}
496-
v = m
493+
v = maps.Clone(x)
497494
continue
498495
case map[string]any:
499-
for k, e := range x {
500-
w[k] = e
501-
}
496+
maps.Copy(w, x)
502497
continue
503498
}
504499
}
@@ -1664,9 +1659,7 @@ func updateObject(v map[string]any, k string, path []any, n any, a allocator) (a
16641659
return v, nil
16651660
}
16661661
w := a.makeObject(len(v) + 1)
1667-
for k, v := range v {
1668-
w[k] = v
1669-
}
1662+
maps.Copy(w, v)
16701663
w[k] = u
16711664
return w, nil
16721665
}

operator.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gojq
22

33
import (
4+
"maps"
45
"math"
56
"math/big"
67
"strings"
@@ -334,12 +335,8 @@ func funcOpAdd(_, l, r any) any {
334335
return l
335336
}
336337
m := make(map[string]any, len(l)+len(r))
337-
for k, v := range l {
338-
m[k] = v
339-
}
340-
for k, v := range r {
341-
m[k] = v
342-
}
338+
maps.Copy(m, l)
339+
maps.Copy(m, r)
343340
return m
344341
},
345342
func(l, r any) any {
@@ -416,9 +413,7 @@ func funcOpMul(_, l, r any) any {
416413

417414
func deepMergeObjects(l, r map[string]any) any {
418415
m := make(map[string]any, len(l)+len(r))
419-
for k, v := range l {
420-
m[k] = v
421-
}
416+
maps.Copy(m, l)
422417
for k, v := range r {
423418
if mk, ok := m[k]; ok {
424419
if mk, ok := mk.(map[string]any); ok {

0 commit comments

Comments
 (0)