-
Notifications
You must be signed in to change notification settings - Fork 13
/
scalar.go
115 lines (99 loc) · 2.7 KB
/
scalar.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
package opslevel
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"
)
type ID string
func NewID(id ...string) *ID {
var output ID
if len(id) == 1 {
output = ID(id[0])
}
return &output
}
func (id ID) GetGraphQLType() string { return "ID" }
func (id *ID) MarshalJSON() ([]byte, error) {
if *id == "" {
return []byte("null"), nil
}
return []byte(strconv.Quote(string(*id))), nil
}
type Identifier struct {
Id ID `graphql:"id" json:"id"`
Aliases []string `graphql:"aliases" json:"aliases"`
}
func (identifierInput IdentifierInput) MarshalJSON() ([]byte, error) {
if identifierInput.Id == nil && identifierInput.Alias == nil {
return []byte("null"), nil
}
var out string
if identifierInput.Id != nil {
out = fmt.Sprintf(`{"id":"%s"}`, string(*identifierInput.Id))
} else {
out = fmt.Sprintf(`{"alias":"%s"}`, *identifierInput.Alias)
}
return []byte(out), nil
}
func NewIdentifier(value ...string) *IdentifierInput {
if len(value) == 1 {
if IsID(value[0]) {
return &IdentifierInput{
Id: NewID(value[0]),
}
}
return &IdentifierInput{
Alias: RefOf(value[0]),
}
}
var output IdentifierInput
return &output
}
func NewIdentifierArray(values []string) []IdentifierInput {
output := make([]IdentifierInput, 0)
for _, value := range values {
output = append(output, *NewIdentifier(value))
}
return output
}
func IsID(value string) bool {
decoded, err := base64.RawURLEncoding.DecodeString(value)
if err != nil {
return false
}
return strings.HasPrefix(string(decoded), "gid://")
}
// NullableConstraint defines what types can be nullable - keep separated using the union operator (pipe)
type NullableConstraint interface {
any
}
// Nullable can be used to unset a value using an OpsLevel input struct type, should always be instantiated using a constructor.
type Nullable[T NullableConstraint] struct {
Value T
SetNull bool
}
func (nullable Nullable[T]) MarshalJSON() ([]byte, error) {
if nullable.SetNull {
return []byte("null"), nil
}
return json.Marshal(nullable.Value)
}
// NewNull returns a Nullable string that will always marshal into `null`, can be used to unset fields
func NewNull[T string]() *Nullable[T] {
return NewNullOf[T]()
}
// NewNullOf returns a Nullable of any type that fits NullableConstraint that will always marshal into `null`, can be used to unset fields
func NewNullOf[T NullableConstraint]() *Nullable[T] {
return &Nullable[T]{
SetNull: true,
}
}
// NewNullableFrom returns a Nullable that will never marshal into `null`, can be used to change fields or even set them to an empty value (like "")
func NewNullableFrom[T NullableConstraint](value T) *Nullable[T] {
return &Nullable[T]{
Value: value,
SetNull: false,
}
}