From 744a58e926382a8d965f60ca02765b52db6efd8f Mon Sep 17 00:00:00 2001 From: sdghchj Date: Mon, 18 Dec 2023 16:21:34 +0800 Subject: [PATCH] Drop support for go v1.17.x (#1723) * Drop support for go v1.17.x Signed-off-by: sdghchj --- .github/workflows/ci.yml | 2 +- Makefile | 9 +---- README.md | 2 +- README_pt.md | 2 +- README_zh-CN.md | 2 +- const.go | 9 +++-- example/celler/go.mod | 2 +- example/go-module-support/go.mod | 2 +- example/markdown/go.mod | 2 +- example/object-map-example/go.mod | 2 +- generics_other.go | 42 ------------------- generics_other_test.go | 67 ------------------------------- utils_go18.go | 31 -------------- utils_other.go | 47 ---------------------- 14 files changed, 15 insertions(+), 206 deletions(-) delete mode 100644 generics_other.go delete mode 100644 generics_other_test.go delete mode 100644 utils_go18.go delete mode 100644 utils_other.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2ea781d7..9a847dae9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: test: strategy: matrix: - go: [ '1.17.x', '1.18.x', '1.19.x', '1.20.x' ] + go: [ '1.18.x', '1.19.x', '1.20.x', '1.21.x' ] platform: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/Makefile b/Makefile index 04e6514e9..0d8175da7 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ GOBUILD:=$(GOCMD) build GOINSTALL:=$(GOCMD) install GOCLEAN:=$(GOCMD) clean GOTEST:=$(GOCMD) test +GOMODTIDY:=$(GOCMD) mod tidy GOGET:=$(GOCMD) get GOLIST:=$(GOCMD) list GOVET:=$(GOCMD) vet @@ -54,13 +55,7 @@ clean: .PHONY: deps deps: - $(GOGET) github.com/swaggo/cli - $(GOGET) sigs.k8s.io/yaml - $(GOGET) github.com/KyleBanks/depth - $(GOGET) github.com/go-openapi/jsonreference - $(GOGET) github.com/go-openapi/spec - $(GOGET) github.com/stretchr/testify/assert - $(GOGET) golang.org/x/tools/go/loader + $(GOMODTIDY) .PHONY: devel-deps devel-deps: diff --git a/README.md b/README.md index 05462671c..802016240 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie ```sh go install github.com/swaggo/swag/cmd/swag@latest ``` -To build from source you need [Go](https://golang.org/dl/) (1.17 or newer). +To build from source you need [Go](https://golang.org/dl/) (1.18 or newer). Alternatively you can run the docker image: ```sh diff --git a/README_pt.md b/README_pt.md index d6d005904..7f95066bb 100644 --- a/README_pt.md +++ b/README_pt.md @@ -54,7 +54,7 @@ Swag converte anotações Go para Documentação Swagger 2.0. Criámos uma varie ```sh go install github.com/swaggo/swag/cmd/swag@latest ``` -Para construir a partir da fonte é necessário [Go](https://golang.org/dl/) (1.17 ou mais recente). +Para construir a partir da fonte é necessário [Go](https://golang.org/dl/) (1.18 ou mais recente). Ou descarregar um binário pré-compilado a partir da [página de lançamento](https://github.com/swaggo/swag/releases). diff --git a/README_zh-CN.md b/README_zh-CN.md index 755a7f0b1..fc5c4e188 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -50,7 +50,7 @@ Swag将Go的注释转换为Swagger2.0文档。我们为流行的 [Go Web Framewo go install github.com/swaggo/swag/cmd/swag@latest ``` -从源码开始构建的话,需要有Go环境(1.17及以上版本)。 +从源码开始构建的话,需要有Go环境(1.18及以上版本)。 或者从github的release页面下载预编译好的二进制文件。 diff --git a/const.go b/const.go index 1353b6d1d..83755103b 100644 --- a/const.go +++ b/const.go @@ -6,6 +6,7 @@ import ( "reflect" "strconv" "strings" + "unicode/utf8" ) // ConstVariable a model to record a const variable @@ -60,7 +61,7 @@ func EvaluateEscapedString(text string) string { i++ char, err := strconv.ParseInt(text[i:i+4], 16, 32) if err == nil { - result = AppendUtf8Rune(result, rune(char)) + result = utf8.AppendRune(result, rune(char)) } i += 3 } else if c, ok := escapedChars[text[i]]; ok { @@ -404,7 +405,7 @@ func EvaluateUnary(x interface{}, operator token.Token, xtype ast.Expr) (interfa func EvaluateBinary(x, y interface{}, operator token.Token, xtype, ytype ast.Expr) (interface{}, ast.Expr) { if operator == token.SHR || operator == token.SHL { var rightOperand uint64 - yValue := CanIntegerValue{reflect.ValueOf(y)} + yValue := reflect.ValueOf(y) if yValue.CanUint() { rightOperand = yValue.Uint() } else if yValue.CanInt() { @@ -467,8 +468,8 @@ func EvaluateBinary(x, y interface{}, operator token.Token, xtype, ytype ast.Exp evalType = ytype } - xValue := CanIntegerValue{reflect.ValueOf(x)} - yValue := CanIntegerValue{reflect.ValueOf(y)} + xValue := reflect.ValueOf(x) + yValue := reflect.ValueOf(y) if xValue.Kind() == reflect.String && yValue.Kind() == reflect.String { return xValue.String() + yValue.String(), evalType } diff --git a/example/celler/go.mod b/example/celler/go.mod index a9900ddc1..9e7634fc6 100644 --- a/example/celler/go.mod +++ b/example/celler/go.mod @@ -1,6 +1,6 @@ module github.com/swaggo/swag/example/celler -go 1.17 +go 1.18 require ( github.com/gin-gonic/gin v1.9.1 diff --git a/example/go-module-support/go.mod b/example/go-module-support/go.mod index c48c53740..a915b693c 100644 --- a/example/go-module-support/go.mod +++ b/example/go-module-support/go.mod @@ -1,6 +1,6 @@ module github.com/swaggo/swag/example/go-module-support -go 1.17 +go 1.18 require ( github.com/gin-gonic/gin v1.9.1 diff --git a/example/markdown/go.mod b/example/markdown/go.mod index 13e072e4f..264857adb 100644 --- a/example/markdown/go.mod +++ b/example/markdown/go.mod @@ -1,6 +1,6 @@ module github.com/swaggo/swag/example/markdown -go 1.17 +go 1.18 require ( github.com/gorilla/mux v1.8.0 diff --git a/example/object-map-example/go.mod b/example/object-map-example/go.mod index 1d00b58cb..080338c85 100644 --- a/example/object-map-example/go.mod +++ b/example/object-map-example/go.mod @@ -1,6 +1,6 @@ module github.com/swaggo/swag/example/object-map-example -go 1.17 +go 1.18 require ( github.com/gin-gonic/gin v1.9.1 diff --git a/generics_other.go b/generics_other.go deleted file mode 100644 index 5fd9e8231..000000000 --- a/generics_other.go +++ /dev/null @@ -1,42 +0,0 @@ -//go:build !go1.18 -// +build !go1.18 - -package swag - -import ( - "fmt" - "github.com/go-openapi/spec" - "go/ast" -) - -type genericTypeSpec struct { - ArrayDepth int - TypeSpec *TypeSpecDef - Name string -} - -func (pkgDefs *PackagesDefinitions) parametrizeGenericType(file *ast.File, original *TypeSpecDef, fullGenericForm string) *TypeSpecDef { - return original -} - -func getGenericFieldType(file *ast.File, field ast.Expr, genericParamTypeDefs map[string]*genericTypeSpec) (string, error) { - return "", fmt.Errorf("unknown field type %#v", field) -} - -func (parser *Parser) parseGenericTypeExpr(file *ast.File, typeExpr ast.Expr) (*spec.Schema, error) { - switch typeExpr.(type) { - // suppress debug messages for these types - case *ast.InterfaceType: - case *ast.StructType: - case *ast.Ident: - case *ast.StarExpr: - case *ast.SelectorExpr: - case *ast.ArrayType: - case *ast.MapType: - case *ast.FuncType: - default: - parser.debug.Printf("Type definition of type '%T' is not supported yet. Using 'object' instead.\n", typeExpr) - } - - return PrimitiveSchema(OBJECT), nil -} diff --git a/generics_other_test.go b/generics_other_test.go deleted file mode 100644 index 1a396a04b..000000000 --- a/generics_other_test.go +++ /dev/null @@ -1,67 +0,0 @@ -//go:build !go1.18 -// +build !go1.18 - -package swag - -import ( - "fmt" - "github.com/stretchr/testify/assert" - "go/ast" - "testing" -) - -type testLogger struct { - Messages []string -} - -func (t *testLogger) Printf(format string, v ...interface{}) { - t.Messages = append(t.Messages, fmt.Sprintf(format, v...)) -} - -func TestParametrizeStruct(t *testing.T) { - t.Parallel() - - pd := PackagesDefinitions{ - packages: make(map[string]*PackageDefinitions), - } - - tSpec := &TypeSpecDef{ - TypeSpec: &ast.TypeSpec{ - Name: &ast.Ident{Name: "Field"}, - Type: &ast.StructType{Struct: 100, Fields: &ast.FieldList{Opening: 101, Closing: 102}}, - }, - } - - tr := pd.parametrizeGenericType(&ast.File{}, tSpec, "") - assert.Equal(t, tr, tSpec) - - tr = pd.parametrizeGenericType(&ast.File{}, tSpec, "") - assert.Equal(t, tr, tSpec) -} - -func TestParseGenericTypeExpr(t *testing.T) { - t.Parallel() - - parser := New() - logger := &testLogger{} - SetDebugger(logger)(parser) - - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.InterfaceType{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StructType{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.Ident{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StarExpr{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.SelectorExpr{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.ArrayType{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.MapType{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.FuncType{}) - assert.Empty(t, logger.Messages) - _, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.BadExpr{}) - assert.NotEmpty(t, logger.Messages) -} diff --git a/utils_go18.go b/utils_go18.go deleted file mode 100644 index 814f93433..000000000 --- a/utils_go18.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build go1.18 -// +build go1.18 - -package swag - -import ( - "reflect" - "unicode/utf8" -) - -// AppendUtf8Rune appends the UTF-8 encoding of r to the end of p and -// returns the extended buffer. If the rune is out of range, -// it appends the encoding of RuneError. -func AppendUtf8Rune(p []byte, r rune) []byte { - return utf8.AppendRune(p, r) -} - -// CanIntegerValue a wrapper of reflect.Value -type CanIntegerValue struct { - reflect.Value -} - -// CanInt reports whether Uint can be used without panicking. -func (v CanIntegerValue) CanInt() bool { - return v.Value.CanInt() -} - -// CanUint reports whether Uint can be used without panicking. -func (v CanIntegerValue) CanUint() bool { - return v.Value.CanUint() -} diff --git a/utils_other.go b/utils_other.go deleted file mode 100644 index 531c0df12..000000000 --- a/utils_other.go +++ /dev/null @@ -1,47 +0,0 @@ -//go:build !go1.18 -// +build !go1.18 - -package swag - -import ( - "reflect" - "unicode/utf8" -) - -// AppendUtf8Rune appends the UTF-8 encoding of r to the end of p and -// returns the extended buffer. If the rune is out of range, -// it appends the encoding of RuneError. -func AppendUtf8Rune(p []byte, r rune) []byte { - length := utf8.RuneLen(rune(r)) - if length > 0 { - utf8Slice := make([]byte, length) - utf8.EncodeRune(utf8Slice, rune(r)) - p = append(p, utf8Slice...) - } - return p -} - -// CanIntegerValue a wrapper of reflect.Value -type CanIntegerValue struct { - reflect.Value -} - -// CanInt reports whether Uint can be used without panicking. -func (v CanIntegerValue) CanInt() bool { - switch v.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return true - default: - return false - } -} - -// CanUint reports whether Uint can be used without panicking. -func (v CanIntegerValue) CanUint() bool { - switch v.Kind() { - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return true - default: - return false - } -}