-
Notifications
You must be signed in to change notification settings - Fork 1
/
number.go
118 lines (103 loc) · 2.28 KB
/
number.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
package types
import (
"encoding/json"
"github.com/go-jstmpl/go-jstmpl/helpers"
"github.com/go-jstmpl/go-jstmpl/validations"
"github.com/lestrrat/go-jsschema"
)
type Number struct {
*schema.Schema
NativeType string
ColumnName string
ColumnType string
Type string
Name string
key string
Private bool
Validations []validations.Validation
}
func NewNumber(ctx *Context, s *schema.Schema) *Number {
vs := []validations.Validation{}
if v, err := validations.NewNumberEnumValidation(s); err == nil {
ctx.AddValidation(v)
vs = append(vs, v)
}
if v, err := validations.NewMaximumValidation(s); err == nil {
ctx.AddValidation(v)
vs = append(vs, v)
}
if v, err := validations.NewMinimumValidation(s); err == nil {
ctx.AddValidation(v)
vs = append(vs, v)
}
var pr bool
if s.Extras["private"] != nil {
pr, _ = helpers.GetPrivate(s)
} else {
pr, _ = helpers.GetPrivate(ctx.Raw)
}
var cn, ct string
if s.Extras["column"] != nil {
cn, ct, _ = helpers.GetColumn(s)
} else {
cn, ct, _ = helpers.GetColumn(ctx.Raw)
}
return &Number{
Schema: s,
NativeType: "number",
Type: "number",
ColumnName: cn,
ColumnType: ct,
Name: helpers.UpperCamelCase(s.Title),
key: ctx.Key,
Private: pr,
Validations: vs,
}
}
func (o Number) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Title": o.Title(),
"Description": o.Description,
"NativeType": o.NativeType,
"Type": o.Type,
"Name": o.Name,
"Required": o.Required,
"Validations": o.Validations,
"Properties": o.Properties,
"ColumnName": o.ColumnName,
"ColumnType": o.ColumnType,
})
}
func (o Number) Raw() *schema.Schema {
return o.Schema
}
func (o Number) Title() string {
return o.Schema.Title
}
func (o Number) Format() string {
return string(o.Schema.Format)
}
func (o Number) Key() string {
return o.key
}
func (o Number) ReadOnly() bool {
v := o.Schema.Extras["readOnly"]
if v == nil {
return false
}
r, ok := v.(bool)
if !ok {
return false
}
return r
}
func (o Number) Example(withoutReadOnly bool) interface{} {
v := o.Schema.Extras["example"]
if v == nil {
return 0
}
return v
}
func (o Number) ExampleString() string {
return helpers.Serialize(o.Example(false))
}