LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file contains the test for canonical struct tags. | 5 // This file contains the test for canonical struct tags. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "go/ast" | 10 "go/ast" |
11 "reflect" | 11 "reflect" |
12 "strconv" | 12 "strconv" |
13 ) | 13 ) |
14 | 14 |
15 func init() { | 15 func init() { |
16 register("structtags", | 16 register("structtags", |
17 "check that struct field tags have canonical format and apply to
exported fields as needed", | 17 "check that struct field tags have canonical format and apply to
exported fields as needed", |
| 18 filterCanonicalFieldTag, |
18 checkCanonicalFieldTag, | 19 checkCanonicalFieldTag, |
19 field) | 20 field) |
20 } | 21 } |
| 22 |
| 23 func filterCanonicalFieldTag(f *File) bool { return true } |
21 | 24 |
22 // checkCanonicalFieldTag checks a struct field tag. | 25 // checkCanonicalFieldTag checks a struct field tag. |
23 func checkCanonicalFieldTag(f *File, node ast.Node) { | 26 func checkCanonicalFieldTag(f *File, node ast.Node) { |
24 field := node.(*ast.Field) | 27 field := node.(*ast.Field) |
25 if field.Tag == nil { | 28 if field.Tag == nil { |
26 return | 29 return |
27 } | 30 } |
28 | 31 |
29 tag, err := strconv.Unquote(field.Tag.Value) | 32 tag, err := strconv.Unquote(field.Tag.Value) |
30 if err != nil { | 33 if err != nil { |
(...skipping 22 matching lines...) Expand all Loading... |
53 return | 56 return |
54 } | 57 } |
55 | 58 |
56 for _, enc := range [...]string{"json", "xml"} { | 59 for _, enc := range [...]string{"json", "xml"} { |
57 if st.Get(enc) != "" { | 60 if st.Get(enc) != "" { |
58 f.Badf(field.Pos(), "struct field %s has %s tag but is n
ot exported", field.Names[0].Name, enc) | 61 f.Badf(field.Pos(), "struct field %s has %s tag but is n
ot exported", field.Names[0].Name, enc) |
59 return | 62 return |
60 } | 63 } |
61 } | 64 } |
62 } | 65 } |
LEFT | RIGHT |