building out high model.

works just great, so happy the design came together!
This commit is contained in:
Dave Shanley
2022-08-18 18:38:41 -04:00
parent 0fd825aef8
commit 0bd119f152
10 changed files with 110 additions and 5 deletions

View File

@@ -23,6 +23,16 @@ func NewDocument(document *low.Document) *Document {
d.low = document d.low = document
d.Info = NewInfo(document.Info.Value) d.Info = NewInfo(document.Info.Value)
d.Version = document.Version.Value d.Version = document.Version.Value
var servers []*Server
for _, ser := range document.Servers.Value {
servers = append(servers, NewServer(ser.Value))
}
d.Servers = servers
var tags []*Tag
for _, tag := range document.Tags.Value {
tags = append(tags, NewTag(tag.Value))
}
d.Tags = tags
return d return d
} }

View File

@@ -40,4 +40,37 @@ func TestNewDocument_Info(t *testing.T) {
assert.Equal(t, "pb33f", highDoc.Info.License.Name) assert.Equal(t, "pb33f", highDoc.Info.License.Name)
assert.Equal(t, "https://pb33f.io/made-up", highDoc.Info.License.URL) assert.Equal(t, "https://pb33f.io/made-up", highDoc.Info.License.URL)
assert.Equal(t, "1.2", highDoc.Info.Version) assert.Equal(t, "1.2", highDoc.Info.Version)
wentLow := highDoc.GoLow()
assert.Equal(t, 1, wentLow.Version.ValueNode.Line)
assert.Equal(t, 3, wentLow.Info.Value.Title.KeyNode.Line)
}
func TestNewDocument_Servers(t *testing.T) {
h := NewDocument(doc)
assert.Len(t, h.Servers, 2)
assert.Equal(t, "{scheme}://api.pb33f.io", h.Servers[0].URL)
assert.Equal(t, "this is our main API server, for all fun API things.", h.Servers[0].Description)
assert.Len(t, h.Servers[0].Variables, 1)
assert.Equal(t, "https", h.Servers[0].Variables["scheme"].Default)
assert.Len(t, h.Servers[0].Variables["scheme"].Enum, 2)
assert.Equal(t, "https://{domain}.{host}.com", h.Servers[1].URL)
assert.Equal(t, "this is our second API server, for all fun API things.", h.Servers[1].Description)
assert.Len(t, h.Servers[1].Variables, 2)
assert.Equal(t, "api", h.Servers[1].Variables["domain"].Default)
assert.Equal(t, "pb33f.io", h.Servers[1].Variables["host"].Default)
wentLow := h.GoLow()
assert.Equal(t, 45, wentLow.Servers.Value[0].Value.Description.KeyNode.Line)
assert.Equal(t, 5, wentLow.Servers.Value[0].Value.Description.KeyNode.Column)
assert.Equal(t, 45, wentLow.Servers.Value[0].Value.Description.ValueNode.Line)
assert.Equal(t, 18, wentLow.Servers.Value[0].Value.Description.ValueNode.Column)
// holy shit! the perfect Golang OpenAPI Model! high and low! fuck yeah!
}
func TestNewDocument_Tags(t *testing.T) {
h := NewDocument(doc)
assert.Len(t, h.Tags, 2)
} }

View File

@@ -3,7 +3,10 @@
package v3 package v3
import low "github.com/pb33f/libopenapi/datamodel/low/3.0" import (
"github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/3.0"
)
type ExternalDoc struct { type ExternalDoc struct {
Description string Description string
@@ -12,6 +15,15 @@ type ExternalDoc struct {
low *low.ExternalDoc low *low.ExternalDoc
} }
func NewExternalDoc(extDoc *low.ExternalDoc) *ExternalDoc {
d := new(ExternalDoc)
d.low = extDoc
d.Description = extDoc.Description.Value
d.URL = extDoc.URL.Value
d.Extensions = high.ExtractExtensions(extDoc.Extensions)
return d
}
func (e *ExternalDoc) GoLow() *low.ExternalDoc { func (e *ExternalDoc) GoLow() *low.ExternalDoc {
return e.low return e.low
} }

View File

@@ -12,6 +12,19 @@ type Server struct {
low *low.Server low *low.Server
} }
func NewServer(server *low.Server) *Server {
s := new(Server)
s.low = server
s.Description = server.Description.Value
s.URL = server.URL.Value
vars := make(map[string]*ServerVariable)
for k, val := range server.Variables.Value {
vars[k.Value] = NewServerVariable(val.Value)
}
s.Variables = vars
return s
}
func (s *Server) GoLow() *low.Server { func (s *Server) GoLow() *low.Server {
return s.low return s.low
} }

