-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.go
119 lines (105 loc) · 2.3 KB
/
object.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
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 Object struct {
*schema.Schema
NativeType string
TableName string
ColumnName string
ColumnType string
Type string
Name string
key string
Private bool
Required []string
Reference string
Validations []validations.Validation
Properties []Schema
}
func NewObject(ctx *Context, s *schema.Schema) *Object {
var pr bool
if s.Extras["private"] != nil {
pr, _ = helpers.GetPrivate(s)
} else {
pr, _ = helpers.GetPrivate(ctx.Raw)
}
var tn string
if s.Extras["table"] != nil {
tn, _ = helpers.GetTable(s)
} else {
tn, _ = helpers.GetTable(ctx.Raw)
}
return &Object{
Schema: s,
NativeType: "object",
TableName: tn,
Type: helpers.UpperCamelCase(s.Title),
Name: helpers.UpperCamelCase(s.Title),
key: ctx.Key,
Required: ctx.Raw.Required,
Private: pr,
Reference: ctx.Raw.Reference,
Properties: []Schema{},
}
}
func (o Object) 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,
"TableName": o.TableName,
"ColumnName": o.ColumnName,
"ColumnType": o.ColumnType,
"Reference": o.Reference,
})
}
func (o Object) Raw() *schema.Schema {
return o.Schema
}
func (o Object) Title() string {
return o.Schema.Title
}
func (o Object) Format() string {
return string(o.Schema.Format)
}
func (o Object) Key() string {
return o.key
}
func (o Object) ReadOnly() bool {
v := o.Schema.Extras["readOnly"]
if v == nil {
return false
}
r, ok := v.(bool)
if !ok {
return false
}
return r
}
func (o Object) Example(withoutReadOnly bool) interface{} {
e := make(map[string]interface{})
if withoutReadOnly {
for _, s := range o.Properties {
if !s.ReadOnly() {
e[s.Key()] = s.Example(withoutReadOnly)
}
}
return e
}
for _, s := range o.Properties {
e[s.Key()] = s.Example(withoutReadOnly)
}
return e
}
func (o Object) ExampleString() string {
return helpers.Serialize(o.Example(false))
}