-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.go
100 lines (81 loc) · 1.85 KB
/
common.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
package opslevel
import (
"fmt"
"strings"
"time"
"github.com/relvacode/iso8601"
)
type PageInfo struct {
HasNextPage bool `graphql:"hasNextPage"`
HasPreviousPage bool `graphql:"hasPreviousPage"`
Start string `graphql:"startCursor"`
End string `graphql:"endCursor"`
}
type PayloadVariables map[string]interface{}
type DeleteInput struct {
Id ID `json:"id"`
}
type OpsLevelErrors struct {
Message string
Path []string
}
type Timestamps struct {
CreatedAt iso8601.Time `json:"createdAt"`
UpdatedAt iso8601.Time `json:"updatedAt"`
}
func NullString() *string {
var output *string
return output
}
func NewString(value string) *string {
return &value
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool {
p := new(bool)
*p = v
return p
}
func HandleErrors(err error, errs []OpsLevelErrors) error {
if err != nil {
return err
}
return FormatErrors(errs)
}
func FormatErrors(errs []OpsLevelErrors) error {
if len(errs) == 0 {
return nil
}
var sb strings.Builder
sb.WriteString("OpsLevel API Errors:\n")
for _, err := range errs {
if len(err.Path) == 1 && err.Path[0] == "base" {
err.Path[0] = ""
}
sb.WriteString(fmt.Sprintf("\t- '%s' %s\n", strings.Join(err.Path, "."), err.Message))
}
return fmt.Errorf(sb.String())
}
func NewInt(i int) *int {
output := i
return &output
}
func NewISO8601Date(datetime string) iso8601.Time {
date, _ := iso8601.ParseString(datetime)
return iso8601.Time{Time: date}
}
func NewISO8601DateNow() iso8601.Time {
return iso8601.Time{Time: time.Now()}
}
func removeDuplicates(data []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range data {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}