-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
174 lines (138 loc) · 3.98 KB
/
parser.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Format:
// [entity] [entity_name] {
// [attribute_name] [attribute_type]
// }
// Tokenizer -> Parser -> Generator
package main
import (
"io"
"reflect"
"strings"
"sync"
"text/scanner"
"unicode"
"github.com/jaswdr/faker"
)
const RECORD_COUNT = 10
const IMAGE_METHOD_KEY = "Person.Image.Name.Image.Name"
type Attribute struct {
Name string
Type string
}
type Entity struct {
Name string
Attributes []Attribute
}
// TODO: should be refactored
func parseSource(src io.Reader) []Entity {
var s scanner.Scanner
s.Init(src)
s.Filename = "input"
s.Mode ^= scanner.SkipComments
// Allow dot in identifiers
s.IsIdentRune = func(ch rune, i int) bool {
return ch == '.' && i > 0 || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}
var entities []Entity
var currentEntity *Entity
var currentAttribute *Attribute
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
text := s.TokenText()
switch {
case text == "entity":
if currentEntity != nil {
entities = append(entities, *currentEntity)
}
currentEntity = &Entity{}
case text == "{":
// Start of an entity's attributes
case text == "}":
// End of an entity's attributes
if currentEntity != nil && currentAttribute != nil {
currentEntity.Attributes = append(currentEntity.Attributes, *currentAttribute)
currentAttribute = nil
}
case currentEntity != nil && currentEntity.Name == "":
currentEntity.Name = text
case currentAttribute == nil:
currentAttribute = &Attribute{Name: text}
case currentAttribute != nil && currentAttribute.Type == "":
currentAttribute.Type = text
typeParts := strings.Split(currentAttribute.Type, ".")
if len(typeParts) == 2 {
faker := faker.New()
fakerValue := reflect.ValueOf(faker)
fakerMethod := fakerValue.MethodByName(typeParts[0])
if !fakerMethod.IsValid() {
panic("Invalid type at attribute " + currentAttribute.Name + " for " + typeParts[0])
}
}
if currentEntity != nil {
currentEntity.Attributes = append(currentEntity.Attributes, *currentAttribute)
currentAttribute = nil
}
}
}
if currentEntity != nil {
entities = append(entities, *currentEntity)
}
return entities
}
type entityResult struct {
EntityName string
Data *[]interface{}
}
// TODO: Map the all available funcitons instead of using reflection. Reflection is performance killer.
func generateFake(entities []Entity) map[string]*[]interface{} {
resultList := make(map[string]*[]interface{})
fake := faker.New()
methodCache := make(map[string]reflect.Value)
fakerValue := reflect.ValueOf(fake)
var wg sync.WaitGroup
resultsChan := make(chan entityResult, len(entities))
for _, entity := range entities {
wg.Add(1)
go func(entity Entity) {
defer wg.Done()
lowercasedEntityName := strings.ToLower(entity.Name)
entityResults := make([]interface{}, 0, RECORD_COUNT)
for i := 0; i < RECORD_COUNT; i++ {
fakeValues := make(map[string]any)
for _, attr := range entity.Attributes {
methodKey := attr.Type
method, exists := methodCache[methodKey]
if !exists {
typeParts := strings.Split(attr.Type, ".")
method = fakerValue.MethodByName(typeParts[0])
for _, part := range typeParts[1:] {
method = method.Call([]reflect.Value{})[0].MethodByName(part)
methodKey += "." + part
}
methodCache[methodKey] = method
}
result := method.Call([]reflect.Value{})[0].Interface()
if methodKey == IMAGE_METHOD_KEY {
result = strings.Split(result.(string), "/")[2]
}
fakeValues[attr.Name] = result
}
entityResults = append(entityResults, fakeValues)
}
resultsChan <- entityResult{EntityName: lowercasedEntityName, Data: &entityResults}
}(entity)
}
go func() {
wg.Wait()
close(resultsChan)
}()
for result := range resultsChan {
resultList[result.EntityName] = result.Data
}
return resultList
}
func Generate(
src io.Reader,
) map[string]*[]interface{} {
entities := parseSource(src)
return generateFake(entities)
}