blob: a73a38ef28685d9566bc84f3a251edb4525e5374 (
plain) (
blame)
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
|
package graphql
import (
"encoding/base64"
"strings"
)
type ResolvedGlobalID struct {
Type string `json:"type"`
ID string `json:"id"`
}
// Takes a type name and an ID specific to that type name, and returns a
// "global ID" that is unique among all types.
func ToGlobalID(ttype string, id string) string {
str := ttype + ":" + id
encStr := base64.StdEncoding.EncodeToString([]byte(str))
return encStr
}
// Takes the "global ID" created by toGlobalID, and returns the type name and ID
// used to create it.
func FromGlobalID(globalID string) *ResolvedGlobalID {
strID := ""
b, err := base64.StdEncoding.DecodeString(globalID)
if err == nil {
strID = string(b)
}
tokens := strings.Split(strID, ":")
if len(tokens) < 2 {
return nil
}
return &ResolvedGlobalID{
Type: tokens[0],
ID: tokens[1],
}
}
|