-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtag.go
81 lines (70 loc) · 2.17 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
69
70
71
72
73
74
75
76
77
78
79
80
81
package questdb
import (
"fmt"
"strings"
)
const tagName = "qdb"
// ensureOptionsAreValid func will take a option tags []string and check and make sure
// each one being set is valid. If not, it will return an error.
func ensureOptionsAreValid(opts []string) error {
for _, v := range opts {
vSplit := strings.Split(v, ":")
if len(vSplit) != 2 {
return fmt.Errorf("'%s' is not valid option", v)
}
// possibly check against valid options keys and values in opts []string in future?
}
return nil
}
// getOption func will take a slice of strings (tag options) and a string representing an option
// thats trying to be extracted and will attempt to find and return that options set value.
// If that option is not set in the struct field, it will return an empty string ("").
func getOption(opts []string, option string) string {
for _, v := range opts {
vSplit := strings.Split(v, ":")
optName := vSplit[0]
optVal := vSplit[1]
if option == optName {
return optVal
}
}
return ""
}
// tagOptions struct represents options set by the tag of a specific struct field.
type tagOptions struct {
embeddedPrefix string
designatedTS bool
commitZeroValue bool
index bool
}
// makeTagOptions func takes a tagOpts []string and returns a tagOptions struct
func makeTagOptions(f *field, tagsOpts []string) (tagOptions, error) {
opts := tagOptions{}
// embeddedPrefix
embeddedPrefix := getOption(tagsOpts, "embeddedPrefix")
if embeddedPrefix != "" {
opts.embeddedPrefix = embeddedPrefix
}
// designated ts fields
isDesignatedTSField := getOption(tagsOpts, "designatedTS")
if isDesignatedTSField == "true" {
if f.qdbType != Timestamp {
return opts, fmt.Errorf("type must be timestamp if 'designatedTS:true' option set")
}
opts.designatedTS = true
}
// commit zero values
commitZeroValueField := getOption(tagsOpts, "commitZeroValue")
if commitZeroValueField == "true" {
opts.commitZeroValue = true
}
// index field
indexField := getOption(tagsOpts, "index")
if indexField == "true" {
if f.qdbType != Symbol {
return opts, fmt.Errorf("type must be symbol not %s in order to index field", f.qdbType)
}
opts.index = true
}
return opts, nil
}