refactor: use switch-case not try-err (#1112)

This commit is contained in:
6543
2022-08-15 14:39:08 +02:00
committed by GitHub
parent 5a945c10e9
commit a46723334b

View File

@@ -97,34 +97,26 @@ func (when *When) IsLocal() bool {
} }
func (when *When) UnmarshalYAML(value *yaml.Node) error { func (when *When) UnmarshalYAML(value *yaml.Node) error {
unmarshelAsList := func() error { switch value.Kind {
lst := []Constraint{} case yaml.SequenceNode:
err := value.Decode(&lst) if err := value.Decode(&when.Constraints); err != nil {
if err != nil {
return err return err
} }
when.Constraints = lst
return nil
}
unmarshelAsDict := func() error { case yaml.MappingNode:
c := Constraint{} c := Constraint{}
err := value.Decode(&c) if err := value.Decode(&c); err != nil {
if err != nil {
return err return err
} }
when.Constraints = append(when.Constraints, c) when.Constraints = append(when.Constraints, c)
default:
return fmt.Errorf("not supported yaml kind: %v", value.Kind)
}
return nil return nil
} }
err := unmarshelAsList()
if err != nil {
err = unmarshelAsDict()
}
return err
}
// Match returns true if all constraints match the given input. If a single // Match returns true if all constraints match the given input. If a single
// constraint fails a false value is returned. // constraint fails a false value is returned.
func (c *Constraint) Match(metadata frontend.Metadata) bool { func (c *Constraint) Match(metadata frontend.Metadata) bool {