Simplify casting in discardNullValuesFromPatch

This commit is contained in:
Arda Güçlü 2022-02-16 19:43:35 +03:00
parent 9b5d9c70fc
commit 0ee00ba104

View File

@ -1331,10 +1331,9 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
if !isDeleteList { if !isDeleteList {
// If it's not in the original document, just take the patch value. // If it's not in the original document, just take the patch value.
if mergeOptions.IgnoreUnmatchedNulls { if mergeOptions.IgnoreUnmatchedNulls {
original[k] = discardNullValuesFromPatch(patchV) discardNullValuesFromPatch(patchV)
} else {
original[k] = patchV
} }
original[k] = patchV
} }
continue continue
} }
@ -1344,10 +1343,9 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
if originalType != patchType { if originalType != patchType {
if !isDeleteList { if !isDeleteList {
if mergeOptions.IgnoreUnmatchedNulls { if mergeOptions.IgnoreUnmatchedNulls {
original[k] = discardNullValuesFromPatch(patchV) discardNullValuesFromPatch(patchV)
} else {
original[k] = patchV
} }
original[k] = patchV
} }
continue continue
} }
@ -1383,27 +1381,23 @@ func mergeMap(original, patch map[string]interface{}, schema LookupPatchMeta, me
return original, nil return original, nil
} }
// discardNullValuesFromPatch discards all null values from patch. // discardNullValuesFromPatch discards all null property values from patch.
// It traverses for all slices and map types to discard all nulls. // It traverses all slices and map types.
func discardNullValuesFromPatch(patchV interface{}) interface{} { func discardNullValuesFromPatch(patchV interface{}) {
switch patchV.(type) { switch patchV := patchV.(type) {
case map[string]interface{}: case map[string]interface{}:
for k, v := range patchV.(map[string]interface{}) { for k, v := range patchV {
if v == nil { if v == nil {
delete(patchV.(map[string]interface{}), k) delete(patchV, k)
} else { } else {
discardNullValuesFromPatch(v) discardNullValuesFromPatch(v)
} }
} }
case []interface{}: case []interface{}:
patchS := patchV.([]interface{}) for _, v := range patchV {
for i, v := range patchV.([]interface{}) { discardNullValuesFromPatch(v)
patchS[i] = discardNullValuesFromPatch(v)
} }
return patchS
} }
return patchV
} }
// mergeMapHandler handles how to merge `patchV` whose key is `key` with `original` respecting // mergeMapHandler handles how to merge `patchV` whose key is `key` with `original` respecting