Centralize trusted JSON value cloning
This commit is contained in:
27
internal/jsonvalue/trusted_test.go
Normal file
27
internal/jsonvalue/trusted_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package jsonvalue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type trustedNamed string
|
||||
|
||||
func TestCloneTrustedPreservesTypesAndDetaches(t *testing.T) {
|
||||
named := trustedNamed("value")
|
||||
original := map[string]any{"": map[string]any{"number": json.Number("9007199254740993")}, "typed": []string{"original"}, "pointer": &named, "nilmap": map[string]any(nil), "nilslice": []string(nil), "empty": []any{}}
|
||||
copy := CloneTrusted(original).(map[string]any)
|
||||
if !reflect.DeepEqual(copy, original) {
|
||||
t.Fatalf("clone = %#v", copy)
|
||||
}
|
||||
copy[""].(map[string]any)["number"] = json.Number("1")
|
||||
copy["typed"].([]string)[0] = "changed"
|
||||
*copy["pointer"].(*trustedNamed) = "changed"
|
||||
if original[""].(map[string]any)["number"] != json.Number("9007199254740993") || original["typed"].([]string)[0] != "original" || named != "value" {
|
||||
t.Fatal("clone shares mutable storage")
|
||||
}
|
||||
if copy["nilmap"].(map[string]any) != nil || copy["nilslice"].([]string) != nil || copy["empty"].([]any) == nil {
|
||||
t.Fatal("nil/empty shape changed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user