-
Notifications
You must be signed in to change notification settings - Fork 4
/
random.go
78 lines (73 loc) · 2.05 KB
/
random.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
package gomodel
import (
"fmt"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/enum"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
"projectforge.dev/projectforge/app/util"
)
const nilKey = "nil"
func modelRandom(m *model.Model, enums enum.Enums) *golang.Block {
ret := golang.NewBlock(m.Proper()+"Random", "struct")
ret.W("func Random() *%s {", m.Proper())
ret.W("\treturn &%s{", m.Proper())
maxColLength := m.Columns.MaxCamelLength() + 1
for _, col := range m.Columns {
ret.W("\t\t%s %s,", util.StringPad(col.Proper()+":", maxColLength), randFor(col, m.PackageWithGroup(""), enums))
}
ret.W("\t}")
ret.W("}")
return ret
}
func randFor(col *model.Column, pkg string, enums enum.Enums) string {
switch col.Type.Key() {
case types.KeyAny:
return types.KeyNil
case types.KeyBool:
return "util.RandomBool()"
case types.KeyEnum:
et, err := model.AsEnumInstance(col.Type, enums)
if err != nil {
return "ERROR:" + err.Error()
}
if pkg == et.PackageWithGroup("") {
return fmt.Sprintf("%s(util.RandomString(12))", et.Proper())
}
return fmt.Sprintf("%s.%s(util.RandomString(12))", et.Package, et.Proper())
case types.KeyInt:
return "util.RandomInt(10000)"
case types.KeyFloat:
return "util.RandomFloat(1000)"
case types.KeyList:
return nilKey
case types.KeyMap, types.KeyValueMap:
return "util.RandomValueMap(4)"
case types.KeyReference:
return nilKey
case types.KeyString:
switch col.Format {
case model.FmtHTML:
return "\"<h3>\" + util.RandomString(6) + \"</h3>\""
case model.FmtURL:
return "\"https://\" + util.RandomString(6) + \".com/\" + util.RandomString(6)"
default:
return "util.RandomString(12)"
}
case types.KeyDate, types.KeyTimestamp:
if col.HasTag("deleted") {
return types.KeyNil
}
if col.Nullable {
return "util.NowPointer()"
}
return "time.Now()"
case types.KeyUUID:
if col.Nullable {
return "util.UUIDP()"
}
return "util.UUID()"
default:
return "TODO"
}
}