-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathkolpa.go
121 lines (95 loc) · 2.5 KB
/
kolpa.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
/*
kolpa is a fake data generator written in and for Go.
Usage:
k := kolpa.C()
k.Name()
k.FirstName()
k.Address()
*/
package kolpa
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
// Generator struct to access various generator functions
type Generator struct {
Locale_ string
Pkg string
}
// Debugging purposes
var Print = fmt.Println
// C is the creator function, initiates kolpa with or without locale
// setting. The default locale setting is "en_US".
// Returns a generator type that will be used to call generator methods.
func C(localeVar ...string) Generator {
newGenerator := Generator{}
if len(localeVar) > 0 {
newGenerator.Locale_ = localeVar[0]
} else {
newGenerator.Locale_ = "en_US"
}
// newGenerator.populateFunctions()
newGenerator.Pkg = reflect.TypeOf(newGenerator).PkgPath()
return newGenerator
}
// SetLanguage is the language setter function. Language setting change be changed
// anytime by using this function.
func (g *Generator) SetLanguage(localeVar string) {
g.Locale_ = localeVar
}
// GenericGenerator is the generic function that powers all generations within kolpa.
// Recursively generates data for intended data type.
// Intended variable should be slug version of a valid data type.
func (g *Generator) GenericGenerator(intended string) string {
var result string
var slice []string
var err error
slice, err = g.fileToSlice(intended)
if err != nil {
slice, err = g.fileToSliceAll(intended)
}
if err != nil {
words := strings.Split(intended, "_")
intendedNew := strings.Join(words[:len(words)-1], "_")
slice, err = g.fileToSlice(intendedNew)
}
if err != nil {
return fmt.Sprint(err)
}
line := getRandom(slice)
search := regexp.MustCompile(`{{(.*?)}}`)
src := search.FindAllString(line, -1)
m := map[int]string{}
for c, s := range src {
splitted := s[2 : len(s)-2]
typeOfToken := g.typeOfToken(splitted)
if g.isParseable(line) {
switch typeOfToken {
case "func":
funcLine := strings.Split(splitted[1:len(splitted)-1], " ")
funcName := funcLine[0]
funcArgs := funcLine[1:]
result = funcMap[funcName](g, funcArgs)
case "same":
whichTokenInt := parseSame(splitted)
result = m[whichTokenInt]
default:
result = g.GenericGenerator(string(s[2 : len(s)-2]))
}
}
m[c] = result
}
r := g.nparser(line, m)
return r
}
func parseSame(splitted string) int {
sameLine := strings.Split(splitted, " ")
whichTokenInt, err := strconv.Atoi(sameLine[1])
if err != nil {
panic(err)
}
return whichTokenInt
}