github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/import.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 Imports struct { 20 Items []*Import 21 } 22 23 // UnmarshalYAML implements the unmarshal interface. 24 func (v *Imports) UnmarshalYAML(unmarshal func(interface{}) error) error { 25 var out1 *Import 26 var out2 []*Import 27 if err := unmarshal(&out1); err == nil { 28 v.Items = append(v.Items, out1) 29 return nil 30 } 31 if err := unmarshal(&out2); err == nil { 32 v.Items = out2 33 return nil 34 } 35 return errors.New("failed to unmarshal imports") 36 } 37 38 // MarshalYAML implements the marshal interface. 39 func (v *Imports) MarshalYAML() (interface{}, error) { 40 return v.Items, nil 41 } 42 43 type Import struct { 44 Source string `yaml:"source,omitempty"` 45 Mode string `yaml:"mode,omitempty"` // merge, deep_merge, deep_merge_append, deep_merge_prepend 46 If string `yaml:"if,omitempty"` 47 } 48 49 // UnmarshalYAML implements the unmarshal interface. 50 func (v *Import) UnmarshalYAML(unmarshal func(interface{}) error) error { 51 var out1 string 52 var out2 = struct { 53 Source string `yaml:"source"` 54 Mode string `yaml:"mode"` 55 If string `yaml:"if"` 56 }{} 57 if err := unmarshal(&out1); err == nil { 58 v.Source = out1 59 return nil 60 } 61 if err := unmarshal(&out2); err == nil { 62 v.Source = out2.Source 63 v.Mode = out2.Mode 64 v.If = out2.If 65 return nil 66 } 67 return errors.New("failed to unmarshal import") 68 }