1package models
2
3import (
4 "bytes"
5 "fmt"
6 "io"
7 "strconv"
8)
9
10// GitRefType is the kind of git reference: a branch or a tag.
11type GitRefType string
12
13const (
14 // GitRefTypeBranch refers to a local branch (refs/heads/*).
15 GitRefTypeBranch GitRefType = "BRANCH"
16 // GitRefTypeTag refers to an annotated or lightweight tag (refs/tags/*).
17 GitRefTypeTag GitRefType = "TAG"
18)
19
20func (e GitRefType) IsValid() bool {
21 switch e {
22 case GitRefTypeBranch, GitRefTypeTag:
23 return true
24 }
25 return false
26}
27
28func (e GitRefType) String() string { return string(e) }
29
30func (e *GitRefType) UnmarshalGQL(v any) error {
31 str, ok := v.(string)
32 if !ok {
33 return fmt.Errorf("enums must be strings")
34 }
35 *e = GitRefType(str)
36 if !e.IsValid() {
37 return fmt.Errorf("%s is not a valid GitRefType", str)
38 }
39 return nil
40}
41
42func (e GitRefType) MarshalGQL(w io.Writer) {
43 fmt.Fprint(w, strconv.Quote(e.String()))
44}
45
46func (e *GitRefType) UnmarshalJSON(b []byte) error {
47 s, err := strconv.Unquote(string(b))
48 if err != nil {
49 return err
50 }
51 return e.UnmarshalGQL(s)
52}
53
54func (e GitRefType) MarshalJSON() ([]byte, error) {
55 var buf bytes.Buffer
56 e.MarshalGQL(&buf)
57 return buf.Bytes(), nil
58}