-
Notifications
You must be signed in to change notification settings - Fork 11
/
tag.go
59 lines (46 loc) · 1005 Bytes
/
tag.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
package content
import (
"database/sql/driver"
"errors"
"fmt"
)
type TagID int64
type TagValue string
type Tag struct {
ID TagID `json:"id"`
Value TagValue `json:"value"`
}
func (t Tag) Validate() error {
if t.Value == "" {
return NewValidationError(errors.New("Tag has no value"))
}
return nil
}
func (t Tag) String() string {
return string(t.Value)
}
func (id *TagID) Scan(src interface{}) error {
asInt, ok := src.(int64)
if !ok {
return fmt.Errorf("Scan source '%#v' (%T) was not of type int64 (TagId)", src, src)
}
*id = TagID(asInt)
return nil
}
func (id TagID) Value() (driver.Value, error) {
return int64(id), nil
}
func (val *TagValue) Scan(src interface{}) error {
switch t := src.(type) {
case string:
*val = TagValue(t)
case []byte:
*val = TagValue(t)
default:
return fmt.Errorf("Scan source '%#v' (%T) was not of type string (TagValue)", src, src)
}
return nil
}
func (val TagValue) Value() (driver.Value, error) {
return string(val), nil
}