-
Notifications
You must be signed in to change notification settings - Fork 4
/
components.go
72 lines (62 loc) · 1.26 KB
/
components.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package models
import (
"database/sql/driver"
"encoding/json"
"strings"
)
type Component struct {
Name string
Group string
}
func (c *Component) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
func (c *Component) UnmarshalJSON(bytes []byte) error {
var s string
err := json.Unmarshal(bytes, &s)
if err != nil {
return err
}
split := strings.SplitN(s, " - ", 2)
if len(split) == 1 {
c.Name = split[0]
return nil
}
c.Group = split[0]
c.Name = split[1]
return nil
}
func (c Component) String() string {
if c.Group == "" {
return c.Name
}
return c.Group + " - " + c.Name
}
type Components []Component
func (c Components) Value() (driver.Value, error) {
valueString, err := json.Marshal(c)
return string(valueString), err
}
func (c *Components) Scan(src interface{}) error {
if err := json.Unmarshal([]byte(src.(string)), c); err != nil {
return err
}
return nil
}
func (c Components) String() string {
components := make([]string, len(c))
for i, co := range c {
components[i] = co.String()
}
return strings.Join(components, ", ")
}
func (c *Components) Inline() []string {
if c == nil {
return []string{}
}
components := make([]string, len(*c))
for i, co := range *c {
components[i] = co.String()
}
return components
}