Skip to content

Commit

Permalink
Sub-collection of objects now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
macinnir committed Oct 13, 2017
1 parent fcc85c0 commit 44257e1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
28 changes: 27 additions & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,49 @@ func GetModels(lines []string, filePath string) (models map[string]Model, err er

fieldLineParts := strings.Fields(strings.TrimPrefix(lines[currentLine], " "))
fieldType := ""
isArray := false

if fieldLineParts[1][0:2] == "[]" {
fieldLineParts[1] = fieldLineParts[1][2:]
isArray = true
}

if strings.Contains(fieldLineParts[1], ".") {
fieldParts := strings.Split(fieldLineParts[1], ".")
fieldLineParts[1] = fieldParts[len(fieldParts)-1]
}

fieldRef := ""
// float, int
switch {
case len(fieldLineParts[1]) > 4 && fieldLineParts[1][0:5] == "float":
fieldType = "number"
case fieldLineParts[1][0:3] == "int":
fieldType = "integer"
default:
case fieldLineParts[1] == "string":
fieldType = "string"
default:
if isArray == true {
fieldType = "array"
fieldRef = fieldLineParts[1]
} else {
fieldType = "#object"
fieldRef = fieldLineParts[1]
}
}

// fmt.Printf("FieldType: %s\n", fieldType)

field := ModelField{
fieldLineParts[0],
fieldType,
fieldRef,
}
currentLine = currentLine + 1
model.Fields = append(model.Fields, field)
}
models[model.Name] = model

}

return
Expand Down
11 changes: 7 additions & 4 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type Model struct {
type ModelField struct {
Name string
Type string
Ref string
}

type Config struct {
Expand All @@ -156,8 +157,10 @@ type ModelDefinition struct {
}

type Property struct {
Type string `json:"type"`
Format string `json:"format,omitempty"`
Enum []string `json:"enum,omitempty"`
Default interface{} `json:"default,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Enum []string `json:"enum,omitempty"`
Default interface{} `json:"default,omitempty"`
Items map[string]string `json:"items,omitempty"`
Ref string `json:"$ref,omitempty"`
}
30 changes: 23 additions & 7 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (s *Swaggerf) BuildSwagger(rootPath string) {
for name, model := range models {
allModels[name] = model
}

// for arrayModelName, modelName := range arrayModels {
// if _, ok := allArrayModels[arrayModelName]; !ok {
// allArrayModels[arrayModelName] = modelName
// }
// }
}

// Definitions (Models)
Expand All @@ -71,6 +77,20 @@ func (s *Swaggerf) BuildSwagger(rootPath string) {
property := Property{}
property.Type = field.Type

if field.Type == "array" {

property.Type = "array"
property.Items = map[string]string{}

// TODO this does not support a simple array of strings
// if len(field.Ref) > 0 {
property.Items["$ref"] = "#/definitions/" + field.Ref
// }

} else if field.Type == "#object" {
property.Ref = "#/definitions/" + field.Ref
}

definition.Properties[field.Name] = property
}

Expand Down Expand Up @@ -98,28 +118,24 @@ func (s *Swaggerf) BuildSwagger(rootPath string) {
parameter := Parameter{}
paramType := param.Type

// Currently ignoring array of objects as input. Possibly this would just be another model that encapsulates the array of data?
if param.Type[0:2] == "[]" {
paramType = param.Type[2:]
}

parameter.In = param.In
parameter.Name = param.Name
parameter.Description = param.Description

// Check if the return type is a known model
if _, ok := s.Swagger.Definitions[paramType]; ok {

parameter.Schema = map[string]string{}
parameter.Schema["$ref"] = "#/definitions/" + paramType

} else {
parameter.Required = param.Required
parameter.Schema = map[string]string{}
parameter.Type = paramType
}
path.Parameters = append(path.Parameters, parameter)

}

// Responses
path.Responses = map[string]PathResponse{}
for _, response := range route.Responses {
pr := PathResponse{}
Expand Down

0 comments on commit 44257e1

Please sign in to comment.