github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/travis/yaml/job.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 Jobs struct {
    20  	Include       []map[string]string `yaml:"include,omitempty"`
    21  	Exclude       []map[string]string `yaml:"exclude,omitempty"`
    22  	AllowFailures []map[string]string `yaml:"allow_failures,omitempty"`
    23  	FastFinish    bool                `yaml:"fast_finish,omitempty"`
    24  }
    25  
    26  // UnmarshalYAML implements the unmarshal interface.
    27  func (v *Jobs) UnmarshalYAML(unmarshal func(interface{}) error) error {
    28  	var out1 map[string]string
    29  	var out2 []map[string]string
    30  	var out3 = struct {
    31  		Include            []map[string]string `yaml:"include,omitempty"`
    32  		Exclude            []map[string]string `yaml:"exclude,omitempty"`
    33  		AllowFailures      []map[string]string `yaml:"allow_failures,omitempty"`
    34  		AllowFailuresAlias []map[string]string `yaml:"allowed_failures,omitempty"`
    35  		FastFinish         bool                `yaml:"fast_finish,omitempty"`
    36  		FastFinishAlias    bool                `yaml:"fast_failure,omitempty"`
    37  	}{}
    38  	if err := unmarshal(&out1); err == nil {
    39  		v.Include = append(v.Include, out1)
    40  		return nil
    41  	}
    42  	if err := unmarshal(&out2); err == nil {
    43  		v.Include = out2
    44  		return nil
    45  	}
    46  	if err := unmarshal(&out3); err == nil {
    47  		v.Include = out3.Include
    48  		v.Exclude = out3.Exclude
    49  		v.AllowFailures = out3.AllowFailures
    50  		v.FastFinish = out3.FastFinish
    51  		// map allowed_failures alias to allow_failures
    52  		v.AllowFailures = append(v.AllowFailures, out3.AllowFailuresAlias...)
    53  		// map fast_failure alias to fast_finish
    54  		if out3.FastFinishAlias {
    55  			v.FastFinish = true
    56  		}
    57  		return nil
    58  	}
    59  	return errors.New("failed to unmarshal jobs")
    60  }