2018-07-12 22:31:11 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-27 20:48:45 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-09-15 14:15:00 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2018-07-27 20:48:45 +03:00
|
|
|
)
|
|
|
|
|
2018-07-12 22:31:11 +03:00
|
|
|
type Label string
|
2018-07-18 17:41:09 +03:00
|
|
|
|
|
|
|
func (l Label) String() string {
|
|
|
|
return string(l)
|
|
|
|
}
|
2018-07-27 20:48:45 +03:00
|
|
|
|
2018-08-03 00:37:49 +03:00
|
|
|
// UnmarshalGQL implements the graphql.Unmarshaler interface
|
2018-07-27 20:48:45 +03:00
|
|
|
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) {
|
2018-07-29 21:56:03 +03:00
|
|
|
w.Write([]byte(`"` + l.String() + `"`))
|
2018-07-27 20:48:45 +03:00
|
|
|
}
|
2018-09-15 14:15:00 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|