github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/bitbucket/yaml/size.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  // List of machine sizes.
    18  type Size int
    19  
    20  // Size enumeration.
    21  const (
    22  	SizeNone Size = iota
    23  	Size1x
    24  	Size2x
    25  	Size4x
    26  	Size8x
    27  )
    28  
    29  // String returns the Size as a string.
    30  func (e Size) String() string {
    31  	switch e {
    32  	case Size1x:
    33  		return "1x"
    34  	case Size2x:
    35  		return "2x"
    36  	case Size4x:
    37  		return "4x"
    38  	case Size8x:
    39  		return "8x"
    40  	default:
    41  		return ""
    42  	}
    43  }
    44  
    45  // UnmarshalYAML implements the unmarshal interface.
    46  func (e *Size) UnmarshalYAML(unmarshal func(interface{}) error) error {
    47  	var v string
    48  	unmarshal(&v)
    49  	switch v {
    50  	case "1x":
    51  		*e = Size1x
    52  	case "2x":
    53  		*e = Size2x
    54  	case "4x":
    55  		*e = Size4x
    56  	case "8x":
    57  		*e = Size8x
    58  	default:
    59  		*e = SizeNone
    60  	}
    61  	return nil
    62  }
    63  
    64  // MarshalJSON marshals the enum as a quoted json string
    65  func (e Size) MarshalYAML() (interface{}, error) {
    66  	if e == SizeNone {
    67  		return nil, nil
    68  	} else {
    69  		return e.String(), nil
    70  	}
    71  }