Debian Codegraph @

tags.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found here: https://github.com/golang/go/blob/master/LICENSE
4
5 package goreq
6
7 import (
8 "strings"
9 "unicode"
10 )
11
12 // tagOptions is the string following a comma in a struct field's "json"
13 // tag, or the empty string. It does not include the leading comma.
14 type tagOptions string
15
16 // parseTag splits a struct field's json tag into its name and
17 // comma-separated options.
18 func parseTag(tag string) (string, tagOptions) {
19 if idx := strings.Index(tag, ","); idx != -1 {
20 return tag[:idx], tagOptions(tag[idx+1:])
21 }
22 return tag, tagOptions("")
23 }
24
25 // Contains reports whether a comma-separated list of options
26 // contains a particular substr flag. substr must be surrounded by a
27 // string boundary or commas.
28 func (o tagOptions) Contains(optionName string) bool {
29 if len(o) == 0 {
30 return false
31 }
32 s := string(o)
33 for s != "" {
34 var next string
35 i := strings.Index(s, ",")
36 if i >= 0 {
37 s, next = s[:i], s[i+1:]
38 }
39 if s == optionName {
40 return true
41 }
42 s = next
43 }
44 return false
45 }
46
47 func isValidTag(s string) bool {
48 if s == "" {
49 return false
50 }
51 for _, c := range s {
52 switch {
53 case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
54 // Backslash and quote chars are reserved, but
55 // otherwise any punctuation chars are allowed
56 // in a tag name.
57 default:
58 if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
59 return false
60 }
61 }
62 }
63 return true
64 }