/
commerce.go
123 lines (101 loc) · 2.8 KB
/
commerce.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
package faker
import (
"math"
"math/rand"
"reflect"
"strconv"
"strings"
)
type ProductName struct {
Product string `json:"product"`
Adjective string `json:"adjective"`
Material string `json:"material"`
}
type Commerce struct {
Color string `json:"color"`
Department string `json:"color"`
Price string `json:"color"`
ProductName ProductName `json:"product_name"`
}
func (commerce *Commerce) Colorʹ() string {
// get color
color := getData("Commerce", "Color")
// assign to commerce
commerce.Color = sample(color)
return commerce.Color
}
func (commerce *Commerce) Departmentʹ() string {
department := getData("Commerce", "Department")
commerce.Department = sample(department)
return commerce.Department
}
func (commerce *Commerce) Priceʹ(params ...interface{}) string {
// max int, dec float64, symbol string
min := 0
max := 1000
dec := float64(2)
var symbol string
kinds := kindOf(params...)
if len(kinds) >= 1 && kinds[0] == reflect.Int {
min = params[0].(int)
}
if len(kinds) >= 2 && kinds[1] == reflect.Int {
max = params[1].(int)
}
if len(kinds) >= 3 && kinds[2] == reflect.Float64 {
dec = params[2].(float64)
}
if len(kinds) >= 4 && kinds[3] == reflect.String {
symbol = params[3].(string)
}
if min < 0 || max < 0 {
return symbol + "0.00"
}
rnd := float64(rand.Intn(max))
pow := math.Pow(10, dec)
val := float64(round(rnd*pow)) / pow
result := strconv.FormatFloat(val, 'f', int(dec), 32)
return symbol + result
}
func (commerce *Commerce) ProductNameʹ() string {
separator := " "
productName := []string{
commerce.ProductAdjectiveʹ(),
commerce.ProductMaterialʹ(),
commerce.Productʹ(),
}
return strings.Join(productName, separator)
}
func (commerce *Commerce) ProductAdjectiveʹ() string {
adjective := getData("Commerce", "ProductName", "Adjective")
commerce.ProductName.Adjective = sample(adjective)
return commerce.ProductName.Adjective
}
func (commerce *Commerce) ProductMaterialʹ() string {
material := getData("Commerce", "ProductName", "Material")
commerce.ProductName.Material = sample(material)
return commerce.ProductName.Material
}
func (commerce *Commerce) Productʹ() string {
product := getData("Commerce", "ProductName", "Product")
commerce.ProductName.Product = sample(product)
return commerce.ProductName.Product
}
// Fake Generate random data for all field
func (commerce *Commerce) Fake() {
commerce.Colorʹ()
commerce.Departmentʹ()
commerce.Priceʹ()
commerce.ProductNameʹ()
commerce.ProductAdjectiveʹ()
commerce.ProductMaterialʹ()
commerce.Productʹ()
}
// ToJSON Encode name and its fields to JSON.
func (commerce *Commerce) ToJSON() (string, error) {
return ToJSON(commerce)
}
// ToXML Encode name and its fields to XML.
func (commerce *Commerce) ToXML() (string, error) {
return ToJSON(commerce)
}