-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathproperties.go
More file actions
116 lines (99 loc) · 2.9 KB
/
properties.go
File metadata and controls
116 lines (99 loc) · 2.9 KB
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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package spec
import (
"bytes"
"encoding/json"
"reflect"
"sort"
)
// OrderSchemaItem holds a named schema (e.g. from a property of an object).
type OrderSchemaItem struct {
Schema
Name string
}
// OrderSchemaItems is a sortable slice of named schemas.
// The ordering is defined by the x-order schema extension.
type OrderSchemaItems []OrderSchemaItem
// MarshalJSON produces a json object with keys defined by the name schemas
// of the OrderSchemaItems slice, keeping the original order of the slice.
func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf.WriteByte('{')
if len(items) == 0 {
buf.WriteByte('}')
return buf.Bytes(), nil
}
if err := items.marshalJSONItem(items[0], buf); err != nil {
return nil, err
}
for _, item := range items[1:] {
buf.WriteByte(',')
if err := items.marshalJSONItem(item, buf); err != nil {
return nil, err
}
}
buf.WriteByte('}')
return buf.Bytes(), nil
}
func (items OrderSchemaItems) Len() int { return len(items) }
func (items OrderSchemaItems) Swap(i, j int) { items[i], items[j] = items[j], items[i] }
func (items OrderSchemaItems) Less(i, j int) (ret bool) {
ii, oki := items[i].Extensions.GetInt("x-order")
ij, okj := items[j].Extensions.GetInt("x-order")
if oki { //nolint:nestif // nested recover logic for safe type comparison
if okj {
defer func() {
if err := recover(); err != nil {
defer func() {
if err = recover(); err != nil {
ret = items[i].Name < items[j].Name
}
}()
ret = reflect.ValueOf(ii).String() < reflect.ValueOf(ij).String()
}
}()
return ii < ij
}
return true
} else if okj {
return false
}
return items[i].Name < items[j].Name
}
func (items OrderSchemaItems) marshalJSONItem(item OrderSchemaItem, output *bytes.Buffer) error {
nameJSON, err := json.Marshal(item.Name)
if err != nil {
return err
}
output.Write(nameJSON)
output.WriteByte(':')
schemaJSON, err := json.Marshal(&item.Schema)
if err != nil {
return err
}
output.Write(schemaJSON)
return nil
}
// SchemaProperties is a map representing the properties of a Schema object.
// It knows how to transform its keys into an ordered slice.
type SchemaProperties map[string]Schema
// ToOrderedSchemaItems transforms the map of properties into a sortable slice.
func (properties SchemaProperties) ToOrderedSchemaItems() OrderSchemaItems {
items := make(OrderSchemaItems, 0, len(properties))
for k, v := range properties {
items = append(items, OrderSchemaItem{
Name: k,
Schema: v,
})
}
sort.Sort(items)
return items
}
// MarshalJSON produces properties as json, keeping their order.
func (properties SchemaProperties) MarshalJSON() ([]byte, error) {
if properties == nil {
return []byte("null"), nil
}
return json.Marshal(properties.ToOrderedSchemaItems())
}