Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from launchdarkly/raymond/ch102249/goas-map-sup…
Browse files Browse the repository at this point in the history
…port

support map types using additionalProperties
  • Loading branch information
Raymond Ho authored Mar 8, 2021
2 parents 6b15afd + 1b74958 commit defdc9d
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 180 deletions.
9 changes: 6 additions & 3 deletions example/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (

type FooResponse struct {
ID string `json:"id"`
Bar string `json:"bar"`
Baz string `json:"baz"`
startDate time.Time `json:"startDate"`
StartDate time.Time `json:"startDate"`
Msg json.RawMessage `json:"msg"`
InnerFoos []InnerFoo `json:"foo"`
Environments map[string]Environment `json:"environments"`
}

type Environment struct {
Name string `json:"name"`
}

// @Title Get all foos
Expand Down
18 changes: 9 additions & 9 deletions oas.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ type SchemaObject struct {
FieldName string `json:"-"` // For goas
DisabledFieldNames map[string]struct{} `json:"-"` // For goas

Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties *orderedmap.OrderedMap `json:"properties,omitempty"`
Description string `json:"description,omitempty"`
Items *SchemaObject `json:"items,omitempty"` // use ptr to prevent recursive error
Example interface{} `json:"example,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
Properties *orderedmap.OrderedMap `json:"properties,omitempty"`
AdditionalProperties *SchemaObject `json:"additionalProperties,omitempty"`
Description string `json:"description,omitempty"`
Items *SchemaObject `json:"items,omitempty"` // use ptr to prevent recursive error
Example interface{} `json:"example,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`

// Ref is used when SchemaObject is as a ReferenceObject
Ref string `json:"$ref,omitempty"`
Expand All @@ -167,7 +168,6 @@ type SchemaObject struct {
// OneOf
// AnyOf
// Not
// AdditionalProperties
// Description
// Default
// Nullable
Expand Down
12 changes: 5 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ func (p *parser) parse() error {
return nil
}


func (p *parser) CreateOASFile(path string) error {
if err := p.parse(); err != nil {
return err
Expand Down Expand Up @@ -1057,15 +1056,14 @@ func (p *parser) parseSchemaObject(pkgPath, pkgName, typeName string) (*SchemaOb
itemTypeName := typeName[5:]
schema, ok := p.KnownIDSchema[genSchemeaObjectID(pkgName, itemTypeName)]
if ok {
schemaObject.Items = &SchemaObject{Ref: addSchemaRefLinkPrefix(schema.ID)}
schemaObject.AdditionalProperties = &SchemaObject{Ref: addSchemaRefLinkPrefix(schema.ID)}
return &schemaObject, nil
}
schemaProperty, err := p.parseSchemaObject(pkgPath, pkgName, itemTypeName)
if err != nil {
return nil, err
}
schemaObject.Properties = orderedmap.New()
schemaObject.Properties.Set("key", schemaProperty)
schemaObject.AdditionalProperties = schemaProperty
return &schemaObject, nil
} else if typeName == "time.Time" {
schemaObject.Type = "string"
Expand Down Expand Up @@ -1138,7 +1136,7 @@ func (p *parser) parseSchemaObject(pkgPath, pkgName, typeName string) (*SchemaOb
return &schemaObject, nil
}

log.Fatalf("Cannot find definition of guess %s ast.TypeSpec in package %s. " +
log.Fatalf("Cannot find definition of guess %s ast.TypeSpec in package %s. "+
"If definition is in a vendor dependency, try running `go mod tidy && go mod vendor`",
guessTypeName, guessPkgName)
}
Expand Down Expand Up @@ -1503,7 +1501,7 @@ func (p *parser) debugf(format string, args ...interface{}) {
}
}

func sortedPackageKeys(m map[string]*ast.Package) ([]string) {
func sortedPackageKeys(m map[string]*ast.Package) []string {
keys := make([]string, len(m))
i := 0
for k, _ := range m {
Expand All @@ -1514,7 +1512,7 @@ func sortedPackageKeys(m map[string]*ast.Package) ([]string) {
return keys
}

func sortedFileKeys(m map[string]*ast.File) ([]string) {
func sortedFileKeys(m map[string]*ast.File) []string {
keys := make([]string, len(m))
i := 0
for k, _ := range m {
Expand Down
Loading

0 comments on commit defdc9d

Please sign in to comment.