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.go
197 lines (175 loc) · 3.12 KB
/
schema_field.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package property
import (
"github.com/reearth/reearth-backend/pkg/i18n"
)
type SchemaField struct {
id FieldID
propertyType ValueType
title i18n.String
description i18n.String
prefix string
suffix string
defaultValue *Value
ui SchemaFieldUI
min *float64
max *float64
choices []SchemaFieldChoice
cond *Condition
}
type SchemaFieldChoice struct {
Key string
Title i18n.String
Icon string
}
func (p *SchemaField) ID() FieldID {
if p == nil {
return ""
}
return p.id
}
func (p *SchemaField) Type() ValueType {
if p == nil {
return ValueTypeUnknown
}
return p.propertyType
}
func (p *SchemaField) Title() i18n.String {
if p == nil {
return nil
}
return p.title.Clone()
}
func (p *SchemaField) Description() i18n.String {
if p == nil {
return nil
}
return p.description.Clone()
}
func (p *SchemaField) Prefix() string {
if p == nil {
return ""
}
return p.prefix
}
func (p *SchemaField) Suffix() string {
if p == nil {
return ""
}
return p.suffix
}
func (p *SchemaField) DefaultValue() *Value {
if p == nil {
return nil
}
return p.defaultValue.Clone()
}
func (p *SchemaField) UI() *SchemaFieldUI {
if p == nil || p.ui == "" {
return nil
}
ui := p.ui
return &ui
}
func (p *SchemaField) Min() *float64 {
if p == nil || p.min == nil {
return nil
}
min := *p.min
return &min
}
func (p *SchemaField) Max() *float64 {
if p == nil || p.max == nil {
return nil
}
max := *p.max
return &max
}
func (p *SchemaField) MinMax() (*float64, *float64) {
if p == nil {
return nil, nil
}
return p.Min(), p.Max()
}
func (p *SchemaField) Choices() []SchemaFieldChoice {
if p == nil {
return nil
}
if p.choices == nil {
return p.choices
}
return append([]SchemaFieldChoice{}, p.choices...)
}
func (p *SchemaField) Choice(key string) *SchemaFieldChoice {
if p == nil || p.choices == nil {
return nil
}
for _, c := range p.choices {
if c.Key == key {
return &c
}
}
return nil
}
func (p *SchemaField) IsAvailableIf() *Condition {
if p == nil {
return nil
}
return p.cond.Clone()
}
func (p *SchemaField) Validate(value *OptionalValue) bool {
if p == nil || value == nil || p.propertyType != value.Type() {
return false
}
switch v := value.Value().Value().(type) {
case float64:
if min := p.Min(); min != nil {
if v < *min {
return false
}
}
if max := p.Max(); max != nil {
if v > *max {
return false
}
}
case string:
if choices := p.Choices(); choices != nil {
ok := false
for _, k := range choices {
if k.Key == v {
ok = true
break
}
}
if !ok {
return false
}
}
}
return true
}
func (p *SchemaField) SetTitle(title i18n.String) {
if p == nil {
return
}
p.title = title.Clone()
}
func (p *SchemaField) SetDescription(des i18n.String) {
if p == nil {
return
}
p.description = des.Clone()
}
func (c *SchemaFieldChoice) SetTitle(l i18n.String) {
if c == nil {
return
}
c.Title = l.Clone()
}
func (c SchemaFieldChoice) Copy() SchemaFieldChoice {
return SchemaFieldChoice{
Icon: c.Icon,
Key: c.Key,
Title: c.Title.Clone(),
}
}