Skip to content

Commit c1317be

Browse files
committed
Support wildcard replacements and rounding as well
1 parent fb56eac commit c1317be

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

golden/map.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math"
66
"reflect"
7+
"regexp"
78
"strconv"
89
"strings"
910
"time"
@@ -23,17 +24,32 @@ func replaceTransient(
2324

2425
replaced := map[string]any{}
2526
for key, value := range original {
27+
// Keep the original value.
2628
replaced[key] = value
29+
30+
// Check if the field is meant to be replaced. If not, continue.
31+
// We also check for wildcard replacements that are meant to replace all
32+
// fields in a slice.
2733
replacement, isTransient := transientLookup[key]
28-
if !isTransient {
34+
cleanedKey := replaceIndicesInKeys(key)
35+
replacementCleaned, isTransientCleaned := transientLookup[cleanedKey]
36+
if !isTransient && !isTransientCleaned {
37+
// No replacement defined, continue and keep the original value.
2938
continue
3039
}
40+
if isTransientCleaned {
41+
replacement = replacementCleaned
42+
}
3143

44+
// Replace the value with the replacement value.
3245
if replacement != nil {
3346
replaced[key] = replacement
3447
continue
3548
}
3649

50+
// No replacement defined, we fall back to default stable values here
51+
// (based on type).
52+
3753
if stringValue, isString := value.(string); isString {
3854
if _, err := time.Parse(time.RFC3339, stringValue); err == nil {
3955
replaced[key] = StableTime
@@ -82,21 +98,35 @@ func roundFields(
8298

8399
replaced := map[string]any{}
84100
for key, value := range original {
101+
// Keep the original value.
85102
replaced[key] = value
103+
104+
// Check if the field is meant to be rounded. If not, continue.
105+
// We also check for wildcard replacements that are meant to replace all
106+
// fields in a slice.
107+
cleanedKey := replaceIndicesInKeys(key)
86108
replacement, isRounded := roundingLookup[key]
87-
if !isRounded {
109+
replacementCleaned, isRoundedCleaned := roundingLookup[cleanedKey]
110+
if !isRounded && !isRoundedCleaned {
111+
// No rounding defined, continue and keep the original value.
88112
continue
89113
}
114+
if isRoundedCleaned {
115+
replacement = replacementCleaned
116+
}
90117

118+
// We don't deal with negative precision values.
91119
if replacement < 0 {
92120
continue
93121
}
94122

123+
// Replace the value with the rounded value.
95124
if _, isFloat := value.(float64); isFloat {
96125
replaced[key] = round(value.(float64), replacement)
97126
continue
98127
}
99128

129+
// If the value was not a float, return an error.
100130
return nil, fmt.Errorf("field %s is not a float", key)
101131
}
102132

@@ -109,6 +139,14 @@ func round(value float64, precision int) float64 {
109139
return math.Round(value*shift) / shift
110140
}
111141

142+
var keyIndexMatcher = regexp.MustCompile(`\[\d+\]`)
143+
144+
// replaceIndicesInKeys replaces all the indices in a key with "[]" to make it
145+
// easier to match them with configuration defined in jq-style.
146+
func replaceIndicesInKeys(key string) string {
147+
return keyIndexMatcher.ReplaceAllString(key, "[]")
148+
}
149+
112150
/*
113151
flatten takes a nested map and flattens it into a single level map. The
114152
flattening roughly follows the [JSONPath] standard. Please see test function to

0 commit comments

Comments
 (0)