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

342 lines
5.1 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 (
jsonArray = "array"
)
// HeaderProps describes a response header
2024-05-14 13:07:09 +00:00
type HeaderProps struct {
Description string `json:"description,omitempty"`
}
// Header describes a header for a response of the API
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#headerObject
2024-05-14 13:07:09 +00:00
type Header struct {
CommonValidations
2024-05-14 13:07:09 +00:00
SimpleSchema
2024-05-14 13:07:09 +00:00
VendorExtensible
2024-05-14 13:07:09 +00:00
HeaderProps
}
// ResponseHeader creates a new header instance for use in a response
2024-05-14 13:07:09 +00:00
func ResponseHeader() *Header {
2024-05-14 13:07:09 +00:00
return new(Header)
2024-05-14 13:07:09 +00:00
}
// WithDescription sets the description on this response, allows for chaining
2024-05-14 13:07:09 +00:00
func (h *Header) WithDescription(description string) *Header {
2024-05-14 13:07:09 +00:00
h.Description = description
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// Typed a fluent builder method for the type of parameter
2024-05-14 13:07:09 +00:00
func (h *Header) Typed(tpe, format string) *Header {
2024-05-14 13:07:09 +00:00
h.Type = tpe
2024-05-14 13:07:09 +00:00
h.Format = format
2024-05-14 13:07:09 +00:00
return h
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 (h *Header) CollectionOf(items *Items, format string) *Header {
2024-05-14 13:07:09 +00:00
h.Type = jsonArray
2024-05-14 13:07:09 +00:00
h.Items = items
2024-05-14 13:07:09 +00:00
h.CollectionFormat = format
2024-05-14 13:07:09 +00:00
return h
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 (h *Header) WithDefault(defaultValue interface{}) *Header {
2024-05-14 13:07:09 +00:00
h.Default = defaultValue
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMaxLength sets a max length value
2024-05-14 13:07:09 +00:00
func (h *Header) WithMaxLength(max int64) *Header {
2024-05-14 13:07:09 +00:00
h.MaxLength = &max
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMinLength sets a min length value
2024-05-14 13:07:09 +00:00
func (h *Header) WithMinLength(min int64) *Header {
2024-05-14 13:07:09 +00:00
h.MinLength = &min
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithPattern sets a pattern value
2024-05-14 13:07:09 +00:00
func (h *Header) WithPattern(pattern string) *Header {
2024-05-14 13:07:09 +00:00
h.Pattern = pattern
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMultipleOf sets a multiple of value
2024-05-14 13:07:09 +00:00
func (h *Header) WithMultipleOf(number float64) *Header {
2024-05-14 13:07:09 +00:00
h.MultipleOf = &number
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMaximum sets a maximum number value
2024-05-14 13:07:09 +00:00
func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
2024-05-14 13:07:09 +00:00
h.Maximum = &max
2024-05-14 13:07:09 +00:00
h.ExclusiveMaximum = exclusive
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMinimum sets a minimum number value
2024-05-14 13:07:09 +00:00
func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
2024-05-14 13:07:09 +00:00
h.Minimum = &min
2024-05-14 13:07:09 +00:00
h.ExclusiveMinimum = exclusive
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithEnum sets a the enum values (replace)
2024-05-14 13:07:09 +00:00
func (h *Header) WithEnum(values ...interface{}) *Header {
2024-05-14 13:07:09 +00:00
h.Enum = append([]interface{}{}, values...)
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMaxItems sets the max items
2024-05-14 13:07:09 +00:00
func (h *Header) WithMaxItems(size int64) *Header {
2024-05-14 13:07:09 +00:00
h.MaxItems = &size
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithMinItems sets the min items
2024-05-14 13:07:09 +00:00
func (h *Header) WithMinItems(size int64) *Header {
2024-05-14 13:07:09 +00:00
h.MinItems = &size
2024-05-14 13:07:09 +00:00
return h
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 (h *Header) UniqueValues() *Header {
2024-05-14 13:07:09 +00:00
h.UniqueItems = true
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// AllowDuplicates this array can have duplicates
2024-05-14 13:07:09 +00:00
func (h *Header) AllowDuplicates() *Header {
2024-05-14 13:07:09 +00:00
h.UniqueItems = false
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// WithValidations is a fluent method to set header validations
2024-05-14 13:07:09 +00:00
func (h *Header) WithValidations(val CommonValidations) *Header {
2024-05-14 13:07:09 +00:00
h.SetValidations(SchemaValidations{CommonValidations: val})
2024-05-14 13:07:09 +00:00
return h
2024-05-14 13:07:09 +00:00
}
// MarshalJSON marshal this to JSON
2024-05-14 13:07:09 +00:00
func (h Header) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(h.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(h.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(h.HeaderProps)
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(b1, b2, b3), nil
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON unmarshals this header from JSON
2024-05-14 13:07:09 +00:00
func (h *Header) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &h.CommonValidations); 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
if err := json.Unmarshal(data, &h.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
if err := json.Unmarshal(data, &h.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
return json.Unmarshal(data, &h.HeaderProps)
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 (h Header) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if ex, ok := h.Extensions[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
r, _, err := jsonpointer.GetForToken(h.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(h.SimpleSchema, 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(h.HeaderProps, token)
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}