This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
schema_field_builder.go
141 lines (120 loc) · 2.87 KB
/
schema_field_builder.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
package property
import (
"errors"
"github.com/reearth/reearth-backend/pkg/i18n"
)
type SchemaFieldBuilder struct {
p *SchemaField
}
func NewSchemaField() *SchemaFieldBuilder {
return &SchemaFieldBuilder{p: &SchemaField{}}
}
func (b *SchemaFieldBuilder) Build() (*SchemaField, error) {
if b.p.id.String() == "" || b.p.id.String() == "id" {
return nil, ErrInvalidID
}
if b.p.ui != SchemaFieldUI("") && SchemaFieldUIFrom(string(b.p.ui)) == SchemaFieldUI("") {
return nil, errors.New("invalid property schema field ui")
}
if b.p.min != nil && b.p.max != nil && *b.p.min > *b.p.max {
return nil, errors.New("invalid min and max")
}
if ok := b.p.propertyType.Valid(); !ok {
return nil, errors.New("invalid value type")
}
return b.p, nil
}
func (b *SchemaFieldBuilder) MustBuild() *SchemaField {
p, err := b.Build()
if err != nil {
panic(err)
}
return p
}
func (b *SchemaFieldBuilder) ID(id FieldID) *SchemaFieldBuilder {
b.p.id = id
return b
}
func (b *SchemaFieldBuilder) Type(propertyType ValueType) *SchemaFieldBuilder {
b.p.propertyType = propertyType
return b
}
func (b *SchemaFieldBuilder) Name(name i18n.String) *SchemaFieldBuilder {
b.p.title = name.Clone()
return b
}
func (b *SchemaFieldBuilder) Description(description i18n.String) *SchemaFieldBuilder {
b.p.description = description.Clone()
return b
}
func (b *SchemaFieldBuilder) Prefix(prefix string) *SchemaFieldBuilder {
b.p.prefix = prefix
return b
}
func (b *SchemaFieldBuilder) Suffix(suffix string) *SchemaFieldBuilder {
b.p.suffix = suffix
return b
}
func (b *SchemaFieldBuilder) DefaultValue(v *Value) *SchemaFieldBuilder {
if v == nil {
b.p.defaultValue = nil
} else {
b.p.defaultValue = v.Clone()
}
return b
}
func (b *SchemaFieldBuilder) UI(ui SchemaFieldUI) *SchemaFieldBuilder {
b.p.ui = ui
return b
}
func (b *SchemaFieldBuilder) UIRef(ui *SchemaFieldUI) *SchemaFieldBuilder {
if ui == nil {
b.p.ui = SchemaFieldUI("")
} else {
b.p.ui = *ui
}
return b
}
func (b *SchemaFieldBuilder) Min(min float64) *SchemaFieldBuilder {
m := min
b.p.min = &m
return b
}
func (b *SchemaFieldBuilder) Max(max float64) *SchemaFieldBuilder {
m := max
b.p.max = &m
return b
}
func (b *SchemaFieldBuilder) MinRef(min *float64) *SchemaFieldBuilder {
if min == nil {
b.p.min = nil
} else {
m := *min
b.p.min = &m
}
return b
}
func (b *SchemaFieldBuilder) MaxRef(max *float64) *SchemaFieldBuilder {
if max == nil {
b.p.max = nil
} else {
m := *max
b.p.max = &m
}
return b
}
func (b *SchemaFieldBuilder) Choices(choices []SchemaFieldChoice) *SchemaFieldBuilder {
if choices == nil {
b.p.choices = nil
} else {
b.p.choices = make([]SchemaFieldChoice, 0, len(choices))
for _, c := range choices {
b.p.choices = append(b.p.choices, c.Copy())
}
}
return b
}
func (b *SchemaFieldBuilder) IsAvailableIf(cond *Condition) *SchemaFieldBuilder {
b.p.cond = cond.Clone()
return b
}