Skip to content

Commit

Permalink
refactor: added some error checks (#411)
Browse files Browse the repository at this point in the history
* Added some error checks

* Further improved error handling

* Simplified some if checks

* go fmt'ed
  • Loading branch information
Nerzal authored and easonlin404 committed Jun 12, 2019
1 parent b534de4 commit 6ee61d7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 41 deletions.
13 changes: 10 additions & 3 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func (g *Gen) Build(config *Config) error {
return err
}

os.MkdirAll(config.OutputDir, os.ModePerm)
if err := os.MkdirAll(config.OutputDir, os.ModePerm); err != nil {
return err
}

docs, err := os.Create(path.Join(config.OutputDir, "docs.go"))
if err != nil {
return err
Expand All @@ -75,7 +78,9 @@ func (g *Gen) Build(config *Config) error {
}

defer swaggerJSON.Close()
swaggerJSON.Write(b)
if _, err := swaggerJSON.Write(b); err != nil {
return err
}

swaggerYAML, err := os.Create(path.Join(config.OutputDir, "swagger.yaml"))
if err != nil {
Expand All @@ -88,7 +93,9 @@ func (g *Gen) Build(config *Config) error {
return errors.Wrap(err, "cannot covert json to yaml")
}

swaggerYAML.Write(y)
if _, err := swaggerYAML.Write(y); err != nil {
return err
}

if err := packageTemplate.Execute(docs, struct {
Timestamp time.Time
Expand Down
22 changes: 16 additions & 6 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func TestParseProduceComment(t *testing.T) {
}`
comment := `/@Produce json,xml,plain,html,mpfd,x-www-form-urlencoded,json-api,json-stream,octet-stream,png,jpeg,gif,application/health+json`
operation := new(Operation)
operation.ParseComment(comment, nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
assert.JSONEq(t, expected, string(b))
}
Expand Down Expand Up @@ -202,7 +203,8 @@ func TestParseResponseCommentWithArrayType(t *testing.T) {
func TestParseResponseCommentWithBasicType(t *testing.T) {
comment := `@Success 200 {string} string "it's ok'"`
operation := NewOperation()
operation.ParseComment(comment, nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")

expected := `{
Expand All @@ -221,7 +223,9 @@ func TestParseResponseCommentWithBasicType(t *testing.T) {
func TestParseEmptyResponseComment(t *testing.T) {
comment := `@Success 200 "it's ok"`
operation := NewOperation()
operation.ParseComment(comment, nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")

b, _ := json.MarshalIndent(operation, "", " ")

expected := `{
Expand All @@ -237,9 +241,13 @@ func TestParseEmptyResponseComment(t *testing.T) {
func TestParseResponseCommentWithHeader(t *testing.T) {
comment := `@Success 200 "it's ok"`
operation := NewOperation()
operation.ParseComment(comment, nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")

comment = `@Header 200 {string} Token "qwerty"`
operation.ParseComment(comment, nil)
err = operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")

b, err := json.MarshalIndent(operation, "", " ")
assert.NoError(t, err)

Expand All @@ -262,7 +270,9 @@ func TestParseResponseCommentWithHeader(t *testing.T) {
func TestParseEmptyResponseOnlyCode(t *testing.T) {
comment := `@Success 200`
operation := NewOperation()
operation.ParseComment(comment, nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")

b, _ := json.MarshalIndent(operation, "", " ")

expected := `{
Expand Down
27 changes: 19 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (parser *Parser) ParseAPI(searchDir string, mainAPIFile string) error {
if err := parser.getAllGoFileInfo(searchDir); err != nil {
return err
}
parser.ParseGeneralAPIInfo(path.Join(searchDir, mainAPIFile))

if err := parser.ParseGeneralAPIInfo(path.Join(searchDir, mainAPIFile)); err != nil {
return err
}

for _, astFile := range parser.files {
parser.ParseType(astFile)
Expand Down Expand Up @@ -368,13 +371,13 @@ func getScopeScheme(scope string) (string, error) {
func isExistsScope(scope string) (bool, error) {
s := strings.Fields(scope)
for _, v := range s {
if strings.Index(v, "@scope.") != -1 {
if strings.Index(v, ",") != -1 {
if strings.Contains(v, "@scope.") {
if strings.Contains(v, ",") {
return false, fmt.Errorf("@scope can't use comma(,) get=" + v)
}
}
}
return strings.Index(scope, "@scope.") != -1, nil
return strings.Contains(scope, "@scope."), nil
}

// getSchemes parses swagger schemes for given commentLine
Expand Down Expand Up @@ -899,9 +902,13 @@ func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
return nil, err
}
if len(prop.ArrayType) == 0 {
CheckSchemaType(prop.SchemaType)
if err := CheckSchemaType(prop.SchemaType); err != nil {
return nil, err
}
} else {
CheckSchemaType("array")
if err := CheckSchemaType("array"); err != nil {
return nil, err
}
}
structField := &structField{
name: field.Names[0].Name,
Expand Down Expand Up @@ -956,8 +963,12 @@ func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
}
}

CheckSchemaType(newSchemaType)
CheckSchemaType(newArrayType)
if err := CheckSchemaType(newSchemaType); err != nil {
return nil, err
}
if err := CheckSchemaType(newArrayType); err != nil {
return nil, err
}
structField.schemaType = newSchemaType
structField.arrayType = newArrayType
}
Expand Down
63 changes: 42 additions & 21 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
gopath := os.Getenv("GOPATH")
assert.NotNil(t, gopath)
p := New()
p.ParseGeneralAPIInfo("testdata/main.go")
err := p.ParseGeneralAPIInfo("testdata/main.go")
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand Down Expand Up @@ -172,7 +173,8 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) {
gopath := os.Getenv("GOPATH")
assert.NotNil(t, gopath)
p := New()
p.ParseGeneralAPIInfo("testdata/templated.go")
err := p.ParseGeneralAPIInfo("testdata/templated.go")
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand All @@ -197,7 +199,8 @@ func TestParser_ParseGeneralApiInfoWithOpsInSameFile(t *testing.T) {
gopath := os.Getenv("GOPATH")
assert.NotNil(t, gopath)
p := New()
p.ParseGeneralAPIInfo("testdata/single_file_api/main.go")
err := p.ParseGeneralAPIInfo("testdata/single_file_api/main.go")
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand All @@ -214,8 +217,9 @@ func TestGetAllGoFileInfo(t *testing.T) {
searchDir := "testdata/pet"

p := New()
p.getAllGoFileInfo(searchDir)
err := p.getAllGoFileInfo(searchDir)

assert.NoError(t, err)
assert.NotEmpty(t, p.files["testdata/pet/main.go"])
assert.NotEmpty(t, p.files["testdata/pet/web/handler.go"])
assert.Equal(t, 2, len(p.files))
Expand All @@ -225,7 +229,8 @@ func TestParser_ParseType(t *testing.T) {
searchDir := "testdata/simple/"

p := New()
p.getAllGoFileInfo(searchDir)
err := p.getAllGoFileInfo(searchDir)
assert.NoError(t, err)

for _, file := range p.files {
p.ParseType(file)
Expand Down Expand Up @@ -877,7 +882,9 @@ func TestParseSimpleApi1(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = PascalCase
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -1365,7 +1372,9 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = SnakeCase
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -1825,7 +1834,9 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
searchDir := "testdata/simple3"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -1935,7 +1946,8 @@ func TestParseStructComment(t *testing.T) {
searchDir := "testdata/struct_comment"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -1967,8 +1979,8 @@ func TestParsePetApi(t *testing.T) {
searchDir := "testdata/pet"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)

err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -2121,8 +2133,8 @@ func TestParseModelNotUnderRoot(t *testing.T) {
searchDir := "testdata/model_not_under_root/cmd"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)

err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}
Expand Down Expand Up @@ -2191,7 +2203,8 @@ func TestParseModelAsTypeAlias(t *testing.T) {
searchDir := "testdata/alias_type"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
Expand All @@ -2201,7 +2214,8 @@ func TestParseComposition(t *testing.T) {
searchDir := "testdata/composition"
mainAPIFile := "main.go"
p := New()
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

expected, err := ioutil.ReadFile(path.Join(searchDir, "expected.json"))
assert.NoError(t, err)
Expand Down Expand Up @@ -2422,7 +2436,8 @@ func Test3(){

func TestSkip(t *testing.T) {
folder1 := "/tmp/vendor"
os.Mkdir(folder1, os.ModePerm)
err := os.Mkdir(folder1, os.ModePerm)
assert.NoError(t, err)
f1, _ := os.Stat(folder1)

parser := New()
Expand All @@ -2431,7 +2446,8 @@ func TestSkip(t *testing.T) {
assert.NoError(t, os.Remove(folder1))

folder2 := "/tmp/.git"
os.Mkdir(folder2, os.ModePerm)
err = os.Mkdir(folder2, os.ModePerm)
assert.NoError(t, err)
f2, _ := os.Stat(folder2)

assert.True(t, parser.Skip(folder2, f2) == filepath.SkipDir)
Expand All @@ -2444,7 +2460,9 @@ func TestSkip(t *testing.T) {

func TestSkipMustParseVendor(t *testing.T) {
folder1 := "/tmp/vendor"
os.Mkdir(folder1, os.ModePerm)
err := os.Mkdir(folder1, os.ModePerm)
assert.NoError(t, err)

f1, _ := os.Stat(folder1)

parser := New()
Expand All @@ -2454,7 +2472,9 @@ func TestSkipMustParseVendor(t *testing.T) {
assert.NoError(t, os.Remove(folder1))

folder2 := "/tmp/.git"
os.Mkdir(folder2, os.ModePerm)
err = os.Mkdir(folder2, os.ModePerm)
assert.NoError(t, err)

f2, _ := os.Stat(folder2)

assert.True(t, parser.Skip(folder2, f2) == filepath.SkipDir)
Expand Down Expand Up @@ -2485,7 +2505,7 @@ func TestSkipMustParseVendor(t *testing.T) {
// for i := 0; i < 100; i++ {
// p := New()
// p.PropNamingStrategy = PascalCase
// p.ParseAPI(searchDir, mainAPIFile)
// err := p.ParseAPI(searchDir, mainAPIFile)
// b, _ := json.MarshalIndent(p.swagger, "", " ")
// assert.NotEqual(t, "", string(b))

Expand All @@ -2504,7 +2524,8 @@ func TestApiParseTag(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = PascalCase
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

if len(p.swagger.Tags) != 2 {
t.Log("Number of tags did not match")
Expand Down
3 changes: 2 additions & 1 deletion property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ func TestParseTag(t *testing.T) {
mainAPIFile := "main.go"
p := New()
p.PropNamingStrategy = PascalCase
p.ParseAPI(searchDir, mainAPIFile)
err := p.ParseAPI(searchDir, mainAPIFile)
assert.NoError(t, err)

if len(p.swagger.Tags) != 2 {
t.Log("Number of tags did not match")
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-03-21 18:50:45.048401 +0900 JST m=+0.038855337
// 2019-06-12 13:12:22.266776997 +0200 CEST m=+0.119881542

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-03-21 18:50:45.056315 +0900 JST m=+0.046769114
// 2019-06-12 13:12:22.317087441 +0200 CEST m=+0.170192113

package docs

Expand Down

0 comments on commit 6ee61d7

Please sign in to comment.