Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: fix: linter build err #1905

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ cover.out
/swag.exe
cmd/swag/docs/*

.vscode/launch.json
.vscode/launch.json

vendor/
10 changes: 10 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
linters:
disable-all: true
enable:
- errcheck
- govet
- typecheck
- unused
- goimports
- misspell
fast: true
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]

.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
Expand Down
42 changes: 0 additions & 42 deletions field_parserv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
3 changes: 2 additions & 1 deletion generics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
29 changes: 19 additions & 10 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1870,17 +1870,20 @@ func TestParseParamCommentByExampleUnsupportedType(t *testing.T) {
t.Parallel()
var param spec.Parameter

setExample(&param, "something", "random value")
err := setExample(&param, "something", "random value")
assert.Equal(t, param.Example, nil)

setExample(&param, STRING, "string value")
err = setExample(&param, STRING, "string value")
assert.Equal(t, param.Example, "string value")
assert.Empty(t, err)

setExample(&param, INTEGER, "10")
err = setExample(&param, INTEGER, "10")
assert.Equal(t, param.Example, 10)
assert.Empty(t, err)

setExample(&param, NUMBER, "10")
err = setExample(&param, NUMBER, "10")
assert.Equal(t, param.Example, float64(10))
assert.Empty(t, err)
}

func TestParseParamCommentBySchemaExampleString(t *testing.T) {
Expand Down Expand Up @@ -1911,24 +1914,30 @@ func TestParseParamCommentBySchemaExampleUnsupportedType(t *testing.T) {
t.Parallel()
var param spec.Parameter

setSchemaExample(&param, "something", "random value")
err := setSchemaExample(&param, "something", "random value")
assert.Nil(t, param.Schema)
assert.Empty(t, err)

setSchemaExample(&param, STRING, "string value")
err = setSchemaExample(&param, STRING, "string value")
assert.Nil(t, param.Schema)
assert.Empty(t, err)

param.Schema = &spec.Schema{}
setSchemaExample(&param, STRING, "string value")
err = setSchemaExample(&param, STRING, "string value")
assert.Equal(t, "string value", param.Schema.Example)
assert.Empty(t, err)

setSchemaExample(&param, INTEGER, "10")
err = setSchemaExample(&param, INTEGER, "10")
assert.Equal(t, 10, param.Schema.Example)
assert.Empty(t, err)

setSchemaExample(&param, NUMBER, "10")
err = setSchemaExample(&param, NUMBER, "10")
assert.Equal(t, float64(10), param.Schema.Example)
assert.Empty(t, err)

setSchemaExample(&param, STRING, "string \\r\\nvalue")
err = setSchemaExample(&param, STRING, "string \\r\\nvalue")
assert.Equal(t, "string \r\nvalue", param.Schema.Example)
assert.Empty(t, err)
}

func TestParseParamArrayWithEnums(t *testing.T) {
Expand Down
11 changes: 0 additions & 11 deletions operationv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions operationv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 0 additions & 14 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
{
Expand Down
1 change: 1 addition & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package swag
import (
"errors"
"fmt"

"github.com/go-openapi/spec"
)

Expand Down
1 change: 1 addition & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swag

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

"testing"
)

Expand Down
Loading