-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
83 lines (64 loc) · 1.62 KB
/
schema.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
package main
import (
"fmt"
"strings"
)
type Schema struct {
name string
Fields []string
}
func NewSchema(name string) *Schema {
return &Schema{
name: name,
Fields: make([]string, 0),
}
}
func (s *Schema) addEntry(name, expr string) {
if name != "" && expr != "" {
s.Fields = append(s.Fields, fmt.Sprintf("%s: %s,", name, expr))
}
}
func (s *Schema) AddBoolField(name, yupExpression string) {
s.addEntry(name, yupExpression)
}
func (s *Schema) AddStringField(name, yupExpression string) {
s.addEntry(name, yupExpression)
}
func (s *Schema) AddNumberField(name, yupExpression string) {
s.addEntry(name, yupExpression)
}
func (s *Schema) AddTimeField(name, yupExpression string) {
s.addEntry(name, yupExpression)
}
func (s *Schema) AddMixedField(name, yupExpression string) {
s.addEntry(name, yupExpression)
}
func (s *Schema) Name() string {
return s.name + "Schema"
}
func (s *Schema) Filename(snakecase bool) string {
if snakecase {
return strings.ToLower(s.name + "_schema" + ".js")
}
return strings.ToUpper(string(s.Name()[0])) + s.Name()[1:] + ".js"
}
func (s *Schema) IsEmpty() bool {
return len(s.Fields) < 1
}
func (s *Schema) ToJS(m mapper) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("const %s = %s({\n", s.Name(), m.object()))
for _, expr := range s.Fields {
sb.WriteString(fmt.Sprintf("\t%s\n", expr))
}
sb.WriteString("})\n")
return sb.String()
}
func (s *Schema) ToJSFile(m mapper) string {
var sb strings.Builder
sb.WriteString(m.jsImport() + ";\n\n")
sb.WriteString(s.ToJS(m))
sb.WriteString("\nexport default ")
sb.WriteString(s.Name() + ";\n")
return sb.String()
}