-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
119 lines (107 loc) · 2.49 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package configmap
import (
"encoding/json"
"log"
)
// AsJSON returns the ConfigMap as a Marshaled JSON string ([]byte)
func (c *Configmap) AsJSON() []byte {
result, err := json.Marshal(*c)
if err != nil {
log.Printf("error marshaling JSON: %s", err)
}
return result
}
func (c *Configmap) GetJSONNumber(key string) json.Number {
jsonNumber := json.Number("")
numberIntf := c.Get(key)
if numberIntf != nil {
switch number := numberIntf.(type) {
case json.Number:
jsonNumber = number
}
}
return jsonNumber
}
func (c *Configmap) GetJSONNumberOrNil(key string) *json.Number {
var jsonNumber *json.Number
numberIntf := c.Get(key)
if numberIntf != nil {
switch number := numberIntf.(type) {
case json.Number:
jsonNumber = &number
}
}
return jsonNumber
}
func (c *Configmap) GetJSONNumberAsInt64(key string) (numberAsInt int64) {
var err error
numberAsInt = int64(0)
jsonNumberIntf := c.Get(key)
if jsonNumberIntf != nil {
jsonNumber, ok := jsonNumberIntf.(json.Number)
if !ok {
return
}
numberAsInt, err = jsonNumber.Int64()
if err != nil {
log.Printf("error retrieving Int64 value of json.Number: %s", err.Error())
return
}
}
return
}
func (c *Configmap) GetJSONNumberAsFloat64(key string) (numberAsFloat float64) {
var err error
numberAsFloat = 0.0
jsonNumberIntf := c.Get(key)
if jsonNumberIntf != nil {
jsonNumber, ok := jsonNumberIntf.(json.Number)
if !ok {
return
}
numberAsFloat, err = jsonNumber.Float64()
if err != nil {
log.Printf("error retrieving Float64 value of json.Number: %s", err.Error())
return
}
}
return
}
func (c *Configmap) GetJSONNumberAsInt64OrNil(key string) (numberAsInt *int64) {
var err error
var rawNumber int64
numberAsInt = nil
jsonNumberIntf := c.Get(key)
if jsonNumberIntf != nil {
jsonNumber, ok := jsonNumberIntf.(json.Number)
if !ok {
return
}
rawNumber, err = jsonNumber.Int64()
if err != nil {
log.Printf("error retrieving Int64 value of json.Number: %s", err.Error())
return
}
numberAsInt = &rawNumber
}
return
}
func (c *Configmap) GetJSONNumberAsFloat64OrNil(key string) (numberAsFloat *float64) {
var err error
var rawNumber float64
numberAsFloat = nil
jsonNumberIntf := c.Get(key)
if jsonNumberIntf != nil {
jsonNumber, ok := jsonNumberIntf.(json.Number)
if !ok {
return
}
rawNumber, err = jsonNumber.Float64()
if err != nil {
log.Printf("error retrieving Float64 value of json.Number: %s", err.Error())
return
}
numberAsFloat = &rawNumber
}
return
}