From a46723334b0eb67c45140e6cc3fa587192a90577 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 15 Aug 2022 14:39:08 +0200 Subject: [PATCH] refactor: use switch-case not try-err (#1112) --- .../frontend/yaml/constraint/constraint.go | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/pipeline/frontend/yaml/constraint/constraint.go b/pipeline/frontend/yaml/constraint/constraint.go index b363112f3..b6e763ed5 100644 --- a/pipeline/frontend/yaml/constraint/constraint.go +++ b/pipeline/frontend/yaml/constraint/constraint.go @@ -97,32 +97,24 @@ func (when *When) IsLocal() bool { } func (when *When) UnmarshalYAML(value *yaml.Node) error { - unmarshelAsList := func() error { - lst := []Constraint{} - err := value.Decode(&lst) - if err != nil { + switch value.Kind { + case yaml.SequenceNode: + if err := value.Decode(&when.Constraints); err != nil { return err } - when.Constraints = lst - return nil - } - unmarshelAsDict := func() error { + case yaml.MappingNode: c := Constraint{} - err := value.Decode(&c) - if err != nil { + if err := value.Decode(&c); err != nil { return err } when.Constraints = append(when.Constraints, c) - return nil + + default: + return fmt.Errorf("not supported yaml kind: %v", value.Kind) } - err := unmarshelAsList() - if err != nil { - err = unmarshelAsDict() - } - - return err + return nil } // Match returns true if all constraints match the given input. If a single