Files
libopenapi/datamodel/low/v3/parameter.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

90 lines
2.6 KiB
Go

// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v3
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
const (
ContentLabel = "content"
)
type Parameter struct {
Name low.NodeReference[string]
In low.NodeReference[string]
Description low.NodeReference[string]
Required low.NodeReference[bool]
Deprecated low.NodeReference[bool]
AllowEmptyValue low.NodeReference[bool]
Style low.NodeReference[string]
Explode low.NodeReference[bool]
AllowReserved low.NodeReference[bool]
Schema low.NodeReference[*base.SchemaProxy]
Example low.NodeReference[any]
Examples low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*base.Example]]
Content low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]
Extensions map[low.KeyReference[string]]low.ValueReference[any]
}
func (p *Parameter) FindContent(cType string) *low.ValueReference[*MediaType] {
return low.FindItemInMap[*MediaType](cType, p.Content.Value)
}
func (p *Parameter) FindExample(eType string) *low.ValueReference[*base.Example] {
return low.FindItemInMap[*base.Example](eType, p.Examples.Value)
}
func (p *Parameter) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap[any](ext, p.Extensions)
}
func (p *Parameter) Build(root *yaml.Node, idx *index.SpecIndex) error {
p.Extensions = low.ExtractExtensions(root)
// handle example if set.
_, expLabel, expNode := utils.FindKeyNodeFull(base.ExampleLabel, root.Content)
if expNode != nil {
p.Example = low.ExtractExample(expNode, expLabel)
}
// handle schema
sch, sErr := base.ExtractSchema(root, idx)
if sErr != nil {
return sErr
}
if sch != nil {
p.Schema = *sch
}
// handle examples if set.
exps, expsL, expsN, eErr := low.ExtractMapFlat[*base.Example](base.ExamplesLabel, root, idx)
if eErr != nil {
return eErr
}
if exps != nil {
p.Examples = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*base.Example]]{
Value: exps,
KeyNode: expsL,
ValueNode: expsN,
}
}
// handle content, if set.
con, cL, cN, cErr := low.ExtractMapFlat[*MediaType](ContentLabel, root, idx)
if cErr != nil {
return cErr
}
p.Content = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]{
Value: con,
KeyNode: cL,
ValueNode: cN,
}
return nil
}