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

400 lines
5.8 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"strings"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
const (
jsonRef = "$ref"
)
// SimpleSchema describe swagger simple schemas for parameters and headers
2024-05-14 13:07:09 +00:00
type SimpleSchema struct {
Type string `json:"type,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Format string `json:"format,omitempty"`
Items *Items `json:"items,omitempty"`
CollectionFormat string `json:"collectionFormat,omitempty"`
Default interface{} `json:"default,omitempty"`
Example interface{} `json:"example,omitempty"`
2024-05-14 13:07:09 +00:00
}
// TypeName return the type (or format) of a simple schema
2024-05-14 13:07:09 +00:00
func (s *SimpleSchema) TypeName() string {
2024-05-14 13:07:09 +00:00
if s.Format != "" {
2024-05-14 13:07:09 +00:00
return s.Format
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return s.Type
2024-05-14 13:07:09 +00:00
}
// ItemsTypeName yields the type of items in a simple schema array
2024-05-14 13:07:09 +00:00
func (s *SimpleSchema) ItemsTypeName() string {
2024-05-14 13:07:09 +00:00
if s.Items == nil {
2024-05-14 13:07:09 +00:00
return ""
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return s.Items.TypeName()
2024-05-14 13:07:09 +00:00
}
// Items a limited subset of JSON-Schema's items object.
2024-05-14 13:07:09 +00:00
// It is used by parameter definitions that are not located in "body".
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#items-object
2024-05-14 13:07:09 +00:00
type Items struct {
Refable
2024-05-14 13:07:09 +00:00
CommonValidations
2024-05-14 13:07:09 +00:00
SimpleSchema
2024-05-14 13:07:09 +00:00
VendorExtensible
}
// NewItems creates a new instance of items
2024-05-14 13:07:09 +00:00
func NewItems() *Items {
2024-05-14 13:07:09 +00:00
return &Items{}
2024-05-14 13:07:09 +00:00
}
// Typed a fluent builder method for the type of item
2024-05-14 13:07:09 +00:00
func (i *Items) Typed(tpe, format string) *Items {
2024-05-14 13:07:09 +00:00
i.Type = tpe
2024-05-14 13:07:09 +00:00
i.Format = format
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// AsNullable flags this schema as nullable.
2024-05-14 13:07:09 +00:00
func (i *Items) AsNullable() *Items {
2024-05-14 13:07:09 +00:00
i.Nullable = true
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// CollectionOf a fluent builder method for an array item
2024-05-14 13:07:09 +00:00
func (i *Items) CollectionOf(items *Items, format string) *Items {
2024-05-14 13:07:09 +00:00
i.Type = jsonArray
2024-05-14 13:07:09 +00:00
i.Items = items
2024-05-14 13:07:09 +00:00
i.CollectionFormat = format
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithDefault sets the default value on this item
2024-05-14 13:07:09 +00:00
func (i *Items) WithDefault(defaultValue interface{}) *Items {
2024-05-14 13:07:09 +00:00
i.Default = defaultValue
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMaxLength sets a max length value
2024-05-14 13:07:09 +00:00
func (i *Items) WithMaxLength(max int64) *Items {
2024-05-14 13:07:09 +00:00
i.MaxLength = &max
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMinLength sets a min length value
2024-05-14 13:07:09 +00:00
func (i *Items) WithMinLength(min int64) *Items {
2024-05-14 13:07:09 +00:00
i.MinLength = &min
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithPattern sets a pattern value
2024-05-14 13:07:09 +00:00
func (i *Items) WithPattern(pattern string) *Items {
2024-05-14 13:07:09 +00:00
i.Pattern = pattern
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMultipleOf sets a multiple of value
2024-05-14 13:07:09 +00:00
func (i *Items) WithMultipleOf(number float64) *Items {
2024-05-14 13:07:09 +00:00
i.MultipleOf = &number
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMaximum sets a maximum number value
2024-05-14 13:07:09 +00:00
func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
2024-05-14 13:07:09 +00:00
i.Maximum = &max
2024-05-14 13:07:09 +00:00
i.ExclusiveMaximum = exclusive
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMinimum sets a minimum number value
2024-05-14 13:07:09 +00:00
func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
2024-05-14 13:07:09 +00:00
i.Minimum = &min
2024-05-14 13:07:09 +00:00
i.ExclusiveMinimum = exclusive
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithEnum sets a the enum values (replace)
2024-05-14 13:07:09 +00:00
func (i *Items) WithEnum(values ...interface{}) *Items {
2024-05-14 13:07:09 +00:00
i.Enum = append([]interface{}{}, values...)
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMaxItems sets the max items
2024-05-14 13:07:09 +00:00
func (i *Items) WithMaxItems(size int64) *Items {
2024-05-14 13:07:09 +00:00
i.MaxItems = &size
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithMinItems sets the min items
2024-05-14 13:07:09 +00:00
func (i *Items) WithMinItems(size int64) *Items {
2024-05-14 13:07:09 +00:00
i.MinItems = &size
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// UniqueValues dictates that this array can only have unique items
2024-05-14 13:07:09 +00:00
func (i *Items) UniqueValues() *Items {
2024-05-14 13:07:09 +00:00
i.UniqueItems = true
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// AllowDuplicates this array can have duplicates
2024-05-14 13:07:09 +00:00
func (i *Items) AllowDuplicates() *Items {
2024-05-14 13:07:09 +00:00
i.UniqueItems = false
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// WithValidations is a fluent method to set Items validations
2024-05-14 13:07:09 +00:00
func (i *Items) WithValidations(val CommonValidations) *Items {
2024-05-14 13:07:09 +00:00
i.SetValidations(SchemaValidations{CommonValidations: val})
2024-05-14 13:07:09 +00:00
return i
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON hydrates this items instance with the data from JSON
2024-05-14 13:07:09 +00:00
func (i *Items) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
var validations CommonValidations
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &validations); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var ref Refable
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &ref); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var simpleSchema SimpleSchema
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &simpleSchema); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var vendorExtensible VendorExtensible
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &vendorExtensible); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
i.Refable = ref
2024-05-14 13:07:09 +00:00
i.CommonValidations = validations
2024-05-14 13:07:09 +00:00
i.SimpleSchema = simpleSchema
2024-05-14 13:07:09 +00:00
i.VendorExtensible = vendorExtensible
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// MarshalJSON converts this items object to JSON
2024-05-14 13:07:09 +00:00
func (i Items) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(i.CommonValidations)
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
b2, err := json.Marshal(i.SimpleSchema)
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
b3, err := json.Marshal(i.Refable)
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
b4, err := json.Marshal(i.VendorExtensible)
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
return swag.ConcatJSON(b4, b3, b1, b2), nil
2024-05-14 13:07:09 +00:00
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (i Items) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if token == jsonRef {
2024-05-14 13:07:09 +00:00
return &i.Ref, nil
2024-05-14 13:07:09 +00:00
}
r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
2024-05-14 13:07:09 +00:00
if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
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
if r != nil {
2024-05-14 13:07:09 +00:00
return r, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}