github.com/drone/runner-go@v1.12.0/manifest/param.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package manifest
     6  
     7  type (
     8  	// Parameter represents an configuration parameter that
     9  	// can be defined as a literal or as a reference
    10  	// to a secret.
    11  	Parameter struct {
    12  		Value  interface{} `json:"value,omitempty"`
    13  		Secret string      `json:"from_secret,omitempty" yaml:"from_secret"`
    14  	}
    15  
    16  	// parameter is a tempoary type used to unmarshal
    17  	// parameters with references to secrets.
    18  	parameter struct {
    19  		Secret string `yaml:"from_secret"`
    20  	}
    21  )
    22  
    23  // UnmarshalYAML implements yaml unmarshalling.
    24  func (p *Parameter) UnmarshalYAML(unmarshal func(interface{}) error) error {
    25  	d := new(parameter)
    26  	err := unmarshal(d)
    27  	if err == nil && d.Secret != "" {
    28  		p.Secret = d.Secret
    29  		return nil
    30  	}
    31  	var i interface{}
    32  	err = unmarshal(&i)
    33  	p.Value = i
    34  	return err
    35  }
    36  
    37  // MarshalYAML implements yaml marshalling.
    38  func (p *Parameter) MarshalYAML() (interface{}, error) {
    39  	if p.Secret != "" {
    40  		m := map[string]interface{}{}
    41  		m["from_secret"] = p.Secret
    42  		return m, nil
    43  	}
    44  	if p.Value != "" {
    45  		return p.Value, nil
    46  	}
    47  	return nil, nil
    48  }