Using composition vs embedding in yaml model structs

Viewed 34

I have the following yaml file

apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  name: myapp
  namespace: mynamespace

And using the following construct to unmarshal it

type hr struct {
    APIVersion string `yaml:"apiVersion"`
    Kind       string `yaml:"kind"`
    Metadata   `yaml:"metadata"`
}

type Metadata struct {
    Name      string `yaml:"name"`
    Namespace string `yaml:"namespace"`
}

My question is whether there is a practical difference in using embedding vs composision in the hr struct, i.e instead if declaring it as above, providing a name in the Metadata field, as in

type hr struct {
    APIVersion string   `yaml:"apiVersion"`
    Kind       string   `yaml:"kind"`
    MD         Metadata `yaml:"metadata"`
}

Is any of the two approaches preferred over the other when it comes to yaml unmarshalling?

(both methods seem to be working fine btw in the actual unmarshalling)

0 Answers
Related