-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
141 lines (135 loc) · 4.55 KB
/
property.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 chioas
import (
"encoding/json"
"github.com/go-andiamo/chioas/yaml"
)
// Properties is an ordered collection of Property
type Properties []Property
// Property represents the OAS definition of a property
type Property struct {
// Name is the OAS name of the property
Name string
// Description is the OAS description of the property
Description string
// Type is the OAS type of the property
//
// Should be one of "string", "object", "array", "boolean", "integer", "number" or "null"
Type string
// ItemType is the OAS type of array items
//
// only used if Type = "array"
ItemType string
// Properties is the ordered collection of sub-properties
//
// Only used if Type == "object" (or Type == "array" and ItemType == "object"
Properties Properties
// Required indicates the property is required
//
// see also Schema.RequiredProperties
Required bool
// Format is the OAS format for the property
Format string
// Example is the OAS example for the property
Example any
// Enum is the OAS enum of the property
Enum []any
// Deprecated is the OAS deprecated flag for the property
Deprecated bool
// Constraints is the OAS constraints for the property
Constraints Constraints
// SchemaRef is the OAS schema reference
//
// Only used if value is a non-empty string
//
// If the value does not contain a path (i.e. does not contain any "/") then the ref
// path will be the value prefixed with components schemas path. For example, specifying "foo"
// will result in a schema ref:
// schema:
// $ref: "#/components/schemas/foo"
SchemaRef string
// Extensions is extension OAS yaml properties
Extensions Extensions
// Additional is any additional OAS spec yaml to be written
Additional Additional
// Comment is any comment(s) to appear in the OAS spec yaml
Comment string
}
func (p Property) writeYaml(w yaml.Writer, top bool) {
w.WriteTagStart("\"" + p.Name + "\"").
WriteComments(p.Comment)
if p.SchemaRef != "" {
writeSchemaRef(p.SchemaRef, p.Type == tagValueTypeArray, w)
} else {
w.WriteTagValue(tagNameDescription, p.Description).
WriteTagValue(tagNameType, defValue(p.Type, tagValueTypeString))
if p.Type == tagValueTypeArray {
w.WriteTagStart(tagNameItems).
WriteTagValue(tagNameType, defValue(p.ItemType, tagValueTypeString))
}
w.WriteTagValue(tagNameExample, p.Example).
WriteTagValue(tagNameFormat, nilString(p.Format))
if len(p.Enum) > 0 {
w.WriteTagStart(tagNameEnum)
for _, e := range p.Enum {
w.WriteItem(e)
}
w.WriteTagEnd()
}
w.WriteTagValue(tagNameRequired, nilBool(p.Required && !top)).
WriteTagValue(tagNameDeprecated, nilBool(p.Deprecated))
p.Constraints.writeYaml(w)
if (p.Type == tagValueTypeObject || (p.Type == tagValueTypeArray && p.ItemType == tagValueTypeObject)) && len(p.Properties) > 0 {
w.WriteTagStart(tagNameProperties)
for _, sub := range p.Properties {
sub.writeYaml(w, false)
}
w.WriteTagEnd()
}
if p.Type == tagValueTypeArray {
w.WriteTagEnd()
}
}
writeExtensions(p.Extensions, w)
writeAdditional(p.Additional, p, w)
w.WriteTagEnd()
}
// Constraints defines the constraints for an OAS property
type Constraints struct {
Pattern string
Maximum json.Number
Minimum json.Number
ExclusiveMaximum bool
ExclusiveMinimum bool
Nullable bool
MultipleOf uint
MaxLength uint
MinLength uint
MaxItems uint
MinItems uint
UniqueItems bool
MaxProperties uint
MinProperties uint
// Additional is any other OAS constraints for a property (that are not currently defined in Constraints)
Additional map[string]any
}
func (c Constraints) writeYaml(w yaml.Writer) {
w.WriteTagValue(tagNamePattern, nilString(c.Pattern)).
WriteTagValue(tagNameMaximum, nilNumber(c.Maximum)).
WriteTagValue(tagNameMinimum, nilNumber(c.Minimum)).
WriteTagValue(tagNameExclusiveMaximum, nilBool(c.ExclusiveMaximum)).
WriteTagValue(tagNameExclusiveMinimum, nilBool(c.ExclusiveMinimum)).
WriteTagValue(tagNameNullable, nilBool(c.Nullable)).
WriteTagValue(tagNameMultipleOf, nilUint(c.MultipleOf)).
WriteTagValue(tagNameMaxLength, nilUint(c.MaxLength)).
WriteTagValue(tagNameMinLength, nilUint(c.MinLength)).
WriteTagValue(tagNameMaxItems, nilUint(c.MaxItems)).
WriteTagValue(tagNameMinItems, nilUint(c.MinItems)).
WriteTagValue(tagNameUniqueItems, nilBool(c.UniqueItems)).
WriteTagValue(tagNameMaxProperties, nilUint(c.MaxProperties)).
WriteTagValue(tagNameMinProperties, nilUint(c.MinProperties))
if c.Additional != nil {
for k, v := range c.Additional {
w.WriteTagValue(k, v)
}
}
}