Skip to content

Commit

Permalink
Use doc.IsPredeclared() and token.IsKeyword() for codegen.fixReserved…
Browse files Browse the repository at this point in the history
…Go() (#3599)

* Add test for codegen.fixReservedGo()

* Use doc.IsPredeclared() and token.IsKeyword()

for codegen.fixReservedGo().

* Add isPackage

---------

Co-authored-by: Raphael Simon <[email protected]>
  • Loading branch information
tchssk and raphael authored Oct 13, 2024
1 parent bdd4145 commit 6de544f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 63 deletions.
62 changes: 4 additions & 58 deletions codegen/goify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codegen

import (
"go/doc"
"go/token"
"strings"

"goa.design/goa/v3/expr"
Expand Down Expand Up @@ -54,70 +56,14 @@ 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) || isPackage[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,

isPackage = map[string]bool{
// stdlib and goa packages used by generated code
"fmt": true,
"http": true,
Expand Down
26 changes: 26 additions & 0 deletions codegen/goify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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"},
"package": {w: "fmt", want: "fmt_"},
}
for k, tc := range cases {
t.Run(k, func(t *testing.T) {
assert.Equal(t, tc.want, fixReservedGo(tc.w))
})
}
}
10 changes: 5 additions & 5 deletions http/codegen/testdata/result_decode_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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))
Expand Down

0 comments on commit 6de544f

Please sign in to comment.