Skip to content

Commit

Permalink
Fix make lint and make fmt-check
Browse files Browse the repository at this point in the history
  • Loading branch information
whunmr committed Mar 22, 2020
1 parent 2107ee2 commit e1b41ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
31 changes: 15 additions & 16 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,31 +612,30 @@ func findTypeDef(importPath, typeName string) (*ast.TypeSpec, error) {

var responsePattern = regexp.MustCompile(`([\d]+)[\s]+([\w\{\}]+)[\s]+([\w\-\.\/\{\}=\[\]]+)[^"]*(.*)?`)

type NestedField struct {
Name string
Type string
type nestedField struct {
Name string
Type string
IsArray bool

Ref spec.Ref
Ref spec.Ref
}

func (nested *NestedField) GetSchema() *spec.Schema{
func (nested *nestedField) getSchema() *spec.Schema {
if IsGolangPrimitiveType(nested.Type) {
return &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{nested.Type}}}
} else {
return &spec.Schema{SchemaProps: spec.SchemaProps{Ref: nested.Ref}}
}

return &spec.Schema{SchemaProps: spec.SchemaProps{Ref: nested.Ref}}
}

func (nested *NestedField) FillNestedSchema(response spec.Response, ref spec.Ref) {
func (nested *nestedField) fillNestedSchema(response spec.Response, ref spec.Ref) {
props := make(map[string]spec.Schema, 0)
if nested.IsArray {
props[nested.Name] = spec.Schema{SchemaProps: spec.SchemaProps{
Type: []string{"array"},
Items: &spec.SchemaOrArray{Schema: nested.GetSchema()},
Items: &spec.SchemaOrArray{Schema: nested.getSchema()},
}}
} else {
props[nested.Name] = *nested.GetSchema()
props[nested.Name] = *nested.getSchema()
}
nestedSpec := spec.Schema{
SchemaProps: spec.SchemaProps{
Expand All @@ -652,11 +651,11 @@ func (nested *NestedField) FillNestedSchema(response spec.Response, ref spec.Ref
var nestedObjectPattern = regexp.MustCompile(`^([\w\-\.\/]+)\{(.*)=([^\[\]]*)\}$`)
var nestedArrayPattern = regexp.MustCompile(`^([\w\-\.\/]+)\{(.*)=\[\]([^\[\]]*)\}$`)

func (operation *Operation)tryExtractNestedField(specStr string, astFile *ast.File) (refType string, nested *NestedField, err error) {
func (operation *Operation) tryExtractNestedField(specStr string, astFile *ast.File) (refType string, nested *nestedField, err error) {
if matches := nestedObjectPattern.FindStringSubmatch(specStr); len(matches) == 4 {
refType, nested = matches[1], &NestedField{Name: matches[2], Type: matches[3], IsArray: false}
refType, nested = matches[1], &nestedField{Name: matches[2], Type: matches[3], IsArray: false}
} else if matches := nestedArrayPattern.FindStringSubmatch(specStr); len(matches) == 4 {
refType, nested = matches[1], &NestedField{Name: matches[2], Type: matches[3], IsArray: true}
refType, nested = matches[1], &nestedField{Name: matches[2], Type: matches[3], IsArray: true}
} else {
return specStr, nil, nil
}
Expand Down Expand Up @@ -722,14 +721,14 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as

if schemaType == "object" {
response.Schema.SchemaProps = spec.SchemaProps{}
ref := spec.Ref {
ref := spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + TypeDocName(refType, typeSpec)),
}

if nested == nil {
response.Schema.Ref = ref
} else {
nested.FillNestedSchema(response, ref)
nested.fillNestedSchema(response, ref)
}

} else if schemaType == "array" {
Expand Down
2 changes: 0 additions & 2 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func TestParseResponseCommentWithNestedPrimitiveType(t *testing.T) {
assert.Equal(t, expected, string(b))
}


func TestParseResponseCommentWithNestedPrimitiveArrayType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=[]string} "Error message, if code != 200`
operation := NewOperation()
Expand Down Expand Up @@ -274,7 +273,6 @@ func TestParseResponseCommentWithNestedPrimitiveArrayType(t *testing.T) {
assert.Equal(t, expected, string(b))
}


func TestParseResponseCommentWithNestedObjectType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=model.Payload} "Error message, if code != 200`
operation := NewOperation()
Expand Down

0 comments on commit e1b41ed

Please sign in to comment.