forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/properties.go

158 lines
2.4 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
package spec
import (
"bytes"
"encoding/json"
"reflect"
"sort"
)
// OrderSchemaItem holds a named schema (e.g. from a property of an object)
2024-05-14 13:07:09 +00:00
type OrderSchemaItem struct {
Name string
2024-05-14 13:07:09 +00:00
Schema
}
// OrderSchemaItems is a sortable slice of named schemas.
2024-05-14 13:07:09 +00:00
// The ordering is defined by the x-order schema extension.
2024-05-14 13:07:09 +00:00
type OrderSchemaItems []OrderSchemaItem
// MarshalJSON produces a json object with keys defined by the name schemas
2024-05-14 13:07:09 +00:00
// of the OrderSchemaItems slice, keeping the original order of the slice.
2024-05-14 13:07:09 +00:00
func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
buf := bytes.NewBuffer(nil)
2024-05-14 13:07:09 +00:00
buf.WriteString("{")
2024-05-14 13:07:09 +00:00
for i := range items {
2024-05-14 13:07:09 +00:00
if i > 0 {
2024-05-14 13:07:09 +00:00
buf.WriteString(",")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
buf.WriteString("\"")
2024-05-14 13:07:09 +00:00
buf.WriteString(items[i].Name)
2024-05-14 13:07:09 +00:00
buf.WriteString("\":")
2024-05-14 13:07:09 +00:00
bs, err := json.Marshal(&items[i].Schema)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
buf.Write(bs)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
buf.WriteString("}")
2024-05-14 13:07:09 +00:00
return buf.Bytes(), nil
2024-05-14 13:07:09 +00:00
}
func (items OrderSchemaItems) Len() int { return len(items) }
2024-05-14 13:07:09 +00:00
func (items OrderSchemaItems) Swap(i, j int) { items[i], items[j] = items[j], items[i] }
2024-05-14 13:07:09 +00:00
func (items OrderSchemaItems) Less(i, j int) (ret bool) {
2024-05-14 13:07:09 +00:00
ii, oki := items[i].Extensions.GetInt("x-order")
2024-05-14 13:07:09 +00:00
ij, okj := items[j].Extensions.GetInt("x-order")
2024-05-14 13:07:09 +00:00
if oki {
2024-05-14 13:07:09 +00:00
if okj {
2024-05-14 13:07:09 +00:00
defer func() {
2024-05-14 13:07:09 +00:00
if err := recover(); err != nil {
2024-05-14 13:07:09 +00:00
defer func() {
2024-05-14 13:07:09 +00:00
if err = recover(); err != nil {
2024-05-14 13:07:09 +00:00
ret = items[i].Name < items[j].Name
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}()
2024-05-14 13:07:09 +00:00
ret = reflect.ValueOf(ii).String() < reflect.ValueOf(ij).String()
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}()
2024-05-14 13:07:09 +00:00
return ii < ij
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return true
2024-05-14 13:07:09 +00:00
} else if okj {
2024-05-14 13:07:09 +00:00
return false
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return items[i].Name < items[j].Name
2024-05-14 13:07:09 +00:00
}
// SchemaProperties is a map representing the properties of a Schema object.
2024-05-14 13:07:09 +00:00
// It knows how to transform its keys into an ordered slice.
2024-05-14 13:07:09 +00:00
type SchemaProperties map[string]Schema
// ToOrderedSchemaItems transforms the map of properties into a sortable slice
2024-05-14 13:07:09 +00:00
func (properties SchemaProperties) ToOrderedSchemaItems() OrderSchemaItems {
2024-05-14 13:07:09 +00:00
items := make(OrderSchemaItems, 0, len(properties))
2024-05-14 13:07:09 +00:00
for k, v := range properties {
2024-05-14 13:07:09 +00:00
items = append(items, OrderSchemaItem{
Name: k,
2024-05-14 13:07:09 +00:00
Schema: v,
})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
sort.Sort(items)
2024-05-14 13:07:09 +00:00
return items
2024-05-14 13:07:09 +00:00
}
// MarshalJSON produces properties as json, keeping their order.
2024-05-14 13:07:09 +00:00
func (properties SchemaProperties) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
if properties == nil {
2024-05-14 13:07:09 +00:00
return []byte("null"), nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Marshal(properties.ToOrderedSchemaItems())
2024-05-14 13:07:09 +00:00
}