forked from go-openapi/spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
80 lines (73 loc) · 1.71 KB
/
properties.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
package spec
import (
"bytes"
"encoding/json"
"reflect"
"sort"
)
type OrderSchemaItem struct {
Name string
Schema
}
type OrderSchemaItems []OrderSchemaItem
func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf.WriteString("{")
for i := range items {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString("\"")
buf.WriteString(items[i].Name)
buf.WriteString("\":")
bs, err := json.Marshal(&items[i].Schema)
if err != nil {
return nil, err
}
buf.Write(bs)
}
buf.WriteString("}")
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.GetString("x-order")
ij, okj := items[j].Extensions.GetString("x-order")
if oki {
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 reflect.ValueOf(ii).Int() < reflect.ValueOf(ij).Int()
} else {
return true
}
} else if okj {
return false
} else {
return items[i].Name < items[j].Name
}
}
type SchemaProperties map[string]Schema
func (properties SchemaProperties) MarshalJSON() ([]byte, error) {
if properties == nil {
return []byte("null"), nil
}
var items OrderSchemaItems = make(OrderSchemaItems, 0, len(properties))
for k, v := range properties {
items = append(items, OrderSchemaItem{
Name: k,
Schema: v,
})
}
sort.Sort(items)
return json.Marshal(items)
}