...

Source file src/sigs.k8s.io/structured-merge-diff/v4/value/valueunstructured.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/value

     1  /*
     2  Copyright 2018 The Kubernetes 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 value
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // NewValueInterface creates a Value backed by an "interface{}" type,
    24  // typically an unstructured object in Kubernetes world.
    25  // interface{} must be one of: map[string]interface{}, map[interface{}]interface{}, []interface{}, int types, float types,
    26  // string or boolean. Nested interface{} must also be one of these types.
    27  func NewValueInterface(v interface{}) Value {
    28  	return Value(HeapAllocator.allocValueUnstructured().reuse(v))
    29  }
    30  
    31  type valueUnstructured struct {
    32  	Value interface{}
    33  }
    34  
    35  // reuse replaces the value of the valueUnstructured.
    36  func (vi *valueUnstructured) reuse(value interface{}) Value {
    37  	vi.Value = value
    38  	return vi
    39  }
    40  
    41  func (v valueUnstructured) IsMap() bool {
    42  	if _, ok := v.Value.(map[string]interface{}); ok {
    43  		return true
    44  	}
    45  	if _, ok := v.Value.(map[interface{}]interface{}); ok {
    46  		return true
    47  	}
    48  	return false
    49  }
    50  
    51  func (v valueUnstructured) AsMap() Map {
    52  	return v.AsMapUsing(HeapAllocator)
    53  }
    54  
    55  func (v valueUnstructured) AsMapUsing(_ Allocator) Map {
    56  	if v.Value == nil {
    57  		panic("invalid nil")
    58  	}
    59  	switch t := v.Value.(type) {
    60  	case map[string]interface{}:
    61  		return mapUnstructuredString(t)
    62  	case map[interface{}]interface{}:
    63  		return mapUnstructuredInterface(t)
    64  	}
    65  	panic(fmt.Errorf("not a map: %#v", v))
    66  }
    67  
    68  func (v valueUnstructured) IsList() bool {
    69  	if v.Value == nil {
    70  		return false
    71  	}
    72  	_, ok := v.Value.([]interface{})
    73  	return ok
    74  }
    75  
    76  func (v valueUnstructured) AsList() List {
    77  	return v.AsListUsing(HeapAllocator)
    78  }
    79  
    80  func (v valueUnstructured) AsListUsing(_ Allocator) List {
    81  	return listUnstructured(v.Value.([]interface{}))
    82  }
    83  
    84  func (v valueUnstructured) IsFloat() bool {
    85  	if v.Value == nil {
    86  		return false
    87  	} else if _, ok := v.Value.(float64); ok {
    88  		return true
    89  	} else if _, ok := v.Value.(float32); ok {
    90  		return true
    91  	}
    92  	return false
    93  }
    94  
    95  func (v valueUnstructured) AsFloat() float64 {
    96  	if f, ok := v.Value.(float32); ok {
    97  		return float64(f)
    98  	}
    99  	return v.Value.(float64)
   100  }
   101  
   102  func (v valueUnstructured) IsInt() bool {
   103  	if v.Value == nil {
   104  		return false
   105  	} else if _, ok := v.Value.(int); ok {
   106  		return true
   107  	} else if _, ok := v.Value.(int8); ok {
   108  		return true
   109  	} else if _, ok := v.Value.(int16); ok {
   110  		return true
   111  	} else if _, ok := v.Value.(int32); ok {
   112  		return true
   113  	} else if _, ok := v.Value.(int64); ok {
   114  		return true
   115  	} else if _, ok := v.Value.(uint); ok {
   116  		return true
   117  	} else if _, ok := v.Value.(uint8); ok {
   118  		return true
   119  	} else if _, ok := v.Value.(uint16); ok {
   120  		return true
   121  	} else if _, ok := v.Value.(uint32); ok {
   122  		return true
   123  	}
   124  	return false
   125  }
   126  
   127  func (v valueUnstructured) AsInt() int64 {
   128  	if i, ok := v.Value.(int); ok {
   129  		return int64(i)
   130  	} else if i, ok := v.Value.(int8); ok {
   131  		return int64(i)
   132  	} else if i, ok := v.Value.(int16); ok {
   133  		return int64(i)
   134  	} else if i, ok := v.Value.(int32); ok {
   135  		return int64(i)
   136  	} else if i, ok := v.Value.(uint); ok {
   137  		return int64(i)
   138  	} else if i, ok := v.Value.(uint8); ok {
   139  		return int64(i)
   140  	} else if i, ok := v.Value.(uint16); ok {
   141  		return int64(i)
   142  	} else if i, ok := v.Value.(uint32); ok {
   143  		return int64(i)
   144  	}
   145  	return v.Value.(int64)
   146  }
   147  
   148  func (v valueUnstructured) IsString() bool {
   149  	if v.Value == nil {
   150  		return false
   151  	}
   152  	_, ok := v.Value.(string)
   153  	return ok
   154  }
   155  
   156  func (v valueUnstructured) AsString() string {
   157  	return v.Value.(string)
   158  }
   159  
   160  func (v valueUnstructured) IsBool() bool {
   161  	if v.Value == nil {
   162  		return false
   163  	}
   164  	_, ok := v.Value.(bool)
   165  	return ok
   166  }
   167  
   168  func (v valueUnstructured) AsBool() bool {
   169  	return v.Value.(bool)
   170  }
   171  
   172  func (v valueUnstructured) IsNull() bool {
   173  	return v.Value == nil
   174  }
   175  
   176  func (v valueUnstructured) Unstructured() interface{} {
   177  	return v.Value
   178  }
   179  

View as plain text