From 092daabf3fbd1d7c8a2d0ed35959d3e518074901 Mon Sep 17 00:00:00 2001 From: Wout Slakhorst Date: Mon, 6 May 2019 21:57:06 +0200 Subject: [PATCH] Allow underscore to exist at beginning of type name --- generator.go | 6 ++++++ generator_test.go | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/generator.go b/generator.go index 9b9e421..1c673b1 100644 --- a/generator.go +++ b/generator.go @@ -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. diff --git a/generator_test.go b/generator_test.go index 7e02e21..53768c6 100644 --- a/generator_test.go +++ b/generator_test.go @@ -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",