-
Notifications
You must be signed in to change notification settings - Fork 1
/
scalar.go
61 lines (50 loc) · 1.11 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
package shopifygraphql
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
)
/* ---------------------- */
/* ID */
/* ---------------------- */
var shopifyGIDFormat = "gid://shopify/%s/%d"
// ID retrieve id
func ID(gid string) int {
// Split gid with /
substrings := strings.Split(gid, "/")
// Get id string
idStr := substrings[len(substrings)-1]
if i := strings.Index(idStr, "?"); i > 0 {
idStr = idStr[0:i]
}
// Convert str id to int
id, _ := strconv.Atoi(idStr)
return id
}
// GID retrieve id
func GID(resource string, id int) string {
return fmt.Sprintf(shopifyGIDFormat, resource, id)
}
// EncodeGID base64 encode gid
func EncodeGID(gid string) string {
return base64.URLEncoding.EncodeToString([]byte(gid))
}
// EncodedGID base64 encoded gid
func EncodedGID(resource string, id int) string {
return EncodeGID(GID(resource, id))
}
/* ------------------------ */
/* Type */
/* ------------------------ */
// Bool bool
func Bool(v bool) *bool {
return &v
}
// BoolValue bool value
func BoolValue(v *bool) bool {
if v != nil {
return *v
}
return false
}