/
main.go
151 lines (116 loc) · 3.98 KB
/
main.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
// +build ignore
//this file generates the handlers/custom_keys.go file
//use go generate to update it
package main
import (
"fmt"
"html/template"
"log"
"os"
"reflect"
"sort"
"strconv"
"strings"
"github.com/bgadrian/fastfaker/faker"
)
func main() {
if len(os.Args) != 2 {
log.Panicf("requires one parameter, of ['go', 'markdown']")
}
type funct struct {
Key, Call, Example string
}
list := make([]funct, 0, 100)
fastFaker := faker.NewFastFaker()
fastFaker.Seed(42)
fakerType := reflect.TypeOf(fastFaker)
fakerValue := reflect.ValueOf(fastFaker)
countOfMethods := fakerType.NumMethod()
for i := 0; i < countOfMethods; i++ {
method := fakerType.Method(i)
//here we only collect simple functions, no parameters
//the first parameter is the actual struct (because is a method)
if method.Type.NumIn() > 1 {
continue
}
var example string
call := "f." + method.Name
g := byte('g')
res := fakerValue.Method(i).Call([]reflect.Value{})
if len(res) != 1 {
log.Panicf("weird method: %s", fakerValue.Method(i).String())
}
switch res[0].Interface().(type) {
case string:
example = res[0].String()
call = fmt.Sprintf("func(f *Faker) string { return %s() }", call)
case int:
example = strconv.Itoa(int(res[0].Int()))
call = fmt.Sprintf("func(f *Faker) string { return strconv.Itoa(%s()) }", call)
case int8, int16, int32, int64:
example = strconv.Itoa(int(res[0].Int()))
call = fmt.Sprintf("func(f *Faker) string { return strconv.Itoa(int(%s())) }", call)
case uint8, uint16, uint32, uint64:
example = strconv.Itoa(int(res[0].Uint()))
call = fmt.Sprintf("func(f *Faker) string { return strconv.Itoa(int(%s())) }", call)
case float32:
example = strconv.FormatFloat(float64(res[0].Float()), 'g', -1, 32)
call = fmt.Sprintf("func(f *Faker) string { return strconv.FormatFloat(float64(%s()), %d, -1, 32) }", call, g)
case float64:
example = strconv.FormatFloat(float64(res[0].Float()), 'g', -1, 64)
call = fmt.Sprintf("func(f *Faker) string { return strconv.FormatFloat(%s(), %d, -1, 64) }", call, g)
default:
continue
//example = fmt.Sprintf("%v", res[0])
//bytes, err := json.Marshal(res[0])
//if err != nil {
// log.Panic(err)
//}
//example = string(bytes)
}
list = append(list, funct{
Key: strings.ToLower(method.Name),
Call: call,
Example: strings.Replace(example, "\n", " ", -1),
})
}
sort.Slice(list, func(i, j int) bool {
return list[i].Key < list[j].Key
})
//TODO learn how to use the go generate
if os.Args[1] == "go" {
packageTemplate.Execute(os.Stdout, list)
} else {
readmeTemplate.Execute(os.Stdout, list)
}
}
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by make generate DO NOT EDIT.
package faker
import (
"strconv"
)
type fakerer func(*Faker) string
var templateVariables map[string]fakerer
func init() {
templateVariables = getTemplateVars()
}
func getTemplateVars() map[string]fakerer {
vars := make(map[string]fakerer, 100)
{{range .}}vars["{{.Key}}"] = {{.Call}}
{{end}}
return vars
}// Code generated by make generate DO NOT EDIT.
`))
var readmeTemplate = template.Must(template.New("").Parse(`# FastFaker Templates
This document contains all the available variables, with examples that can be used with the Faker.Template, Faker.TemplateCustom and Faker.Struct methods.
A variable is an alias to the public Faker method with the same name, for example:
* **{name}** will be replaced by the result from Faker.Name() method
* **{hackernoun}** will be replaced by the result from Faker.HackerNoun() method
> Note: the Faker.Template() function has 2 more special characters: '#' and '?', they will be replaced by a digit or an ASCII lowercase letter. If you do want this functionality use Faker.TemplateCustom().
For more info see [TEMPLATES](./TEMPLATES.md).
| Variable | Example |
|---------|--------|
{{range .}}| **{{{.Key}}}** | {{.Example}} |
{{end}}
> This file is generated automatically at build.
`))