blob: 0a3964a0df1ae879bcadf9e79de4efaf0090a5c2 (
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
40
41
42
43
44
45
46
47
|
package util
import (
"fmt"
"io"
)
// Hash is a git hash
type Hash string
func (h Hash) String() string {
return string(h)
}
// UnmarshalGQL implement the Unmarshaler interface for gqlgen
func (h *Hash) UnmarshalGQL(v interface{}) error {
_, ok := v.(string)
if !ok {
return fmt.Errorf("labels must be strings")
}
*h = v.(Hash)
if !h.IsValid() {
return fmt.Errorf("invalid hash")
}
return nil
}
// MarshalGQL implement the Marshaler interface for gqlgen
func (h Hash) MarshalGQL(w io.Writer) {
w.Write([]byte(`"` + h.String() + `"`))
}
// IsValid tell if the hash is valid
func (h *Hash) IsValid() bool {
if len(*h) != 40 {
return false
}
for _, r := range *h {
if (r < 'a' || r > 'z') && (r < '0' || r > '9') {
return false
}
}
return true
}
|