FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gunk/loader/validate_struct_tags.go at master · gunk/gunk · GitHub
gunk
/
gunk
Public
Notifications
You must be signed in to change notification settings
Fork
34
Star
341
Code
Issues
15
Pull requests
2
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
gunk
/
loader
/
validate_struct_tags.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (85 loc) · 2.4 KB
Breadcrumbs
gunk
/
loader
/
validate_struct_tags.go
Copy path
File metadata and controls
96 lines (85 loc) · 2.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package
loader
import
(
"errors"
"fmt"
"strconv"
"strings"
)
// copied from go vet source code
// https://github.com/golang/tools/blob/master/go/analysis/passes/structtag/structtag.go
// with added check that only json and pb allowed
var
(
errTagSyntax
=
errors
.
New
(
"bad syntax for struct tag pair"
)
errTagKeySyntax
=
errors
.
New
(
"bad syntax for struct tag key"
)
errTagValueSyntax
=
errors
.
New
(
"bad syntax for struct tag value"
)
errTagValueComma
=
errors
.
New
(
"JSON tags cannot use comma"
)
errTagSpace
=
errors
.
New
(
"key:
\"
value
\"
pairs not separated by spaces"
)
)
// validateStructTag parses the struct tag and returns an error if it is not
// in the canonical format, which is a space-separated list of key:"value"
// settings. The value may contain spaces.
func
validateStructTag
(
tag
string
)
error
{
// This code is based on the StructTag.Get code in package reflect.
n
:=
0
for
;
tag
!=
""
;
n
++
{
if
n
>
0
&&
tag
!=
""
&&
tag
[
0
]
!=
' '
{
// More restrictive than reflect, but catches likely mistakes
// like `x:"foo",y:"bar"`, which parses as `x:"foo" ,y:"bar"` with second key ",y".
return
errTagSpace
}
// Skip leading space.
i
:=
0
for
i
<
len
(
tag
)
&&
tag
[
i
]
==
' '
{
i
++
}
tag
=
tag
[
i
:]
if
tag
==
""
{
break
}
// Scan to colon. A space, a quote or a control character is a syntax error.
// Strictly speaking, control chars include the range [0x7f, 0x9f], not just
// [0x00, 0x1f], but in practice, we ignore the multi-byte control characters
// as it is simpler to inspect the tag's bytes than the tag's runes.
i
=
0
for
i
<
len
(
tag
)
&&
tag
[
i
]
>
' '
&&
tag
[
i
]
!=
':'
&&
tag
[
i
]
!=
'"'
&&
tag
[
i
]
!=
0x7f
{
i
++
}
if
i
==
0
{
return
errTagKeySyntax
}
if
i
+
1
>=
len
(
tag
)
||
tag
[
i
]
!=
':'
{
return
errTagSyntax
}
if
tag
[
i
+
1
]
!=
'"'
{
return
errTagValueSyntax
}
key
:=
tag
[:
i
]
if
!
(
key
==
"pb"
||
key
==
"json"
) {
return
fmt
.
Errorf
(
"tag %q not allowed, only
\"
pb
\"
and
\"
json
\"
"
,
key
)
}
tag
=
tag
[
i
+
1
:]
// Scan quoted string to find value.
i
=
1
for
i
<
len
(
tag
)
&&
tag
[
i
]
!=
'"'
{
if
tag
[
i
]
==
'\\'
{
i
++
}
i
++
}
if
i
>=
len
(
tag
) {
return
errTagValueSyntax
}
qvalue
:=
tag
[:
i
+
1
]
tag
=
tag
[
i
+
1
:]
value
,
err
:=
strconv
.
Unquote
(
qvalue
)
if
err
!=
nil
{
return
errTagValueSyntax
}
if
key
==
"json"
{
if
strings
.
IndexByte
(
value
,
','
)
>=
0
{
return
errTagValueComma
}
}
}
return
nil
}
Back
|
FazBrowse Home
|
New Git URL