Skip to content

Commit 716ee8a

Browse files
committed
Add badjson.UnmarshallExcludedMulti
1 parent f9ca5a8 commit 716ee8a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

common/json/badjson/merge_objects.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,41 @@ func UnmarshallExcludedContext(ctx context.Context, inputContent []byte, parentO
5454
return json.UnmarshalContextDisallowUnknownFields(ctx, inputContent, object)
5555
}
5656

57+
func UnmarshallExcludedMulti(inputContent []byte, parentObject any, object any) error {
58+
return UnmarshallExcludedContextMulti(context.Background(), inputContent, parentObject, object)
59+
}
60+
61+
func UnmarshallExcludedContextMulti(ctx context.Context, inputContent []byte, parentObject any, object any) error {
62+
var content JSONObject
63+
err := content.UnmarshalJSONContext(ctx, inputContent)
64+
if err != nil {
65+
return err
66+
}
67+
parentBinary, err := json.MarshalContext(ctx, parentObject)
68+
if err != nil {
69+
return err
70+
}
71+
var parentMap map[string]any
72+
err = json.UnmarshalContext(ctx, parentBinary, &parentMap)
73+
if err != nil {
74+
return err
75+
}
76+
for key := range parentMap {
77+
content.Remove(key)
78+
}
79+
if object == nil {
80+
if content.IsEmpty() {
81+
return nil
82+
}
83+
return E.New("unexpected key: ", content.Keys()[0])
84+
}
85+
inputContent, err = content.MarshalJSONContext(ctx)
86+
if err != nil {
87+
return err
88+
}
89+
return json.UnmarshalContextDisallowUnknownFields(ctx, inputContent, object)
90+
}
91+
5792
func newJSONObject(ctx context.Context, object any) (*JSONObject, error) {
5893
inputContent, err := json.MarshalContext(ctx, object)
5994
if err != nil {

0 commit comments

Comments
 (0)