-
Notifications
You must be signed in to change notification settings - Fork 13
/
scalar.go
63 lines (53 loc) · 1.14 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
package opslevel
import (
"encoding/base64"
"strconv"
"strings"
)
type ID string
func NewID(id ...string) *ID {
var output ID
if len(id) == 1 {
output = ID(id[0])
}
return &output
}
func (s ID) GetGraphQLType() string { return "ID" }
func (s *ID) MarshalJSON() ([]byte, error) {
if *s == "" {
return []byte("null"), nil
}
return []byte(strconv.Quote(string(*s))), nil
}
type Identifier struct {
Id ID `graphql:"id"`
Aliases []string `graphql:"aliases"`
}
type IdentifierInput struct {
Id ID `graphql:"id" json:"id,omitempty"`
Alias string `graphql:"alias" json:"alias,omitempty"`
}
func NewIdentifier(value string) *IdentifierInput {
if IsID(value) {
return &IdentifierInput{
Id: ID(value),
}
}
return &IdentifierInput{
Alias: value,
}
}
func NewIdentifierArray(values []string) []IdentifierInput {
output := []IdentifierInput{}
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://")
}