View File

@@ -12,6 +12,21 @@ type ServerVariable struct {
low *low.ServerVariable low *low.ServerVariable
} }
func NewServerVariable(variable *low.ServerVariable) *ServerVariable {
v := new(ServerVariable)
v.low = variable
var enums []string
for _, enum := range variable.Enum {
if enum.Value != "" {
enums = append(enums, enum.Value)
}
}
v.Default = variable.Default.Value
v.Description = variable.Description.Value
v.Enum = enums
return v
}
func (s *ServerVariable) GoLow() *low.ServerVariable { func (s *ServerVariable) GoLow() *low.ServerVariable {
return s.low return s.low
} }

View File

@@ -3,7 +3,10 @@
package v3 package v3
import low "github.com/pb33f/libopenapi/datamodel/low/3.0" import (
"github.com/pb33f/libopenapi/datamodel/high"
low "github.com/pb33f/libopenapi/datamodel/low/3.0"
)
type Tag struct { type Tag struct {
Name string Name string
@@ -13,6 +16,16 @@ type Tag struct {
low *low.Tag low *low.Tag
} }
func NewTag(tag *low.Tag) *Tag {
t := new(Tag)
t.low = tag
t.Name = tag.Name.Value
t.Description = tag.Description.Value
t.ExternalDocs = NewExternalDoc(tag.ExternalDocs.Value)
t.Extensions = high.ExtractExtensions(tag.Extensions)
return t
}
func (t *Tag) GoLow() *low.Tag { func (t *Tag) GoLow() *low.Tag {
return t.low return t.low
} }

View File

@@ -3,6 +3,16 @@
package high package high
import "github.com/pb33f/libopenapi/datamodel/low"
type GoesLow[T any] interface { type GoesLow[T any] interface {
GoLow() T GoLow() T
} }
func ExtractExtensions(extensions map[low.KeyReference[string]]low.ValueReference[any]) map[string]any {
extracted := make(map[string]any)
for k, v := range extensions {
extracted[k.Value] = v.Value
}
return extracted
}

View File

@@ -10,7 +10,7 @@ import (
func CreateDocument(info *datamodel.SpecInfo) (*Document, []error) { func CreateDocument(info *datamodel.SpecInfo) (*Document, []error) {
doc := Document{Version: low.NodeReference[string]{Value: info.Version, ValueNode: info.RootNode}} doc := Document{Version: low.ValueReference[string]{Value: info.Version, ValueNode: info.RootNode}}
// build an index // build an index
idx := index.NewSpecIndex(info.RootNode) idx := index.NewSpecIndex(info.RootNode)

View File

@@ -134,7 +134,6 @@ func TestCreateDocument_Tags(t *testing.T) {
} }
func TestCreateDocument_Paths(t *testing.T) { func TestCreateDocument_Paths(t *testing.T) {
doc := doc
assert.Len(t, doc.Paths.Value.PathItems, 5) assert.Len(t, doc.Paths.Value.PathItems, 5)
burgerId := doc.Paths.Value.FindPath("/burgers/{burgerId}") burgerId := doc.Paths.Value.FindPath("/burgers/{burgerId}")
assert.NotNil(t, burgerId) assert.NotNil(t, burgerId)

View File

@@ -9,7 +9,7 @@ import (
) )
type Document struct { type Document struct {
Version low.NodeReference[string] Version low.ValueReference[string]
Info low.NodeReference[*Info] Info low.NodeReference[*Info]
Servers low.NodeReference[[]low.ValueReference[*Server]] Servers low.NodeReference[[]low.ValueReference[*Server]]
Paths low.NodeReference[*Paths] Paths low.NodeReference[*Paths]