Last active
January 10, 2020 21:37
-
-
Save bzub/971780d8455a6dc7a67f36a6cc94c16a to your computer and use it in GitHub Desktop.
use deepfield for associative key checck
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
{`nested associative -- add same name, different path`, | |
` | |
kind: Deployment | |
volumes: | |
- name: vol1 | |
projected: | |
sources: | |
- secret: | |
name: source1 | |
optional: false | |
`, | |
` | |
kind: Deployment | |
volumes: | |
- name: vol1 | |
projected: | |
sources: | |
- configMap: | |
name: source1 | |
optional: false | |
`, | |
` | |
kind: Deployment | |
volumes: | |
- name: vol1 | |
projected: | |
sources: | |
- configMap: | |
name: source1 | |
optional: false | |
- secret: | |
name: source1 | |
optional: false | |
`, | |
}, |
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
// checkKey returns true if all elems have the key | |
func checkKey(key string, elems []*Node) bool { | |
count := 0 | |
paths := [][]string{} | |
for i := range elems { | |
elem := NewRNode(elems[i]) | |
path, mn := elem.DeepField(key) | |
if mn != nil { | |
count++ | |
paths = append(paths, path) | |
} | |
} | |
// Make sure all elems have the key. | |
if count != len(elems) { | |
return false | |
} | |
// Make sure nested keys have the same path depth. | |
depth := len(paths[0]) | |
for i := range paths { | |
if len(paths[i]) != depth { | |
return false | |
} | |
} | |
// fmt.Fprintf(os.Stderr, "key: %v\n", key) | |
for i := range paths { | |
fmt.Fprintf(os.Stderr, "path: %v\n", strings.Join(paths[i], ".")) | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment