badjson: Add disableAppend option

This commit is contained in:
世界 2024-06-22 20:04:42 +08:00
parent 1c495c9b07
commit 9571124cf4
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -30,7 +30,7 @@ func Omitempty[T any](value T) (T, error) {
return newObject, nil
}
func Merge[T any](source T, destination T) (T, error) {
func Merge[T any](source T, destination T, disableAppend bool) (T, error) {
rawSource, err := json.Marshal(source)
if err != nil {
return common.DefaultValue[T](), E.Cause(err, "marshal source")
@ -39,10 +39,10 @@ func Merge[T any](source T, destination T) (T, error) {
if err != nil {
return common.DefaultValue[T](), E.Cause(err, "marshal destination")
}
return MergeFrom[T](rawSource, rawDestination)
return MergeFrom[T](rawSource, rawDestination, disableAppend)
}
func MergeFromSource[T any](rawSource json.RawMessage, destination T) (T, error) {
func MergeFromSource[T any](rawSource json.RawMessage, destination T, disableAppend bool) (T, error) {
if rawSource == nil {
return destination, nil
}
@ -50,10 +50,10 @@ func MergeFromSource[T any](rawSource json.RawMessage, destination T) (T, error)
if err != nil {
return common.DefaultValue[T](), E.Cause(err, "marshal destination")
}
return MergeFrom[T](rawSource, rawDestination)
return MergeFrom[T](rawSource, rawDestination, disableAppend)
}
func MergeFromDestination[T any](source T, rawDestination json.RawMessage) (T, error) {
func MergeFromDestination[T any](source T, rawDestination json.RawMessage, disableAppend bool) (T, error) {
if rawDestination == nil {
return source, nil
}
@ -61,11 +61,11 @@ func MergeFromDestination[T any](source T, rawDestination json.RawMessage) (T, e
if err != nil {
return common.DefaultValue[T](), E.Cause(err, "marshal source")
}
return MergeFrom[T](rawSource, rawDestination)
return MergeFrom[T](rawSource, rawDestination, disableAppend)
}
func MergeFrom[T any](rawSource json.RawMessage, rawDestination json.RawMessage) (T, error) {
rawMerged, err := MergeJSON(rawSource, rawDestination)
func MergeFrom[T any](rawSource json.RawMessage, rawDestination json.RawMessage, disableAppend bool) (T, error) {
rawMerged, err := MergeJSON(rawSource, rawDestination, disableAppend)
if err != nil {
return common.DefaultValue[T](), E.Cause(err, "merge options")
}
@ -77,7 +77,7 @@ func MergeFrom[T any](rawSource json.RawMessage, rawDestination json.RawMessage)
return merged, nil
}
func MergeJSON(rawSource json.RawMessage, rawDestination json.RawMessage) (json.RawMessage, error) {
func MergeJSON(rawSource json.RawMessage, rawDestination json.RawMessage, disableAppend bool) (json.RawMessage, error) {
if rawSource == nil && rawDestination == nil {
return nil, os.ErrInvalid
} else if rawSource == nil {
@ -98,21 +98,23 @@ func MergeJSON(rawSource json.RawMessage, rawDestination json.RawMessage) (json.
} else if destination == nil {
return json.Marshal(source)
}
merged, err := mergeJSON(source, destination)
merged, err := mergeJSON(source, destination, disableAppend)
if err != nil {
return nil, err
}
return json.Marshal(merged)
}
func mergeJSON(anySource any, anyDestination any) (any, error) {
func mergeJSON(anySource any, anyDestination any, disableAppend bool) (any, error) {
switch destination := anyDestination.(type) {
case JSONArray:
switch source := anySource.(type) {
case JSONArray:
destination = append(destination, source...)
default:
destination = append(destination, source)
if !disableAppend {
switch source := anySource.(type) {
case JSONArray:
destination = append(destination, source...)
default:
destination = append(destination, source)
}
}
return destination, nil
case *JSONObject:
@ -122,7 +124,7 @@ func mergeJSON(anySource any, anyDestination any) (any, error) {
oldValue, loaded := destination.Get(entry.Key)
if loaded {
var err error
entry.Value, err = mergeJSON(entry.Value, oldValue)
entry.Value, err = mergeJSON(entry.Value, oldValue, disableAppend)
if err != nil {
return nil, E.Cause(err, "merge object item ", entry.Key)
}