-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support for strategic-merge This change, if applied, adds strategic-merge custom merge strategy to agnosticv. It can be applied to dict or list using the x-merge key in any schema. Once this change is merged, we will update in a separate PR the default for `__meta__` and `agnosticv_meta` variables to use 'strategic-merge' instead of 'merge'. * Use asciidoc footnote instead of dedicated column * wording
- Loading branch information
Showing
9 changed files
with
445 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
cloud_provider: none | ||
adict: | ||
strategic_list: | ||
- name: foo | ||
value: account | ||
alist: | ||
- fromaccount | ||
blist: | ||
|
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
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,90 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/imdario/mergo" | ||
) | ||
|
||
|
||
func strategicCleanupSlice(elems []any) ([]any, error) { | ||
result := []any{} | ||
done := map[string]int{} | ||
|
||
for index, elem := range elems { | ||
if reflect.TypeOf(elem).Kind() != reflect.Map { | ||
// strategic merge works only on map, if it's not a map, just add the elem and continue | ||
result = append(result, elem) | ||
continue | ||
} | ||
|
||
elemMap := elem.(map[string]any) | ||
|
||
|
||
if name, ok := elemMap["name"]; ok { | ||
if reflect.TypeOf(elemMap["name"]).Kind() != reflect.String { | ||
return result, fmt.Errorf("strategic merge cannot work if 'name' is not a string") | ||
} | ||
nameStr := name.(string) | ||
if doneIndex, ok := done[nameStr]; ok { | ||
// An element with the same name exists, replace that element | ||
result1 := append(result[:doneIndex], elem) | ||
result = append(result1, result[doneIndex+1:]...) | ||
continue | ||
} | ||
// Append element | ||
result = append(result, elem) | ||
done[nameStr] = index | ||
continue | ||
} | ||
result = append(result, elem) | ||
} | ||
|
||
return result, nil | ||
} | ||
func strategicCleanupMap(m map[string]any) error { | ||
for key, v := range m { | ||
if v == nil { | ||
continue | ||
} | ||
if reflect.TypeOf(v).Kind() == reflect.Map { | ||
vMap := v.(map[string]any) | ||
|
||
if err := strategicCleanupMap(vMap); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
|
||
if reflect.TypeOf(v).Kind() == reflect.Slice { | ||
vSlice := v.([]any) | ||
res, err := strategicCleanupSlice(vSlice) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m[key] = res | ||
continue | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func strategicMerge(dst map[string]any, src map[string]any) error { | ||
if err := mergo.Merge( | ||
&dst, | ||
src, | ||
mergo.WithOverride, | ||
mergo.WithOverwriteWithEmptyValue, | ||
mergo.WithAppendSlice, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
if err := strategicCleanupMap(dst); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.