I skipped over to that package, and the doc says it converts from yaml to json, and the marshals/unmarshals. I tried changing your snippet to use struct tags with the key name as "json" instead of "yaml", and it immediately behaved as expected: https://goplay.tools/snippet/PSZtr1YErD8
While you're fine to use it like you are, the package you're using is a wrapper around another tool, pkg.go.dev/gopkg.in/yaml.v2, using that tool to convert to/from json, and then using encode/json to marshal/unmarshal. The benefit would be using common marshal/unmarshal handlers and one set of struct tags. Meanwhile, you're not interacting with both yaml and json, so I suggest actually changing the tool you're using to support yaml directly. 1. Simple fix: replace `yaml:"<fieldname>"` with `json:"<fieldname>"` 2. Better fix that might break some code: use gopkg.in/yaml.v2 instead of github.com/ghodss/yaml, to avoid unexpected behavior, such as what you observed. On Sunday, April 10, 2022 at 11:53:57 PM UTC-5 vika...@gmail.com wrote: > I am new to GoLang and looking to join two keys (rules) from two different > Yamls. > > I have been able to join the keys but I found that Unmarshal changes the > keys (example apiVersion to APIVersion, and kind to Kind). > > Overall, my goal is to have a valid Kubernetes YAML manifest. This is what > I have attempted so far https://goplay.tools/snippet/zWYDjnTeA0Q > > ## > package main > > import ( > "fmt" > > "github.com/InVisionApp/conjungo" > "github.com/ghodss/yaml" > ) > > type ClusterRole struct { > APIVersion string `yaml:"apiVersion"` > Kind string `yaml:"kind"` > Metadata struct { > Name string `yaml:"name"` > } `yaml:"metadata"` > Rules []struct { > APIGroups []string `yaml:"apiGroups"` > Resources []string `yaml:"resources"` > Verbs []string `yaml:"verbs"` > } `yaml:"rules"` > } > > func main() { > > yaml1 := ` > --- > apiVersion: rbac.authorization.k8s.io/v1 > kind: ClusterRole > metadata: > name: role1 > rules: > - apiGroups: > - "" > resources: > - services > verbs: > - create > - delete > - apiGroups: > - apiregistration.k8s.io > resources: > - apiservices > verbs: > - create > - delete > ` > > yaml2 := ` > --- > apiVersion: rbac.authorization.k8s.io/v1 > kind: ClusterRole > metadata: > name: role2 > rules: > - apiGroups: > - "" > resources: > - secrets > - events.k8s.io > verbs: > - list > - watch > - apiGroups: > - metrics.k8s.io > resources: > - nodes > - bindings > verbs: > - get > ` > > var role1 ClusterRole > yaml.Unmarshal([]byte(yaml1), &role1) > fmt.Printf("\n## ROLE1\n %v", role1) > > var role2 ClusterRole > yaml.Unmarshal([]byte(yaml2), &role2) > fmt.Printf("\n\n## ROLE2\n %v", role2) > > conjungo.Merge(&role1, role2, nil) > > // Struct to Marshal > y, err := yaml.Marshal(role1) > if err != nil { > fmt.Printf("\nerr: %v\n", err) > return > } > fmt.Printf("\n\n## MERGED ROLE\n %v", string(y)) > } > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/12e554b9-9f5f-445c-b787-ab5ef48d3850n%40googlegroups.com.