-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create references only in string values in JSON templates
- Loading branch information
1 parent
a928904
commit dfaff8c
Showing
4 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* @license | ||
* Copyright 2025 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package json | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
// ApplyToStringValues unmarshals a JSON string and applies the specified transformation function to each string value before remarshaling and returning the result. | ||
func ApplyToStringValues(jsonString string, f func(v string) string) (string, error) { | ||
var v interface{} | ||
if err := json.Unmarshal([]byte(jsonString), &v); err != nil { | ||
return "", err | ||
} | ||
|
||
v = walkAnyAndApplyToStringValues(v, f) | ||
|
||
b, err := json.Marshal(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(b), nil | ||
} | ||
|
||
func walkAnyAndApplyToStringValues(v any, f func(v string) string) any { | ||
switch vv := v.(type) { | ||
case string: | ||
if f == nil { | ||
return vv | ||
} | ||
return f(vv) | ||
|
||
case []interface{}: | ||
for i, u := range vv { | ||
vv[i] = walkAnyAndApplyToStringValues(u, f) | ||
} | ||
return vv | ||
|
||
case map[string]interface{}: | ||
for k, u := range vv { | ||
vv[k] = walkAnyAndApplyToStringValues(u, f) | ||
} | ||
return vv | ||
|
||
default: | ||
return vv | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
//go:build unit | ||
|
||
/* | ||
* @license | ||
* Copyright 2025 Dynatrace LLC | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package json | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestApplyToStringValues_Success(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
content string | ||
f func(s string) string | ||
expectedResult string | ||
}{ | ||
{ | ||
name: "boolean value is preserved", | ||
content: "true", | ||
expectedResult: "true", | ||
}, | ||
{ | ||
name: "boolean value is not replaced", | ||
content: "true", | ||
f: func(s string) string { return strings.ReplaceAll(s, "true", "true") }, | ||
expectedResult: "true", | ||
}, | ||
{ | ||
name: "float value is preserved", | ||
content: "10", | ||
expectedResult: "10", | ||
}, | ||
{ | ||
name: "float value is not replaced", | ||
content: "10", | ||
f: func(s string) string { return strings.ReplaceAll(s, "10", "10") }, | ||
expectedResult: "10", | ||
}, | ||
{ | ||
name: "string value is replaced if found", | ||
content: "\"find\"", | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: "\"replace\"", | ||
}, | ||
{ | ||
name: "string value is not replaced if not found", | ||
content: "\"nope\"", | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: "\"nope\"", | ||
}, | ||
{ | ||
name: "string value is replaced if found in object", | ||
content: `{"key": "find"}`, | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: `{"key":"replace"}`, | ||
}, | ||
{ | ||
name: "string value is not replaced if not found in object", | ||
content: `{"key": "nope"}`, | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: `{"key":"nope"}`, | ||
}, | ||
{ | ||
name: "string value is replaced if found in object in array", | ||
content: `[{"key": "find"}]`, | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: `[{"key":"replace"}]`, | ||
}, | ||
{ | ||
name: "string value is not replaced if not found in object in array", | ||
content: `[{"key": "nope"}]`, | ||
f: func(s string) string { return strings.ReplaceAll(s, "find", "replace") }, | ||
expectedResult: `[{"key":"nope"}]`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := ApplyToStringValues(tt.content, tt.f) | ||
assert.EqualValues(t, tt.expectedResult, result) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestApplyToStringValues_Errors(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
content string | ||
f func(s string) string | ||
}{ | ||
{ | ||
name: "empty string doesnt work", | ||
content: "", | ||
}, | ||
{ | ||
name: "unquoted string produces error", | ||
content: "something", | ||
}, | ||
{ | ||
name: "truncated json produces error", | ||
content: `{ "key": "value", `, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := ApplyToStringValues(tt.content, tt.f) | ||
assert.Empty(t, result) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters