-
Notifications
You must be signed in to change notification settings - Fork 99
/
utils.go
65 lines (54 loc) · 1.47 KB
/
utils.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package v1alpha1
import (
"encoding/json"
)
// NewInt32Pointer returns pointer on a new int32 value instance
func NewInt32Pointer(i int32) *int32 {
return &i
}
// NewInt64Pointer returns pointer on a new int32 value instance
func NewInt64Pointer(i int64) *int64 {
return &i
}
// NewStringPointer returns pointer on a new string value instance
func NewStringPointer(s string) *string {
return &s
}
// NewBoolPointer returns pointer on a new bool value instance
func NewBoolPointer(b bool) *bool {
return &b
}
// BoolValue return the boolean value, false if nil
func BoolValue(b *bool) bool {
if b == nil {
return false
}
return *b
}
// BoolToString return "true" if b == true, else "false"
func BoolToString(b *bool) string {
if BoolValue(b) {
return "true"
}
return "false"
}
// IsEqualStruct is a util fonction that returns whether 2 structures are the same
// We compare the marchaled results to avoid traversing all fields and be agnostic of the struct.
func IsEqualStruct(in interface{}, cmp interface{}) bool {
if in == nil {
return true
}
inJSON, err := json.Marshal(in)
if err != nil {
return false
}
cmpJSON, err := json.Marshal(cmp)
if err != nil {
return false
}
return string(inJSON) == string(cmpJSON)
}