Files
libopenapi/datamodel/low/v2/response.go
Dave Shanley 3d5ecf0efb Refactored version directory names
3.0 and 2.0 do not work, there are multiple versions and anything with a period in it sucks from my point of view, v2 and v3 feel much better from a DX perspective.
2022-09-16 08:33:39 -04:00

65 lines
1.6 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v2
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3"
)
const (
ResponsesLabel = "responses"
)
type Response struct {
Description low.NodeReference[string]
Schema low.NodeReference[*base.SchemaProxy]
Headers low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]
Examples low.NodeReference[*Examples]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
}
func (r *Response) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap[any](ext, r.Extensions)
}
func (r *Response) FindHeader(hType string) *low.ValueReference[*Header] {
return low.FindItemInMap[*Header](hType, r.Headers.Value)
}
func (r *Response) Build(root *yaml.Node, idx *index.SpecIndex) error {
r.Extensions = low.ExtractExtensions(root)
s, err := base.ExtractSchema(root, idx)
if err != nil {
return err
}
if s != nil {
r.Schema = *s
}
// extract examples
examples, expErr := low.ExtractObject[*Examples](ExamplesLabel, root, idx)
if expErr != nil {
return expErr
}
r.Examples = examples
//extract headers
headers, lN, kN, err := low.ExtractMapFlat[*Header](HeadersLabel, root, idx)
if err != nil {
return err
}
if headers != nil {
r.Headers = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*Header]]{
Value: headers,
KeyNode: lN,
ValueNode: kN,
}
}
return nil
}