github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/util/int_string.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strconv"
    23  
    24  	"gopkg.in/yaml.v3"
    25  )
    26  
    27  type IntOrString struct {
    28  	Type   Type
    29  	IntVal int
    30  	StrVal string
    31  }
    32  
    33  type Type int
    34  
    35  const (
    36  	Int    Type = iota // The IntOrString holds an int.
    37  	String             // The IntOrString holds a string.
    38  )
    39  
    40  // FromInt creates an IntOrString object with an int32 value.
    41  func FromInt(val int) IntOrString {
    42  	return IntOrString{Type: Int, IntVal: val}
    43  }
    44  
    45  // FromString creates an IntOrString object with a string value.
    46  func FromString(val string) IntOrString {
    47  	return IntOrString{Type: String, StrVal: val}
    48  }
    49  
    50  // String returns the string value, or the Itoa of the int value.
    51  func (t *IntOrString) String() string {
    52  	if t.Type == String {
    53  		return t.StrVal
    54  	}
    55  	return strconv.Itoa(t.IntVal)
    56  }
    57  
    58  // UnmarshalYAML implements the yaml.Unmarshaler interface.
    59  func (t *IntOrString) UnmarshalYAML(node *yaml.Node) error {
    60  	val := node.Value
    61  	i, err := strconv.Atoi(val)
    62  	if err != nil {
    63  		*t = FromString(val)
    64  	} else {
    65  		*t = FromInt(i)
    66  	}
    67  	return nil
    68  }
    69  
    70  // MarshalYAML implements the yaml.Marshaler interface.
    71  func (t IntOrString) MarshalYAML() (interface{}, error) {
    72  	switch t.Type {
    73  	case Int:
    74  		return t.IntVal, nil
    75  	case String:
    76  		return t.StrVal, nil
    77  	default:
    78  		return nil, fmt.Errorf("impossible IntOrString.Type")
    79  	}
    80  }
    81  
    82  // UnmarshalJSON implements the json.Unmarshaler interface.
    83  func (t *IntOrString) UnmarshalJSON(value []byte) error {
    84  	if value[0] == '"' {
    85  		t.Type = String
    86  		return json.Unmarshal(value, &t.StrVal)
    87  	}
    88  	t.Type = Int
    89  	return json.Unmarshal(value, &t.IntVal)
    90  }
    91  
    92  // MarshalJSON implements the json.Marshaler interface.
    93  func (t IntOrString) MarshalJSON() ([]byte, error) {
    94  	switch t.Type {
    95  	case Int:
    96  		return json.Marshal(t.IntVal)
    97  	case String:
    98  		return json.Marshal(t.StrVal)
    99  	default:
   100  		return []byte{}, fmt.Errorf("impossible IntOrString.Type")
   101  	}
   102  }