@@ -12,22 +12,25 @@ import (
1212
1313// tagParser contains the data needed while parsing.
1414type 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
2021}
2122
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 ) {
2527 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 ,
3134 }
3235
3336 f , err := parser .ParseFile (p .fset , filename , nil , 0 )
@@ -90,6 +93,10 @@ func (p *tagParser) parseFunction(f *ast.FuncDecl) {
9093 tag := p .createTag (f .Name .Name , f .Pos (), Function )
9194
9295 tag .Fields [Access ] = getAccess (tag .Name )
96+ if p .excludeTag (tag ) {
97+ return
98+ }
99+
93100 tag .Fields [Signature ] = fmt .Sprintf ("(%s)" , getTypes (f .Type .Params , true ))
94101 tag .Fields [TypeField ] = getTypes (f .Type .Results , false )
95102
@@ -113,6 +120,9 @@ func (p *tagParser) parseTypeDeclaration(ts *ast.TypeSpec) {
113120 tag := p .createTag (ts .Name .Name , ts .Pos (), Type )
114121
115122 tag .Fields [Access ] = getAccess (tag .Name )
123+ if p .excludeTag (tag ) {
124+ return
125+ }
116126
117127 switch s := ts .Type .(type ) {
118128 case * ast.StructType :
@@ -140,6 +150,9 @@ func (p *tagParser) parseValueDeclaration(v *ast.ValueSpec) {
140150
141151 tag := p .createTag (d .Name , d .Pos (), Variable )
142152 tag .Fields [Access ] = getAccess (tag .Name )
153+ if p .excludeTag (tag ) {
154+ continue
155+ }
143156
144157 if v .Type != nil {
145158 tag .Fields [TypeField ] = getType (v .Type , true )
@@ -164,6 +177,9 @@ func (p *tagParser) parseStructFields(name string, s *ast.StructType) {
164177 for _ , n := range f .Names {
165178 tag = p .createTag (n .Name , n .Pos (), Field )
166179 tag .Fields [Access ] = getAccess (tag .Name )
180+ if p .excludeTag (tag ) {
181+ continue
182+ }
167183 tag .Fields [ReceiverType ] = name
168184 tag .Fields [TypeField ] = getType (f .Type , true )
169185 p .tags = append (p .tags , tag )
@@ -172,6 +188,9 @@ func (p *tagParser) parseStructFields(name string, s *ast.StructType) {
172188 // embedded field
173189 tag = p .createTag (getType (f .Type , true ), f .Pos (), Embedded )
174190 tag .Fields [Access ] = getAccess (tag .Name )
191+ if p .excludeTag (tag ) {
192+ continue
193+ }
175194 tag .Fields [ReceiverType ] = name
176195 tag .Fields [TypeField ] = getType (f .Type , true )
177196 p .tags = append (p .tags , tag )
@@ -192,6 +211,9 @@ func (p *tagParser) parseInterfaceMethods(name string, s *ast.InterfaceType) {
192211 }
193212
194213 tag .Fields [Access ] = getAccess (tag .Name )
214+ if p .excludeTag (tag ) {
215+ continue
216+ }
195217
196218 if t , ok := f .Type .(* ast.FuncType ); ok {
197219 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
249271 return "" , false
250272}
251273
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+
252282// getTypes returns a comma separated list of types in fields. If includeNames is
253283// true each type is preceded by a comma separated list of parameter names.
254284func getTypes (fields * ast.FieldList , includeNames bool ) string {
0 commit comments