From 0f727e1c7a1e988b120cc13be48eebcd32da42b4 Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Fri, 4 Oct 2024 20:06:03 +0900 Subject: [PATCH 1/3] Add test for codegen.fixReservedGo() --- codegen/goify_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 codegen/goify_test.go diff --git a/codegen/goify_test.go b/codegen/goify_test.go new file mode 100644 index 0000000000..6a274d349e --- /dev/null +++ b/codegen/goify_test.go @@ -0,0 +1,25 @@ +package codegen + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFixReservedGo(t *testing.T) { + cases := map[string]struct { + w string + want string + }{ + "predeclared type": {w: "bool", want: "bool_"}, + "predeclared constant": {w: "true", want: "true_"}, + "predeclared zero value": {w: "nil", want: "nil_"}, + "predeclared function": {w: "append", want: "append_"}, + "non predeclared identifier": {w: "foo", want: "foo"}, + } + for k, tc := range cases { + t.Run(k, func(t *testing.T) { + assert.Equal(t, tc.want, fixReservedGo(tc.w)) + }) + } +} From 8e62659ff2d605889ccf19c4c778063e803c57cd Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Fri, 4 Oct 2024 20:12:14 +0900 Subject: [PATCH 2/3] Use doc.IsPredeclared() and token.IsKeyword() for codegen.fixReservedGo(). --- codegen/goify.go | 72 +------------------ .../testdata/result_decode_functions.go | 10 +-- 2 files changed, 8 insertions(+), 74 deletions(-) diff --git a/codegen/goify.go b/codegen/goify.go index b6caae5549..b5348a0c2d 100644 --- a/codegen/goify.go +++ b/codegen/goify.go @@ -1,6 +1,8 @@ package codegen import ( + "go/doc" + "go/token" "strings" "goa.design/goa/v3/expr" @@ -54,76 +56,8 @@ func UnionValTypeName(unionName string) string { // fixReservedGo appends an underscore on to Go reserved keywords. func fixReservedGo(w string) string { - if reservedGo[w] { + if doc.IsPredeclared(w) || token.IsKeyword(w) { w += "_" } return w } - -var ( - // reserved golang keywords and package names - reservedGo = map[string]bool{ - // predeclared types - "bool": true, - "byte": true, - "complex128": true, - "complex64": true, - "float32": true, - "float64": true, - "int": true, - "int16": true, - "int32": true, - "int64": true, - "int8": true, - "rune": true, - "string": true, - "uint": true, - "uint16": true, - "uint32": true, - "uint64": true, - "uint8": true, - "uintptr": true, - - // reserved keywords - "break": true, - "case": true, - "chan": true, - "const": true, - "continue": true, - "default": true, - "defer": true, - "delete": true, - "else": true, - "fallthrough": true, - "for": true, - "func": true, - "go": true, - "goto": true, - "if": true, - "import": true, - "interface": true, - "map": true, - "package": true, - "range": true, - "return": true, - "select": true, - "struct": true, - "switch": true, - "type": true, - "var": true, - - // predeclared constants - "true": true, - "false": true, - "iota": true, - "nil": true, - - // stdlib and goa packages used by generated code - "fmt": true, - "http": true, - "json": true, - "os": true, - "url": true, - "time": true, - } -) diff --git a/http/codegen/testdata/result_decode_functions.go b/http/codegen/testdata/result_decode_functions.go index 4d91cfa90e..6e08aae07d 100644 --- a/http/codegen/testdata/result_decode_functions.go +++ b/http/codegen/testdata/result_decode_functions.go @@ -789,15 +789,15 @@ func DecodeMethodAResponse(decoder func(*http.Response) goahttp.Decoder, restore return res, nil case http.StatusBadRequest: var ( - error string + error_ string numOccur *int err error ) - errorRaw := resp.Header.Get("X-Application-Error") - if errorRaw == "" { + error_Raw := resp.Header.Get("X-Application-Error") + if error_Raw == "" { err = goa.MergeErrors(err, goa.MissingFieldError("error", "header")) } - error = errorRaw + error_ = error_Raw { numOccurRaw := resp.Header.Get("X-Occur") if numOccurRaw != "" { @@ -817,7 +817,7 @@ func DecodeMethodAResponse(decoder func(*http.Response) goahttp.Decoder, restore if err != nil { return nil, goahttp.ErrValidationError("ValidateErrorResponseType", "MethodA", err) } - return nil, NewMethodASomeError(error, numOccur) + return nil, NewMethodASomeError(error_, numOccur) default: body, _ := io.ReadAll(resp.Body) return nil, goahttp.ErrInvalidResponse("ValidateErrorResponseType", "MethodA", resp.StatusCode, string(body)) From 1fded794b38851176d6d5f62366749d72ee27beb Mon Sep 17 00:00:00 2001 From: Taichi Sasaki Date: Fri, 11 Oct 2024 23:22:00 +0900 Subject: [PATCH 3/3] Add isPackage --- codegen/goify.go | 14 +++++++++++++- codegen/goify_test.go | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/codegen/goify.go b/codegen/goify.go index b5348a0c2d..3c26c8d335 100644 --- a/codegen/goify.go +++ b/codegen/goify.go @@ -56,8 +56,20 @@ func UnionValTypeName(unionName string) string { // fixReservedGo appends an underscore on to Go reserved keywords. func fixReservedGo(w string) string { - if doc.IsPredeclared(w) || token.IsKeyword(w) { + if doc.IsPredeclared(w) || token.IsKeyword(w) || isPackage[w] { w += "_" } return w } + +var ( + isPackage = map[string]bool{ + // stdlib and goa packages used by generated code + "fmt": true, + "http": true, + "json": true, + "os": true, + "url": true, + "time": true, + } +) diff --git a/codegen/goify_test.go b/codegen/goify_test.go index 6a274d349e..d7d4339f47 100644 --- a/codegen/goify_test.go +++ b/codegen/goify_test.go @@ -16,6 +16,7 @@ func TestFixReservedGo(t *testing.T) { "predeclared zero value": {w: "nil", want: "nil_"}, "predeclared function": {w: "append", want: "append_"}, "non predeclared identifier": {w: "foo", want: "foo"}, + "package": {w: "fmt", want: "fmt_"}, } for k, tc := range cases { t.Run(k, func(t *testing.T) {