aboutsummaryrefslogtreecommitdiffstats
path: root/bug/label.go
blob: b73222ddb86117724e39c0e4c8e9075e2f0ede01 (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
48
49
50
package bug

import (
	"fmt"
	"io"
	"strings"

	"github.com/MichaelMure/git-bug/util/text"
)

type Label string

func (l Label) String() string {
	return string(l)
}

// UnmarshalGQL implements the graphql.Unmarshaler interface
func (l *Label) UnmarshalGQL(v interface{}) error {
	_, ok := v.(string)
	if !ok {
		return fmt.Errorf("labels must be strings")
	}

	*l = v.(Label)

	return nil
}

// MarshalGQL implements the graphql.Marshaler interface
func (l Label) MarshalGQL(w io.Writer) {
	w.Write([]byte(`"` + l.String() + `"`))
}

func (l Label) Validate() error {
	str := string(l)

	if text.Empty(str) {
		return fmt.Errorf("empty")
	}

	if strings.Contains(str, "\n") {
		return fmt.Errorf("should be a single line")
	}

	if !text.Safe(str) {
		return fmt.Errorf("not fully printable")
	}

	return nil
}