github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/pkg/labels/component.go (about)

     1  package labels
     2  
     3  import (
     4  	"errors"
     5  
     6  	"gopkg.in/yaml.v3"
     7  )
     8  
     9  var _ Labels = (*Component)(nil)
    10  
    11  type Component struct {
    12  	model InternalComponent
    13  	base  *API
    14  }
    15  
    16  func ForComponent(l *API, component string) (*Component, error) {
    17  	if component == "" {
    18  		return nil, errors.New("component must not be nil")
    19  	}
    20  	return &Component{
    21  		base: l,
    22  		model: InternalComponent{
    23  			InternalComponentProp: InternalComponentProp{Component: component},
    24  			InternalAPI:           l.model,
    25  		},
    26  	}, nil
    27  }
    28  
    29  func (l *Component) UnmarshalYAML(node *yaml.Node) error {
    30  	if err := node.Decode(&l.model); err != nil {
    31  		return err
    32  	}
    33  	l.base = &API{}
    34  	return node.Decode(l.base)
    35  }
    36  
    37  func MustForComponent(l *API, component string) *Component {
    38  
    39  	c, err := ForComponent(l, component)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	return c
    44  }
    45  
    46  func MustReplaceComponent(l *Component, component string) *Component {
    47  	return MustForComponent(GetAPIFromComponent(l), component)
    48  }
    49  
    50  func GetAPIFromComponent(l *Component) *API {
    51  	return l.base
    52  }
    53  
    54  /*
    55  func (l *Component) Major() int8 {
    56  	return l.base.Major()
    57  }
    58  */
    59  func (l *Component) Equal(r comparable) bool {
    60  	if right, ok := r.(*Component); ok {
    61  		return l.model == right.model
    62  	}
    63  	return false
    64  }
    65  
    66  func (l *Component) MarshalYAML() (interface{}, error) {
    67  	return nil, errors.New("type *labels.Component is not serializable")
    68  }
    69  
    70  type InternalComponentProp struct {
    71  	Component string `yaml:"app.kubernetes.io/component"`
    72  }
    73  
    74  type InternalComponent struct {
    75  	InternalComponentProp `yaml:",inline"`
    76  	InternalAPI           `yaml:",inline"`
    77  }