github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/yaml/machine.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 (
    20  	Machine struct {
    21  		Default            bool   `yaml:"default,omitempty"`
    22  		Image              string `yaml:"image,omitempty"`
    23  		Shell              string `yaml:"shell,omitempty"`
    24  		DockerLayerCaching bool   `yaml:"docker_layer_caching,omitempty"`
    25  	}
    26  
    27  	machine struct {
    28  		Image              string `yaml:"image,omitempty"`
    29  		Shell              string `yaml:"shell,omitempty"`
    30  		DockerLayerCaching bool   `yaml:"docker_layer_caching,omitempty"`
    31  	}
    32  )
    33  
    34  // UnmarshalYAML implements the unmarshal interface.
    35  func (v *Machine) UnmarshalYAML(unmarshal func(interface{}) error) error {
    36  	var out1 bool
    37  	var out2 *machine
    38  
    39  	if err := unmarshal(&out1); err == nil {
    40  		v.Default = true
    41  		return nil
    42  	}
    43  
    44  	if err := unmarshal(&out2); err == nil {
    45  		v.Image = out2.Image
    46  		v.Shell = out2.Shell
    47  		v.DockerLayerCaching = out2.DockerLayerCaching
    48  		return nil
    49  	}
    50  
    51  	return errors.New("failed to unmarshal machine")
    52  }
    53  
    54  // MarshalYAML implements the marshal interface.
    55  func (v *Machine) MarshalYAML() (interface{}, error) {
    56  	if v.Default {
    57  		return true, nil
    58  	}
    59  	return &machine{
    60  		Image:              v.Image,
    61  		Shell:              v.Shell,
    62  		DockerLayerCaching: v.DockerLayerCaching,
    63  	}, nil
    64  }