| 1 |
package scan |
| 2 |
|
| 3 |
import "go/ast" |
| 4 |
|
| 5 |
type fixtureCollector struct { |
| 6 |
candidates map[string]*fixtureInfo |
| 7 |
fixtures map[string]*fixtureInfo |
| 8 |
} |
| 9 |
|
| 10 |
func newFixtureCollector() *fixtureCollector { |
| 11 |
return &fixtureCollector{ |
| 12 |
candidates: make(map[string]*fixtureInfo), |
| 13 |
fixtures: make(map[string]*fixtureInfo), |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
func (this *fixtureCollector) Collect(file *ast.File) map[string]*fixtureInfo { |
| 18 |
ast.Walk(this, file) |
| 19 |
return this.fixtures |
| 20 |
} |
| 21 |
|
| 22 |
func (this *fixtureCollector) Visit(node ast.Node) ast.Visitor { |
| 23 |
switch t := node.(type) { |
| 24 |
case *ast.TypeSpec: |
| 25 |
name := t.Name.Name |
| 26 |
this.candidates[name] = &fixtureInfo{StructName: name} |
| 27 |
return &fixtureValidator{Parent: this, FixtureName: name} |
| 28 |
default: |
| 29 |
return this |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func (this *fixtureCollector) Validate(fixture string) { |
| 34 |
this.fixtures[fixture] = this.candidates[fixture] |
| 35 |
delete(this.candidates, fixture) |
| 36 |
} |
| 37 |
|
| 38 |
func (this *fixtureCollector) Invalidate(fixture string) { |
| 39 |
this.Validate(fixture) |
| 40 |
} |