github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/gitlab/yaml/rules.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import "errors" 18 19 type Rule struct { 20 If string `yaml:"if,omitempty"` 21 Changes Change `yaml:"changes,omitempty"` 22 Exists []string `yaml:"exists,omitempty"` 23 AllowFailures bool `yaml:"allow_failure,omitempty"` 24 Variables map[string]string `yaml:"variables,omitempty"` 25 When string `yaml:"when,omitempty"` 26 Needs []string `yaml:"needs,omitempty"` 27 } 28 29 type Change struct { 30 Paths []string `yaml:"paths"` 31 CompareTo string `yaml:"compare_to,omitempty"` 32 } 33 34 func (v *Change) UnmarshalYAML(unmarshal func(interface{}) error) error { 35 var out1 []string 36 var out2 string 37 var out3 = struct { 38 Paths []string `yaml:"paths"` 39 CompareTo string `yaml:"compare_to,omitempty"` 40 }{} 41 42 if err := unmarshal(&out1); err == nil { 43 v.Paths = out1 44 return nil 45 } 46 47 if err := unmarshal(&out2); err == nil { 48 v.Paths = []string{out2} 49 return nil 50 } 51 52 if err := unmarshal(&out3); err == nil { 53 v.Paths = out3.Paths 54 v.CompareTo = out3.CompareTo 55 return nil 56 } 57 58 return errors.New("failed to unmarshal rules:changes") 59 } 60 61 func (v *Change) MarshalYAML() (interface{}, error) { 62 if v.CompareTo != "" { 63 return map[string]interface{}{ 64 "paths": v.Paths, 65 "compare_to": v.CompareTo, 66 }, nil 67 } 68 return v.Paths, nil 69 }