github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/addons_snaps.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 Snaps struct { 20 Items []*Snap 21 } 22 23 // UnmarshalYAML implements the unmarshal interface. 24 func (v *Snaps) UnmarshalYAML(unmarshal func(interface{}) error) error { 25 var out1 *Snap 26 var out2 []*Snap 27 28 if err := unmarshal(&out1); err == nil { 29 v.Items = append(v.Items, out1) 30 return nil 31 } 32 33 if err := unmarshal(&out2); err == nil { 34 v.Items = append(v.Items, out2...) 35 return nil 36 } 37 38 return errors.New("failed to unmarshal snaps") 39 } 40 41 // MarshalYAML implements the marshal interface. 42 func (v *Snaps) MarshalYAML() (interface{}, error) { 43 return v.Items, nil 44 } 45 46 type Snap struct { 47 Name string `yaml:"name,omitempty"` 48 Classic bool `yaml:"classic,omitempty"` 49 Channel string `yaml:"channel,omitempty"` 50 } 51 52 // UnmarshalYAML implements the unmarshal interface. 53 func (v *Snap) UnmarshalYAML(unmarshal func(interface{}) error) error { 54 var out1 string 55 var out2 = struct { 56 Name string `yaml:"name,omitempty"` 57 Classic bool `yaml:"classic,omitempty"` 58 Channel string `yaml:"channel,omitempty"` 59 }{} 60 61 if err := unmarshal(&out1); err == nil { 62 v.Name = out1 63 return nil 64 } 65 66 if err := unmarshal(&out2); err == nil { 67 *v = out2 68 return nil 69 } 70 71 return errors.New("failed to unmarshal snap") 72 }