blob: ac5a926285d3abc068244b48488de0e10224d263 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package value
18
19import (
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.
27func NewValueInterface(v interface{}) Value {
28 return Value(HeapAllocator.allocValueUnstructured().reuse(v))
29}
30
31type valueUnstructured struct {
32 Value interface{}
33}
34
35// reuse replaces the value of the valueUnstructured.
36func (vi *valueUnstructured) reuse(value interface{}) Value {
37 vi.Value = value
38 return vi
39}
40
41func (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
51func (v valueUnstructured) AsMap() Map {
52 return v.AsMapUsing(HeapAllocator)
53}
54
55func (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
68func (v valueUnstructured) IsList() bool {
69 if v.Value == nil {
70 return false
71 }
72 _, ok := v.Value.([]interface{})
73 return ok
74}
75
76func (v valueUnstructured) AsList() List {
77 return v.AsListUsing(HeapAllocator)
78}
79
80func (v valueUnstructured) AsListUsing(_ Allocator) List {
81 return listUnstructured(v.Value.([]interface{}))
82}
83
84func (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
95func (v valueUnstructured) AsFloat() float64 {
96 if f, ok := v.Value.(float32); ok {
97 return float64(f)
98 }
99 return v.Value.(float64)
100}
101
102func (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
127func (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
148func (v valueUnstructured) IsString() bool {
149 if v.Value == nil {
150 return false
151 }
152 _, ok := v.Value.(string)
153 return ok
154}
155
156func (v valueUnstructured) AsString() string {
157 return v.Value.(string)
158}
159
160func (v valueUnstructured) IsBool() bool {
161 if v.Value == nil {
162 return false
163 }
164 _, ok := v.Value.(bool)
165 return ok
166}
167
168func (v valueUnstructured) AsBool() bool {
169 return v.Value.(bool)
170}
171
172func (v valueUnstructured) IsNull() bool {
173 return v.Value == nil
174}
175
176func (v valueUnstructured) Unstructured() interface{} {
177 return v.Value
178}