Skip to content

Commit 6de544f

Browse files
tchsskraphael
andauthored
Use doc.IsPredeclared() and token.IsKeyword() for codegen.fixReservedGo() (#3599)
* Add test for codegen.fixReservedGo() * Use doc.IsPredeclared() and token.IsKeyword() for codegen.fixReservedGo(). * Add isPackage --------- Co-authored-by: Raphael Simon <[email protected]>
1 parent bdd4145 commit 6de544f

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

codegen/goify.go

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package codegen
22

33
import (
4+
"go/doc"
5+
"go/token"
46
"strings"
57

68
"goa.design/goa/v3/expr"
@@ -54,70 +56,14 @@ func UnionValTypeName(unionName string) string {
5456

5557
// fixReservedGo appends an underscore on to Go reserved keywords.
5658
func fixReservedGo(w string) string {
57-
if reservedGo[w] {
59+
if doc.IsPredeclared(w) || token.IsKeyword(w) || isPackage[w] {
5860
w += "_"
5961
}
6062
return w
6163
}
6264

6365
var (
64-
// reserved golang keywords and package names
65-
reservedGo = map[string]bool{
66-
// predeclared types
67-
"bool": true,
68-
"byte": true,
69-
"complex128": true,
70-
"complex64": true,
71-
"float32": true,
72-
"float64": true,
73-
"int": true,
74-
"int16": true,
75-
"int32": true,
76-
"int64": true,
77-
"int8": true,
78-
"rune": true,
79-
"string": true,
80-
"uint": true,
81-
"uint16": true,
82-
"uint32": true,
83-
"uint64": true,
84-
"uint8": true,
85-
"uintptr": true,
86-
87-
// reserved keywords
88-
"break": true,
89-
"case": true,
90-
"chan": true,
91-
"const": true,
92-
"continue": true,
93-
"default": true,
94-
"defer": true,
95-
"delete": true,
96-
"else": true,
97-
"fallthrough": true,
98-
"for": true,
99-
"func": true,
100-
"go": true,
101-
"goto": true,
102-
"if": true,
103-
"import": true,
104-
"interface": true,
105-
"map": true,
106-
"package": true,
107-
"range": true,
108-
"return": true,
109-
"select": true,
110-
"struct": true,
111-
"switch": true,
112-
"type": true,
113-
"var": true,
114-
115-
// predeclared constants
116-
"true": true,
117-
"false": true,
118-
"iota": true,
119-
"nil": true,
120-
66+
isPackage = map[string]bool{
12167
// stdlib and goa packages used by generated code
12268
"fmt": true,
12369
"http": true,

codegen/goify_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFixReservedGo(t *testing.T) {
10+
cases := map[string]struct {
11+
w string
12+
want string
13+
}{
14+
"predeclared type": {w: "bool", want: "bool_"},
15+
"predeclared constant": {w: "true", want: "true_"},
16+
"predeclared zero value": {w: "nil", want: "nil_"},
17+
"predeclared function": {w: "append", want: "append_"},
18+
"non predeclared identifier": {w: "foo", want: "foo"},
19+
"package": {w: "fmt", want: "fmt_"},
20+
}
21+
for k, tc := range cases {
22+
t.Run(k, func(t *testing.T) {
23+
assert.Equal(t, tc.want, fixReservedGo(tc.w))
24+
})
25+
}
26+
}

http/codegen/testdata/result_decode_functions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -789,15 +789,15 @@ func DecodeMethodAResponse(decoder func(*http.Response) goahttp.Decoder, restore
789789
return res, nil
790790
case http.StatusBadRequest:
791791
var (
792-
error string
792+
error_ string
793793
numOccur *int
794794
err error
795795
)
796-
errorRaw := resp.Header.Get("X-Application-Error")
797-
if errorRaw == "" {
796+
error_Raw := resp.Header.Get("X-Application-Error")
797+
if error_Raw == "" {
798798
err = goa.MergeErrors(err, goa.MissingFieldError("error", "header"))
799799
}
800-
error = errorRaw
800+
error_ = error_Raw
801801
{
802802
numOccurRaw := resp.Header.Get("X-Occur")
803803
if numOccurRaw != "" {
@@ -817,7 +817,7 @@ func DecodeMethodAResponse(decoder func(*http.Response) goahttp.Decoder, restore
817817
if err != nil {
818818
return nil, goahttp.ErrValidationError("ValidateErrorResponseType", "MethodA", err)
819819
}
820-
return nil, NewMethodASomeError(error, numOccur)
820+
return nil, NewMethodASomeError(error_, numOccur)
821821
default:
822822
body, _ := io.ReadAll(resp.Body)
823823
return nil, goahttp.ErrInvalidResponse("ValidateErrorResponseType", "MethodA", resp.StatusCode, string(body))

0 commit comments

Comments
 (0)