-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
68 lines (61 loc) · 1.47 KB
/
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
60
61
62
63
64
65
66
67
68
package csvlib
import (
"fmt"
"reflect"
"strings"
)
type tagDetail struct {
name string
prefix string
ignored bool
empty bool
omitEmpty bool
optional bool
inline bool
}
func parseTag(tagName string, field reflect.StructField) (*tagDetail, error) {
tagValue, ok := field.Tag.Lookup(tagName)
if !ok {
return nil, nil
}
tag := &tagDetail{}
tags := strings.Split(tagValue, ",")
if len(tags) == 1 && tags[0] == "" {
tag.name = field.Name
tag.empty = true
} else {
switch tags[0] {
case "-":
tag.ignored = true
case "":
tag.name = field.Name
default:
tag.name = tags[0]
}
for _, tagOpt := range tags[1:] {
switch {
case tagOpt == "optional":
tag.optional = true
case tagOpt == "omitempty":
tag.omitEmpty = true
case tagOpt == "inline":
tag.inline = true
case strings.HasPrefix(tagOpt, "prefix="):
tag.prefix = tagOpt[len("prefix="):]
}
}
}
// Validation: struct field unexported
if !tag.ignored && !field.IsExported() {
return nil, fmt.Errorf("%w: struct field %s unexported", ErrTagOptionInvalid, field.Name)
}
// Validation: only inline column can have prefix
if tag.prefix != "" && !tag.inline {
return nil, fmt.Errorf("%w: prefix tag is only accepted for inline column", ErrTagOptionInvalid)
}
// Validation: inline column must not be optional
if tag.inline && tag.optional {
return nil, fmt.Errorf("%w: inline column must not be optional", ErrTagOptionInvalid)
}
return tag, nil
}