-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_ref.go
73 lines (64 loc) · 1.66 KB
/
type_ref.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
package ghid
import (
"fmt"
"strconv"
"github.com/vmihailenco/msgpack/v5"
)
func init() {
RegisterDecodeV1(TypeRef, func(key []byte) (KeyV1, error) {
repo, rest, err := decodeV1IDAndRest(TypeRef, key)
if err != nil {
return nil, err
}
return RefKey{RepoID: RepoID(repo), RefName: string(rest)}, nil
})
RegisterDecodeV2(TypeRef, func(key msgpack.RawMessage) (KeyV2, error) {
var arr []any
err := msgpack.Unmarshal(key, &arr)
if err != nil {
return nil, err
}
if len(arr) != 3 {
return nil, fmt.Errorf("unsupported IDv2 ref key: %#v", arr)
}
if v, ok := asUint64(arr[0]); !ok || v != 0 {
return nil, fmt.Errorf("unsupported IDv2 ref key: %#v", arr)
}
repo, ok := asUint64(arr[1])
if !ok {
return nil, fmt.Errorf("unsupported IDv2 ref key: %#v", arr)
}
ref, ok := arr[2].(string)
if !ok {
return nil, fmt.Errorf("unsupported IDv2 ref key: %#v", arr)
}
return RefKey{RepoID: RepoID(repo), RefName: ref}, nil
})
}
var (
_ KeyV1 = RefKey{}
_ KeyV2 = RefKey{}
)
// RefKey is a unique key for Ref nodes.
//
// See https://docs.github.com/en/graphql/reference/objects#ref.
type RefKey struct {
RepoID RepoID // corresponds to repository.databaseId
RefName string // corresponds to prefix+name
}
// Type implements Key.
func (r RefKey) Type() string {
return TypeRef
}
// GetRepoID implements KeyWithRepo.
func (r RefKey) GetRepoID() RepoID {
return r.RepoID
}
// KeyV1 implements KeyV1.
func (r RefKey) KeyV1() string {
return strconv.FormatUint(uint64(r.RepoID), 10) + ":" + r.RefName
}
// KeyV2 implements KeyV2.
func (r RefKey) KeyV2() msgpack.RawMessage {
return mustEncodeV2([]any{uint(0), uint(r.RepoID), r.RefName})
}