label.go

 1package bug
 2
 3import (
 4	"fmt"
 5	"io"
 6	"strings"
 7
 8	"github.com/MichaelMure/git-bug/util/text"
 9)
10
11type Label string
12
13func (l Label) String() string {
14	return string(l)
15}
16
17// UnmarshalGQL implements the graphql.Unmarshaler interface
18func (l *Label) UnmarshalGQL(v interface{}) error {
19	_, ok := v.(string)
20	if !ok {
21		return fmt.Errorf("labels must be strings")
22	}
23
24	*l = v.(Label)
25
26	return nil
27}
28
29// MarshalGQL implements the graphql.Marshaler interface
30func (l Label) MarshalGQL(w io.Writer) {
31	_, _ = w.Write([]byte(`"` + l.String() + `"`))
32}
33
34func (l Label) Validate() error {
35	str := string(l)
36
37	if text.Empty(str) {
38		return fmt.Errorf("empty")
39	}
40
41	if strings.Contains(str, "\n") {
42		return fmt.Errorf("should be a single line")
43	}
44
45	if !text.Safe(str) {
46		return fmt.Errorf("not fully printable")
47	}
48
49	return nil
50}