From d86759b84c6761e24f67a21bb92ec4ab54a8dbda Mon Sep 17 00:00:00 2001 From: Igor Cananea Date: Sat, 16 Apr 2022 16:13:54 -0400 Subject: [PATCH] Prepend an underscore to a StructName if it starts with a digit. --- internal/codegen/golang/struct.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/codegen/golang/struct.go b/internal/codegen/golang/struct.go index 1d36e92aba..f72a228ae3 100644 --- a/internal/codegen/golang/struct.go +++ b/internal/codegen/golang/struct.go @@ -2,6 +2,8 @@ package golang import ( "strings" + "unicode" + "unicode/utf8" "github.com/kyleconroy/sqlc/internal/plugin" ) @@ -25,5 +27,12 @@ func StructName(name string, settings *plugin.Settings) string { out += strings.Title(p) } } - return out + + // If a name has a digit as its first char, prepand an underscore to make it a valid Go name. + r, _ := utf8.DecodeRuneInString(out) + if unicode.IsDigit(r) { + return "_" + out + } else { + return out + } }