Files
libopenapi/datamodel/low/3.0/parameter.go
Dave Shanley c34c4f668c Massive refactor on how the library handles schemas.
Schemas are now rendered on demand. There is no reasonable way to navigate the mayhem that is circular dependencies through multiple inheritance and polymorphism. So now using a msuch simpler design (and MUCH faster), there is a `SchemaProxy` for every schema reference. This holds a reference to the low model and index, that renders the schema on demand. Once rendered, it's done. Any children can also be rendered on demand, and so down the rabbit hole you do (if you want).

All circular dependencies are know by the index, so you can decide when you want to stop, or just keep going for ever, however it's now a choice, not something decided for you.

Signed-off-by: Dave Shanley <dave@quobix.com>
2022-08-27 09:47:37 -04:00

90 lines
2.5 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/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
const (
SchemaLabel = "schema"
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[*SchemaProxy]
Example low.NodeReference[any]
Examples low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*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[*Example] {
return low.FindItemInMap[*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(ExampleLabel, root.Content)
if expNode != nil {
p.Example = low.ExtractExample(expNode, expLabel)
}
// handle schema
sch, sErr := ExtractSchema(root, idx)
if sErr != nil {
return sErr
}
if sch != nil {
p.Schema = *sch
}
// handle examples if set.
exps, expsL, expsN, eErr := low.ExtractMapFlat[*Example](ExamplesLabel, root, idx)
if eErr != nil {
return eErr
}
if exps != nil {
p.Examples = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*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
}