mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
(feat): Unpack extensions into complex function types #8
The more in-depth we use extensions, the more likely is is that we need custom extensions. New `UnpackExtensions` function in the high package makes this easy. Low level models have been updated to support feature fully and docs added in README and examples as well. Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -6,6 +6,7 @@ package high
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -18,4 +19,112 @@ func TestExtractExtensions(t *testing.T) {
|
||||
}
|
||||
ext := ExtractExtensions(n)
|
||||
assert.Equal(t, "new cowboy in town", ext["pb33f"])
|
||||
}
|
||||
|
||||
type textExtension struct {
|
||||
Cowboy string
|
||||
Power int
|
||||
}
|
||||
|
||||
type parent struct {
|
||||
low *child
|
||||
}
|
||||
|
||||
func (p *parent) GoLow() *child {
|
||||
return p.low
|
||||
}
|
||||
|
||||
type child struct {
|
||||
Extensions map[low.KeyReference[string]]low.ValueReference[any]
|
||||
}
|
||||
|
||||
func (c *child) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
||||
return c.Extensions
|
||||
}
|
||||
|
||||
func TestUnpackExtensions(t *testing.T) {
|
||||
|
||||
var resultA, resultB yaml.Node
|
||||
|
||||
ymlA := `
|
||||
cowboy: buckaroo
|
||||
power: 100`
|
||||
|
||||
ymlB := `
|
||||
cowboy: frogman
|
||||
power: 2`
|
||||
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
|
||||
res, err := UnpackExtensions[textExtension, *child](p)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, res)
|
||||
assert.Equal(t, "buckaroo", res["x-rancher-a"].Cowboy)
|
||||
assert.Equal(t, 100, res["x-rancher-a"].Power)
|
||||
assert.Equal(t, "frogman", res["x-rancher-b"].Cowboy)
|
||||
assert.Equal(t, 2, res["x-rancher-b"].Power)
|
||||
}
|
||||
|
||||
func TestUnpackExtensions_Fail(t *testing.T) {
|
||||
|
||||
var resultA, resultB yaml.Node
|
||||
|
||||
ymlA := `
|
||||
cowboy: buckaroo
|
||||
power: 100`
|
||||
|
||||
// this is incorrect types, unpacking will fail.
|
||||
ymlB := `
|
||||
cowboy: 0
|
||||
power: hello`
|
||||
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
|
||||
res, er := UnpackExtensions[textExtension, *child](p)
|
||||
assert.Error(t, er)
|
||||
assert.Empty(t, res)
|
||||
}
|
||||
Reference in New Issue
Block a user