github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/stage.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  // Stages defines a set of build stages. Build stages are run
    20  // sequentially. Stages run their Jobs in parallel.
    21  //
    22  // https://config.travis-ci.com/ref/stages
    23  type Stages struct {
    24  	Items []*Stage
    25  }
    26  
    27  // UnmarshalYAML implements the unmarshal interface.
    28  func (v *Stages) UnmarshalYAML(unmarshal func(interface{}) error) error {
    29  	var out1 *Stage
    30  	var out2 []*Stage
    31  	if err := unmarshal(&out1); err == nil {
    32  		v.Items = append(v.Items, out1)
    33  		return nil
    34  	}
    35  	if err := unmarshal(&out2); err == nil {
    36  		v.Items = out2
    37  		return nil
    38  	}
    39  	return errors.New("failed to unmarshal stages")
    40  }
    41  
    42  // MarshalYAML implements the marshal interface.
    43  func (v *Stages) MarshalYAML() (interface{}, error) {
    44  	return v.Items, nil
    45  }
    46  
    47  // Stage defines a buld stage.
    48  type Stage struct {
    49  	Name string `yaml:"name,omitempty"`
    50  	If   string `yaml:"if,omitempty"`
    51  }
    52  
    53  // UnmarshalYAML implements the unmarshal interface.
    54  func (v *Stage) UnmarshalYAML(unmarshal func(interface{}) error) error {
    55  	var out1 string
    56  	var out2 = struct {
    57  		Name string `yaml:"name"`
    58  		If   string `yaml:"if"`
    59  	}{}
    60  	if err := unmarshal(&out1); err == nil {
    61  		v.Name = out1
    62  		return nil
    63  	}
    64  	if err := unmarshal(&out2); err == nil {
    65  		v.Name = out2.Name
    66  		v.If = out2.If
    67  		return nil
    68  	}
    69  	return errors.New("failed to unmarshal stage")
    70  }