Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow underscore to exist at beginning of type name #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ func (g *Generator) getSchemaName(keyName string, schema *Schema) string {
// getGolangName strips invalid characters out of golang struct or field names.
func getGolangName(s string) string {
buf := bytes.NewBuffer([]byte{})

if s[0] == '_' {
// Go variables are allowed to start with an underscore and an underscore at the beginning does not indicate snake_casing
buf.WriteRune('_')
}

for i, v := range splitOnAll(s, isNotAGoNameCharacter) {
if i == 0 && strings.IndexAny(v, "0123456789") == 0 {
// Go types are not allowed to start with a number, lets prefix with an underscore.
Expand Down
7 changes: 6 additions & 1 deletion generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,15 @@ func TestThatJavascriptKeyNamesCanBeConvertedToValidGoNames(t *testing.T) {
expected: "KeyName",
},
{
description: "Underscores are stripped.",
description: "Underscores are stripped in mid sentence.",
input: "key_name",
expected: "KeyName",
},
{
description: "Underscores at the beginning are preserved.",
input: "_key",
expected: "_Key",
},
{
description: "Periods are stripped.",
input: "a.b.c",
Expand Down