-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_generic_methods.go
107 lines (97 loc) · 1.74 KB
/
client_generic_methods.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
package sfdc
import (
"encoding/json"
"fmt"
"strconv"
)
type ObjectResponse struct {
Data map[string]any
}
func (obj *ObjectResponse) GetString(key string) string {
gval, ok := obj.Data[key]
if !ok {
return ""
}
val := fmt.Sprint(gval)
if val == "<nil>" {
return ""
}
return val
}
func (obj *ObjectResponse) GetInt(key string) int {
gval, ok := obj.Data[key]
if !ok {
return 0
}
val, err := strconv.Atoi(fmt.Sprint(gval))
if err != nil {
return 0
}
return val
}
func (obj *ObjectResponse) GetFloat32(key string) float32 {
gval, ok := obj.Data[key]
if !ok {
return float32(0.0)
}
val, err := strconv.ParseFloat(fmt.Sprint(gval), 32)
if err != nil {
return float32(0.0)
}
return float32(val)
}
func (obj *ObjectResponse) GetFloat64(key string) float64 {
gval, ok := obj.Data[key]
if !ok {
return float64(0)
}
val, err := strconv.ParseFloat(fmt.Sprint(gval), 64)
if err != nil {
return float64(0)
}
return val
}
func (obj *ObjectResponse) GetBool(key string) bool {
gval, ok := obj.Data[key]
if !ok {
return false
}
val, ok := gval.(bool)
if !ok {
return false
}
return val
}
func (obj *ObjectResponse) GetSlice(key string) []any {
gval, ok := obj.Data[key]
if !ok {
return []any{}
}
val, ok := gval.([]any)
if !ok {
return []any{}
}
return val
}
func (obj *ObjectResponse) GetMap(key string) map[string]any {
gval, ok := obj.Data[key]
if !ok {
return map[string]any{}
}
val, ok := gval.(map[string]any)
if !ok {
return map[string]any{}
}
return val
}
func NewObjectResponse(data []byte) (*ObjectResponse, error) {
dataMap := make(map[string]any)
err := json.Unmarshal(data, &dataMap)
if err != nil {
return nil, err
}
obj := &ObjectResponse{
Data: dataMap,
}
return obj, nil
}