diff --git a/.gitignore b/.gitignore index b24be87c2..ed0ff06e5 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,6 @@ cover.out /swag.exe cmd/swag/docs/* -.vscode/launch.json \ No newline at end of file +.vscode/launch.json + +vendor/ \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 000000000..63e9a5010 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,10 @@ +linters: + disable-all: true + enable: + - errcheck + - govet + - typecheck + - unused + - goimports + - misspell + fast: true \ No newline at end of file diff --git a/Makefile b/Makefile index 089c65be6..0e65458e2 100644 --- a/Makefile +++ b/Makefile @@ -64,12 +64,11 @@ deps: .PHONY: devel-deps devel-deps: - GO111MODULE=off $(GOGET) -v -u \ - golang.org/x/lint/golint + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.48 .PHONY: lint lint: devel-deps - for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done; + golangci-lint run . .PHONY: vet vet: deps devel-deps diff --git a/field_parserv3.go b/field_parserv3.go index 733907f4b..c9d4c6d40 100644 --- a/field_parserv3.go +++ b/field_parserv3.go @@ -380,48 +380,6 @@ func getIntTagV3(structTag reflect.StructTag, tagName string) (*int, error) { return &value, nil } -func parseValidTagsV3(validTag string, sf *structFieldV3) { - - // `validate:"required,max=10,min=1"` - // ps. required checked by IsRequired(). - for _, val := range strings.Split(validTag, ",") { - var ( - valValue string - keyVal = strings.Split(val, "=") - ) - - switch len(keyVal) { - case 1: - case 2: - valValue = strings.ReplaceAll(strings.ReplaceAll(keyVal[1], utf8HexComma, ","), utf8Pipe, "|") - default: - continue - } - - switch keyVal[0] { - case "max", "lte": - sf.setMax(valValue) - case "min", "gte": - sf.setMin(valValue) - case "oneof": - if strings.Contains(validTag, "swaggerIgnore") { - continue - } - - sf.setOneOf(valValue) - case "unique": - if sf.schemaType == ARRAY { - sf.unique = true - } - case "dive": - // ignore dive - return - default: - continue - } - } -} - func (sf *structFieldV3) parseValidTags(validTag string) { // `validate:"required,max=10,min=1"` diff --git a/generics_test.go b/generics_test.go index bd79f93de..c2289ad2e 100644 --- a/generics_test.go +++ b/generics_test.go @@ -95,9 +95,10 @@ func TestParseGenericsProperty(t *testing.T) { p := New() err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth) + assert.Empty(t, err) assert.NoError(t, err) b, err := json.MarshalIndent(p.swagger, "", " ") - os.WriteFile(searchDir+"/expected.json", b, fs.ModePerm) + err = os.WriteFile(searchDir+"/expected.json", b, fs.ModePerm) assert.NoError(t, err) assert.Equal(t, string(expected), string(b)) } diff --git a/operation_test.go b/operation_test.go index 87bcae9ce..96ea3285a 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1870,17 +1870,20 @@ func TestParseParamCommentByExampleUnsupportedType(t *testing.T) { t.Parallel() var param spec.Parameter - setExample(¶m, "something", "random value") + err := setExample(¶m, "something", "random value") assert.Equal(t, param.Example, nil) - setExample(¶m, STRING, "string value") + err = setExample(¶m, STRING, "string value") assert.Equal(t, param.Example, "string value") + assert.Empty(t, err) - setExample(¶m, INTEGER, "10") + err = setExample(¶m, INTEGER, "10") assert.Equal(t, param.Example, 10) + assert.Empty(t, err) - setExample(¶m, NUMBER, "10") + err = setExample(¶m, NUMBER, "10") assert.Equal(t, param.Example, float64(10)) + assert.Empty(t, err) } func TestParseParamCommentBySchemaExampleString(t *testing.T) { @@ -1911,24 +1914,30 @@ func TestParseParamCommentBySchemaExampleUnsupportedType(t *testing.T) { t.Parallel() var param spec.Parameter - setSchemaExample(¶m, "something", "random value") + err := setSchemaExample(¶m, "something", "random value") assert.Nil(t, param.Schema) + assert.Empty(t, err) - setSchemaExample(¶m, STRING, "string value") + err = setSchemaExample(¶m, STRING, "string value") assert.Nil(t, param.Schema) + assert.Empty(t, err) param.Schema = &spec.Schema{} - setSchemaExample(¶m, STRING, "string value") + err = setSchemaExample(¶m, STRING, "string value") assert.Equal(t, "string value", param.Schema.Example) + assert.Empty(t, err) - setSchemaExample(¶m, INTEGER, "10") + err = setSchemaExample(¶m, INTEGER, "10") assert.Equal(t, 10, param.Schema.Example) + assert.Empty(t, err) - setSchemaExample(¶m, NUMBER, "10") + err = setSchemaExample(¶m, NUMBER, "10") assert.Equal(t, float64(10), param.Schema.Example) + assert.Empty(t, err) - setSchemaExample(¶m, STRING, "string \\r\\nvalue") + err = setSchemaExample(¶m, STRING, "string \\r\\nvalue") assert.Equal(t, "string \r\nvalue", param.Schema.Example) + assert.Empty(t, err) } func TestParseParamArrayWithEnums(t *testing.T) { diff --git a/operationv3.go b/operationv3.go index 4737694e1..91810348d 100644 --- a/operationv3.go +++ b/operationv3.go @@ -602,17 +602,6 @@ func setSchemaExampleV3(param *spec.Schema, schemaType string, value string) err return nil } -func setExampleParameterV3(param *spec.Parameter, schemaType string, value string) error { - val, err := defineType(schemaType, value) - if err != nil { - return nil // Don't set a example value if it's not valid - } - - param.Example = val - - return nil -} - func setStringParamV3(param *spec.Schema, name, schemaType, attr, commentLine string) error { if schemaType != STRING { return fmt.Errorf("%s is attribute to set to a number. comment=%s got=%s", name, commentLine, schemaType) diff --git a/operationv3_test.go b/operationv3_test.go index 8408aa34f..041943187 100644 --- a/operationv3_test.go +++ b/operationv3_test.go @@ -1571,24 +1571,30 @@ func TestParseParamCommentBySchemaExampleUnsupportedTypeV3(t *testing.T) { t.Parallel() var param spec.Parameter - setSchemaExampleV3(nil, "something", "random value") + err := setSchemaExampleV3(nil, "something", "random value") assert.Nil(t, param.Schema) + assert.Empty(t, err) - setSchemaExampleV3(nil, STRING, "string value") + err = setSchemaExampleV3(nil, STRING, "string value") assert.Nil(t, param.Schema) + assert.Empty(t, err) param.Schema = spec.NewSchemaSpec() - setSchemaExampleV3(param.Schema.Spec, STRING, "string value") + err = setSchemaExampleV3(param.Schema.Spec, STRING, "string value") assert.Equal(t, "string value", param.Schema.Spec.Example) + assert.Empty(t, err) - setSchemaExampleV3(param.Schema.Spec, INTEGER, "10") + err = setSchemaExampleV3(param.Schema.Spec, INTEGER, "10") assert.Equal(t, 10, param.Schema.Spec.Example) + assert.Empty(t, err) - setSchemaExampleV3(param.Schema.Spec, NUMBER, "10") + err = setSchemaExampleV3(param.Schema.Spec, NUMBER, "10") assert.Equal(t, float64(10), param.Schema.Spec.Example) + assert.Empty(t, err) - setSchemaExampleV3(param.Schema.Spec, STRING, "string \\r\\nvalue") + err = setSchemaExampleV3(param.Schema.Spec, STRING, "string \\r\\nvalue") assert.Equal(t, "string \r\nvalue", param.Schema.Spec.Example) + assert.Empty(t, err) } func TestParseParamArrayWithEnumsV3(t *testing.T) { diff --git a/packages.go b/packages.go index 4bf31b751..7afc11577 100644 --- a/packages.go +++ b/packages.go @@ -595,17 +595,3 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File return pkgDefs.parametrizeGenericType(file, typeDef, typeName) } - -func isAliasPkgName(file *ast.File, pkgName string) bool { - if file == nil && file.Imports == nil { - return false - } - - for _, pkg := range file.Imports { - if pkg.Name != nil && pkg.Name.Name == pkgName { - return true - } - } - - return false -} diff --git a/parser.go b/parser.go index 0f92110eb..898f11a7c 100644 --- a/parser.go +++ b/parser.go @@ -405,7 +405,7 @@ func (parser *Parser) ParseAPIMultiSearchDir(searchDirs []string, mainAPIFile st // Use 'go list' command instead of depth.Resolve() if parser.ParseDependency { - parser.parseDeps(absMainAPIFilePath, parseDepth) + _ = parser.parseDeps(absMainAPIFilePath, parseDepth) } err = parser.ParseGeneralAPIInfo(absMainAPIFilePath) @@ -922,7 +922,8 @@ func isExistsScope(scope string) (bool, error) { for _, v := range s { if strings.Contains(v, scopeAttrPrefix) { if strings.Contains(v, ",") { - return false, fmt.Errorf("@scope can't use comma(,) get=" + v) + + return false, errors.New("@scope can't use comma(,) get=" + v) } } } diff --git a/parser_test.go b/parser_test.go index 92cb0624d..3a2d6eedc 100644 --- a/parser_test.go +++ b/parser_test.go @@ -3729,14 +3729,9 @@ func TestGetFieldType(t *testing.T) { } func TestTryAddDescription(t *testing.T) { - type args struct { - spec *spec.SecurityScheme - extensions map[string]interface{} - } tests := []struct { name string lines []string - args args want *spec.SecurityScheme }{ { diff --git a/schema.go b/schema.go index b3a5b38c1..c78a43528 100644 --- a/schema.go +++ b/schema.go @@ -3,6 +3,7 @@ package swag import ( "errors" "fmt" + "github.com/go-openapi/spec" ) diff --git a/utils_test.go b/utils_test.go index 1c4d9953a..e957415e5 100644 --- a/utils_test.go +++ b/utils_test.go @@ -2,6 +2,7 @@ package swag import ( "github.com/stretchr/testify/assert" + "testing" )