Skip to content

Commit

Permalink
feat(schema): add helper functions for check and transform schema type (
Browse files Browse the repository at this point in the history
#47)

* feat(schema): add helper functions for check and transform to schema type

* add testing
  • Loading branch information
easonlin404 authored Feb 22, 2018
1 parent 9345ad4 commit c6c1ad9
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
app := cli.NewApp()
app.Version = "v1.0.0"
app.Version = "v1.1.1"
app.Usage = "Automatically generate RESTful API documentation with Swagger 2.0 for Go."

app.Commands = []cli.Command{
Expand Down
29 changes: 23 additions & 6 deletions example/simple/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
// 2018-02-21 07:37:24.610464 +0800 CST m=+0.064445612
// 2018-02-22 11:19:48.162718 +0800 CST m=+0.048215406

package docs

Expand Down Expand Up @@ -85,7 +85,7 @@ var doc = `{
"operationId": "get-string-by-int",
"parameters": [
{
"type": "int",
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "path",
Expand Down Expand Up @@ -145,14 +145,14 @@ var doc = `{
"required": true
},
{
"type": "int",
"type": "integer",
"description": "Offset",
"name": "offset",
"in": "query",
"required": true
},
{
"type": "int",
"type": "integer",
"description": "Offset",
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -213,6 +213,12 @@ var doc = `{
},
"Status": {
"type": "string"
},
"Tags": {
"type": "array",
"items": {
"$ref": "#/definitions/web.Tag"
}
}
}
},
Expand All @@ -223,10 +229,21 @@ var doc = `{
"type": "integer"
},
"Err": {
"type": "int32"
"type": "integer"
},
"Status": {
"type": "bool"
"type": "boolean"
}
}
},
"web.Tag": {
"type": "object",
"properties": {
"ID": {
"type": "integer"
},
"Name": {
"type": "string"
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions example/simple/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ type Pet struct {
} `json:"category"`
Name string `json:"name"`
PhotoUrls []string `json:"photoUrls"`
Tags []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"tags"`
Status string `json:"status"`
Tags []Tag `json:"tags"`
Status string `json:"status"`
}

type Tag struct {
ID int `json:"id"`
Name string `json:"name"`
}

type Pet2 struct {
Expand Down
2 changes: 1 addition & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (operation *Operation) ParseParamComment(commentLine string) error {
//five possible parameter types.
switch paramType {
case "query", "path", "header":
param = createParameter(paramType, description, name, schemaType, required)
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

Expand Down
4 changes: 2 additions & 2 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestParseParamCommentByPathType(t *testing.T) {
expected := `{
"parameters": [
{
"type": "int",
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "path",
Expand All @@ -251,7 +251,7 @@ func TestParseParamCommentByQueryType(t *testing.T) {
expected := `{
"parameters": [
{
"type": "int",
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "query",
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field, prop

func (parser *Parser) parseField(field *ast.Field) (propName, schemaType string, arrayType string) {
schType, arrType := getPropertyName(field)
CheckSchemaType(schType)
return field.Names[0].Name, schType, arrType
}

Expand Down
27 changes: 22 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestParseSimpleApi(t *testing.T) {
"operationId": "get-string-by-int",
"parameters": [
{
"type": "int",
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "path",
Expand Down Expand Up @@ -223,14 +223,14 @@ func TestParseSimpleApi(t *testing.T) {
"required": true
},
{
"type": "int",
"type": "integer",
"description": "Offset",
"name": "offset",
"in": "query",
"required": true
},
{
"type": "int",
"type": "integer",
"description": "Offset",
"name": "limit",
"in": "query",
Expand Down Expand Up @@ -291,6 +291,12 @@ func TestParseSimpleApi(t *testing.T) {
},
"Status": {
"type": "string"
},
"Tags": {
"type": "array",
"items": {
"$ref": "#/definitions/web.Tag"
}
}
}
},
Expand All @@ -301,10 +307,21 @@ func TestParseSimpleApi(t *testing.T) {
"type": "integer"
},
"Err": {
"type": "int32"
"type": "integer"
},
"Status": {
"type": "bool"
"type": "boolean"
}
}
},
"web.Tag": {
"type": "object",
"properties": {
"ID": {
"type": "integer"
},
"Name": {
"type": "string"
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"go/ast"
"log"
"strings"
)

// getPropertyName returns the string value for the given field if it exists, otherwise it panics.
Expand All @@ -28,13 +27,9 @@ func getPropertyName(field *ast.Field) (name string, fieldType string) {
name = astTypeIdent.Name

// When its the int type will transfer to integer which is goswagger supported type
if "int" == name {
return "integer", "integer"
}
// Transfer float32 and float64 to number type
if strings.HasPrefix(name, "float") {
return "number", "integer"
}
schemeType:=TransToValidSchemeType(name)
return schemeType,schemeType


} else if _, ok := field.Type.(*ast.StarExpr); ok {
panic("not supported astStarExpr yet.")
Expand Down
33 changes: 33 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package swag

import "fmt"

func CheckSchemaType(typeName string) {
switch typeName {
case "string", "number", "integer", "boolean", "array", "object":

default:
panic(fmt.Errorf("%s is not basic types", typeName))
return
}

}

func TransToValidSchemeType(typeName string) string {
switch typeName {
case "uint", "int", "uint8", "int8", "uint16", "int16", "byte":
return "integer"
case "uint32", "int32", "rune":
return "integer"
case "uint64", "int64":
return "integer"
case "float32", "float64":
return "number"
case "bool":
return "boolean"
case "string":
return "string"
default:
panic(fmt.Errorf("%s is not valid go basic types", typeName))
}
}
44 changes: 44 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package swag

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestValidDataType(t *testing.T) {
assert.NotPanics(t, func() {
CheckSchemaType("string")
})
assert.NotPanics(t, func() {
CheckSchemaType("number")
})
assert.NotPanics(t, func() {
CheckSchemaType("integer")
})
assert.NotPanics(t, func() {
CheckSchemaType("boolean")
})
assert.NotPanics(t, func() {
CheckSchemaType("array")
})
assert.NotPanics(t, func() {
CheckSchemaType("object")
})

assert.Panics(t, func() {
CheckSchemaType("oops")
})
}

func TestTransToValidSchemeType(t *testing.T) {
assert.Equal(t, TransToValidSchemeType("uint"), "integer")
assert.Equal(t, TransToValidSchemeType("uint32"), "integer")
assert.Equal(t, TransToValidSchemeType("uint64"), "integer")
assert.Equal(t, TransToValidSchemeType("float32"), "number")
assert.Equal(t, TransToValidSchemeType("bool"), "boolean")
assert.Equal(t, TransToValidSchemeType("string"), "string")

assert.Panics(t, func() {
TransToValidSchemeType("oops")
})
}

0 comments on commit c6c1ad9

Please sign in to comment.