github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/artifacts.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  	Artifacts struct {
    21  		Download *bool
    22  		Paths    []string
    23  	}
    24  
    25  	// temporary data structure for unmarshaling and
    26  	// marshaling artifacts.
    27  	artifacts struct {
    28  		Download *bool    `yaml:"download,omitempty"`
    29  		Paths    []string `yaml:"paths,omitempty"`
    30  	}
    31  )
    32  
    33  // MarshalYAML implements the marshal interface.
    34  func (v *Artifacts) MarshalYAML() (interface{}, error) {
    35  	if len(v.Paths) == 0 && v.Download == nil {
    36  		return nil, nil
    37  	} else if v.Download == nil {
    38  		// emit the short syntax when the download
    39  		// value is true (default)
    40  		return v.Paths, nil
    41  	} else {
    42  		// emit the short syntax when the download
    43  		// value is false (non-default)
    44  		return &artifacts{
    45  			Download: v.Download,
    46  			Paths:    v.Paths,
    47  		}, nil
    48  	}
    49  }
    50  
    51  // UnmarshalYAML implements the unmarshal interface.
    52  func (v *Artifacts) UnmarshalYAML(unmarshal func(interface{}) error) error {
    53  	var out1 string
    54  	var out2 []string
    55  	var out3 *artifacts
    56  	if err := unmarshal(&out1); err == nil {
    57  		v.Paths = append(v.Paths, out1)
    58  		return nil
    59  	}
    60  	if err := unmarshal(&out2); err == nil {
    61  		v.Paths = append(v.Paths, out2...)
    62  		return nil
    63  	}
    64  	if err := unmarshal(&out3); err == nil {
    65  		v.Download = out3.Download
    66  		v.Paths = append(v.Paths, out3.Paths...)
    67  		return nil
    68  	}
    69  	return errors.New("failed to unmarshal artifacts")
    70  }