Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame^] | 1 | package structs |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // tagOptions contains a slice of tag options |
| 6 | type tagOptions []string |
| 7 | |
| 8 | // Has returns true if the given option is available in tagOptions |
| 9 | func (t tagOptions) Has(opt string) bool { |
| 10 | for _, tagOpt := range t { |
| 11 | if tagOpt == opt { |
| 12 | return true |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | return false |
| 17 | } |
| 18 | |
| 19 | // parseTag splits a struct field's tag into its name and a list of options |
| 20 | // which comes after a name. A tag is in the form of: "name,option1,option2". |
| 21 | // The name can be neglectected. |
| 22 | func parseTag(tag string) (string, tagOptions) { |
| 23 | // tag is one of followings: |
| 24 | // "" |
| 25 | // "name" |
| 26 | // "name,opt" |
| 27 | // "name,opt,opt2" |
| 28 | // ",opt" |
| 29 | |
| 30 | res := strings.Split(tag, ",") |
| 31 | return res[0], res[1:] |
| 32 | } |