mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 04:20:24 +00:00
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.
This commit is contained in:
88
datamodel/low/v3/header.go
Normal file
88
datamodel/low/v3/header.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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 (
|
||||
HeadersLabel = "headers"
|
||||
)
|
||||
|
||||
type Header struct {
|
||||
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 (h *Header) FindExtension(ext string) *low.ValueReference[any] {
|
||||
return low.FindItemInMap[any](ext, h.Extensions)
|
||||
}
|
||||
|
||||
func (h *Header) FindExample(eType string) *low.ValueReference[*base.Example] {
|
||||
return low.FindItemInMap[*base.Example](eType, h.Examples.Value)
|
||||
}
|
||||
|
||||
func (h *Header) FindContent(ext string) *low.ValueReference[*MediaType] {
|
||||
return low.FindItemInMap[*MediaType](ext, h.Content.Value)
|
||||
}
|
||||
|
||||
func (h *Header) Build(root *yaml.Node, idx *index.SpecIndex) error {
|
||||
h.Extensions = low.ExtractExtensions(root)
|
||||
|
||||
// handle example if set.
|
||||
_, expLabel, expNode := utils.FindKeyNodeFull(base.ExampleLabel, root.Content)
|
||||
if expNode != nil {
|
||||
h.Example = low.ExtractExample(expNode, expLabel)
|
||||
}
|
||||
|
||||
// handle examples if set.
|
||||
exps, expsL, expsN, eErr := low.ExtractMapFlat[*base.Example](base.ExamplesLabel, root, idx)
|
||||
if eErr != nil {
|
||||
return eErr
|
||||
}
|
||||
if exps != nil {
|
||||
h.Examples = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*base.Example]]{
|
||||
Value: exps,
|
||||
KeyNode: expsL,
|
||||
ValueNode: expsN,
|
||||
}
|
||||
}
|
||||
|
||||
// handle schema
|
||||
sch, sErr := base.ExtractSchema(root, idx)
|
||||
if sErr != nil {
|
||||
return sErr
|
||||
}
|
||||
if sch != nil {
|
||||
h.Schema = *sch
|
||||
}
|
||||
|
||||
// handle content, if set.
|
||||
con, cL, cN, cErr := low.ExtractMapFlat[*MediaType](ContentLabel, root, idx)
|
||||
if cErr != nil {
|
||||
return cErr
|
||||
}
|
||||
h.Content = low.NodeReference[map[low.KeyReference[string]]low.ValueReference[*MediaType]]{
|
||||
Value: con,
|
||||
KeyNode: cL,
|
||||
ValueNode: cN,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user