Skip to content

Commit

Permalink
refactor: extract schema logic (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
pei0804 authored and easonlin404 committed Mar 22, 2019
1 parent 889705a commit d21dc4f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist
testdata/simple/docs
testdata/simple*/docs
cover.out

# Test binary, build with `go test -c`
Expand Down
127 changes: 50 additions & 77 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,43 +143,11 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
param = createParameter(paramType, description, name, TransToValidSchemeType(schemaType), required)
case "body":
param = createParameter(paramType, description, name, "object", required) // TODO: if Parameter types can be objects, but also primitives and arrays
// TODO: this snippets have to extract out
refSplit := strings.Split(schemaType, ".")
if len(refSplit) == 2 {
pkgName := refSplit[0]
typeName := refSplit[1]
if typeSpec, ok := operation.parser.TypeDefinitions[pkgName][typeName]; ok {
operation.parser.registerTypes[schemaType] = typeSpec
} else {
var typeSpec *ast.TypeSpec
if astFile != nil {
for _, imp := range astFile.Imports {
if imp.Name != nil && imp.Name.Name == pkgName { // the import had an alias that matched
break
}
impPath := strings.Replace(imp.Path.Value, `"`, ``, -1)
if strings.HasSuffix(impPath, "/"+pkgName) {
var err error
typeSpec, err = findTypeDef(impPath, typeName)
if err != nil {
return errors.Wrapf(err, "can not find ref type: %q", schemaType)
}
break
}
}
}

if typeSpec == nil {
return fmt.Errorf("can not find ref type:\"%s\"", schemaType)
}

operation.parser.TypeDefinitions[pkgName][typeName] = typeSpec
operation.parser.registerTypes[schemaType] = typeSpec

}
param.Schema.Ref = spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + schemaType),
}
if err := operation.registerSchemaType(schemaType, astFile); err != nil {
return err
}
param.Schema.Ref = spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + schemaType),
}
case "formData":
param = createParameter(paramType, description, name, TransToValidSchemeType(schemaType), required)
Expand All @@ -194,6 +162,49 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
return nil
}

func (operation *Operation) registerSchemaType(schemaType string, astFile *ast.File) error {
refSplit := strings.Split(schemaType, ".")
if len(refSplit) != 2 {
return nil
}
pkgName := refSplit[0]
typeName := refSplit[1]
if typeSpec, ok := operation.parser.TypeDefinitions[pkgName][typeName]; ok {
operation.parser.registerTypes[schemaType] = typeSpec
return nil
}
var typeSpec *ast.TypeSpec
if astFile == nil {
return fmt.Errorf("can not register schema type: %q reason: astFile == nil", schemaType)
}
for _, imp := range astFile.Imports {
if imp.Name != nil && imp.Name.Name == pkgName { // the import had an alias that matched
break
}
impPath := strings.Replace(imp.Path.Value, `"`, ``, -1)
if strings.HasSuffix(impPath, "/"+pkgName) {
var err error
typeSpec, err = findTypeDef(impPath, typeName)
if err != nil {
return errors.Wrapf(err, "can not find type def: %q", schemaType)
}
break
}
}

if typeSpec == nil {
return fmt.Errorf("can not find schema type: %q", schemaType)
}

if _, ok := operation.parser.TypeDefinitions[pkgName]; !ok {
operation.parser.TypeDefinitions[pkgName] = make(map[string]*ast.TypeSpec)
}

operation.parser.TypeDefinitions[pkgName][typeName] = typeSpec
operation.parser.registerTypes[schemaType] = typeSpec
return nil
}

var regexAttributes = map[string]*regexp.Regexp{
// for Enums(A, B)
"enums": regexp.MustCompile(`(?i)enums\(.*\)`),
Expand Down Expand Up @@ -491,46 +502,8 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
refType := matches[3]

if operation.parser != nil { // checking refType has existing in 'TypeDefinitions'
refSplit := strings.Split(refType, ".")
if len(refSplit) == 2 {
pkgName := refSplit[0]
typeName := refSplit[1]

if typeSpec, ok := operation.parser.TypeDefinitions[pkgName][typeName]; ok {
operation.parser.registerTypes[refType] = typeSpec
} else {
var typeSpec *ast.TypeSpec
if astFile != nil {
for _, imp := range astFile.Imports {
if imp.Name != nil && imp.Name.Name == pkgName { // the import had an alias that matched
break
}
impPath := strings.Replace(imp.Path.Value, `"`, ``, -1)

if strings.HasSuffix(impPath, "/"+pkgName) {
var err error

typeSpec, err = findTypeDef(impPath, typeName)
if err != nil {
return errors.Wrapf(err, "can not find ref type: %q", refType)
}
break
}
}
}

if typeSpec == nil {
return fmt.Errorf("can not find ref type: %q", refType)
}

if _, ok := operation.parser.TypeDefinitions[pkgName]; !ok {
operation.parser.TypeDefinitions[pkgName] = make(map[string]*ast.TypeSpec)

}
operation.parser.TypeDefinitions[pkgName][typeName] = typeSpec
operation.parser.registerTypes[refType] = typeSpec
}

if err := operation.registerSchemaType(refType, astFile); err != nil {
return err
}
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/simple2/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-02-12 17:10:08.661139858 +0800 CST m=+0.038166509
// 2019-03-21 18:50:45.048401 +0900 JST m=+0.038855337

package docs

Expand Down
2 changes: 1 addition & 1 deletion testdata/simple3/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-02-12 17:10:08.668996207 +0800 CST m=+0.046022623
// 2019-03-21 18:50:45.056315 +0900 JST m=+0.046769114

package docs

Expand Down

0 comments on commit d21dc4f

Please sign in to comment.