@@ -12,22 +12,25 @@ import (
12
12
13
13
// tagParser contains the data needed while parsing.
14
14
type tagParser struct {
15
- fset * token.FileSet
16
- tags []Tag // list of created tags
17
- types []string // all types we encounter, used to determine the constructors
18
- relative bool // should filenames be relative to basepath
19
- basepath string // output file directory
15
+ fset * token.FileSet
16
+ tags []Tag // list of created tags
17
+ types []string // all types we encounter, used to determine the constructors
18
+ relative bool // should filenames be relative to basepath
19
+ basepath string // output file directory
20
+ excludePrivate bool // exclude private symbols
20
21
}
21
22
22
- // Parse parses the source in filename and returns a list of tags. If relative
23
- // is true, the filenames in the list of tags are relative to basepath.
24
- func Parse (filename string , relative bool , basepath string ) ([]Tag , error ) {
23
+ // Parse parses the source in filename and returns a list of tags.
24
+ // If relative is true, the filenames in the list of tags are relative to basepath.
25
+ // If excludePrivate is true, any symbols that are not exported will be excluded.
26
+ func Parse (filename string , relative bool , basepath string , excludePrivate bool ) ([]Tag , error ) {
25
27
p := & tagParser {
26
- fset : token .NewFileSet (),
27
- tags : []Tag {},
28
- types : make ([]string , 0 ),
29
- relative : relative ,
30
- basepath : basepath ,
28
+ fset : token .NewFileSet (),
29
+ tags : []Tag {},
30
+ types : make ([]string , 0 ),
31
+ relative : relative ,
32
+ basepath : basepath ,
33
+ excludePrivate : excludePrivate ,
31
34
}
32
35
33
36
f , err := parser .ParseFile (p .fset , filename , nil , 0 )
@@ -90,6 +93,10 @@ func (p *tagParser) parseFunction(f *ast.FuncDecl) {
90
93
tag := p .createTag (f .Name .Name , f .Pos (), Function )
91
94
92
95
tag .Fields [Access ] = getAccess (tag .Name )
96
+ if p .excludeTag (tag ) {
97
+ return
98
+ }
99
+
93
100
tag .Fields [Signature ] = fmt .Sprintf ("(%s)" , getTypes (f .Type .Params , true ))
94
101
tag .Fields [TypeField ] = getTypes (f .Type .Results , false )
95
102
@@ -113,6 +120,9 @@ func (p *tagParser) parseTypeDeclaration(ts *ast.TypeSpec) {
113
120
tag := p .createTag (ts .Name .Name , ts .Pos (), Type )
114
121
115
122
tag .Fields [Access ] = getAccess (tag .Name )
123
+ if p .excludeTag (tag ) {
124
+ return
125
+ }
116
126
117
127
switch s := ts .Type .(type ) {
118
128
case * ast.StructType :
@@ -140,6 +150,9 @@ func (p *tagParser) parseValueDeclaration(v *ast.ValueSpec) {
140
150
141
151
tag := p .createTag (d .Name , d .Pos (), Variable )
142
152
tag .Fields [Access ] = getAccess (tag .Name )
153
+ if p .excludeTag (tag ) {
154
+ continue
155
+ }
143
156
144
157
if v .Type != nil {
145
158
tag .Fields [TypeField ] = getType (v .Type , true )
@@ -164,6 +177,9 @@ func (p *tagParser) parseStructFields(name string, s *ast.StructType) {
164
177
for _ , n := range f .Names {
165
178
tag = p .createTag (n .Name , n .Pos (), Field )
166
179
tag .Fields [Access ] = getAccess (tag .Name )
180
+ if p .excludeTag (tag ) {
181
+ continue
182
+ }
167
183
tag .Fields [ReceiverType ] = name
168
184
tag .Fields [TypeField ] = getType (f .Type , true )
169
185
p .tags = append (p .tags , tag )
@@ -172,6 +188,9 @@ func (p *tagParser) parseStructFields(name string, s *ast.StructType) {
172
188
// embedded field
173
189
tag = p .createTag (getType (f .Type , true ), f .Pos (), Embedded )
174
190
tag .Fields [Access ] = getAccess (tag .Name )
191
+ if p .excludeTag (tag ) {
192
+ continue
193
+ }
175
194
tag .Fields [ReceiverType ] = name
176
195
tag .Fields [TypeField ] = getType (f .Type , true )
177
196
p .tags = append (p .tags , tag )
@@ -192,6 +211,9 @@ func (p *tagParser) parseInterfaceMethods(name string, s *ast.InterfaceType) {
192
211
}
193
212
194
213
tag .Fields [Access ] = getAccess (tag .Name )
214
+ if p .excludeTag (tag ) {
215
+ continue
216
+ }
195
217
196
218
if t , ok := f .Type .(* ast.FuncType ); ok {
197
219
tag .Fields [Signature ] = fmt .Sprintf ("(%s)" , getTypes (t .Params , true ))
@@ -249,6 +271,14 @@ func (p *tagParser) belongsToReceiver(types *ast.FieldList) (name string, ok boo
249
271
return "" , false
250
272
}
251
273
274
+ // excludeTag returns true if tag symbol is not public and excludePrivate is set.
275
+ func (p * tagParser ) excludeTag (tag Tag ) bool {
276
+ if p .excludePrivate && tag .Fields [Access ] != "public" {
277
+ return true
278
+ }
279
+ return false
280
+ }
281
+
252
282
// getTypes returns a comma separated list of types in fields. If includeNames is
253
283
// true each type is preceded by a comma separated list of parameter names.
254
284
func getTypes (fields * ast.FieldList , includeNames bool ) string {
0 commit comments