From ad87fe465206bf2ce370dc77bbb209d3e0480d14 Mon Sep 17 00:00:00 2001 From: Vadim Petrov Date: Thu, 28 Jul 2016 14:24:17 +0300 Subject: [PATCH] Remove functionNamer interface --- Makefile | 2 +- bootstrap/bootstrap.go | 6 +++--- easyjson/main.go | 4 ++-- gen/generator.go | 22 ++++++---------------- gen/generator_test.go | 40 ++++++++++++++++------------------------ 5 files changed, 28 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index 351f969..5a95104 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ generate: root build .root/src/$(PKG)/tests/snake.go \ .root/src/$(PKG)/tests/data.go \ .root/src/$(PKG)/tests/omitempty.go \ - .root/src/$(PKG)/tests/nothing.go \ + .root/src/$(PKG)/tests/nothing.go .root/bin/easyjson -all .root/src/$(PKG)/tests/data.go .root/bin/easyjson -all .root/src/$(PKG)/tests/nothing.go diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index f9cf3b6..04aaaad 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -22,7 +22,7 @@ type Generator struct { Types []string NoStdMarshalers bool - SnakeCaseFields bool + SnakeCase bool OmitEmpty bool OutName string @@ -105,8 +105,8 @@ func (g *Generator) writeMain() (path string, err error) { if g.BuildTags != "" { fmt.Fprintf(f, " g.SetBuildTags(%q)\n", g.BuildTags) } - if g.SnakeCaseFields { - fmt.Fprintln(f, " g.UseSnakeCaseFieldNamer()") + if g.SnakeCase { + fmt.Fprintln(f, " g.UseSnakeCase()") } if g.OmitEmpty { fmt.Fprintln(f, " g.OmitEmpty()") diff --git a/easyjson/main.go b/easyjson/main.go index abcd183..8ae9223 100644 --- a/easyjson/main.go +++ b/easyjson/main.go @@ -15,7 +15,7 @@ import ( ) var buildTags = flag.String("build_tags", "", "build tags to add to generated file") -var snakeCaseFields = flag.Bool("snake_case", false, "use snake_case names instead of CamelCase by default") +var snakeCase = flag.Bool("snake_case", false, "use snake_case names instead of CamelCase by default") var noStdMarshalers = flag.Bool("no_std_marshalers", false, "don't generate MarshalJSON/UnmarshalJSON methods") var omitEmpty = flag.Bool("omit_empty", false, "omit empty fields by default") var allStructs = flag.Bool("all", false, "generate un-/marshallers for all structs in a file") @@ -46,7 +46,7 @@ func generate(fname string) (err error) { PkgPath: p.PkgPath, PkgName: p.PkgName, Types: p.StructNames, - SnakeCaseFields: *snakeCaseFields, + SnakeCase: *snakeCase, NoStdMarshalers: *noStdMarshalers, OmitEmpty: *omitEmpty, LeaveTemps: *leaveTemps, diff --git a/gen/generator.go b/gen/generator.go index 9ebe4f5..2a3ef9c 100644 --- a/gen/generator.go +++ b/gen/generator.go @@ -20,11 +20,6 @@ type FieldNamer interface { GetJSONFieldName(t reflect.Type, f reflect.StructField) string } -// FuncNamer defines a policy for generating function names -type FunctionNamer interface { - GetName(keepFirst bool, parts ...string) string -} - // Generator generates the requested marshallers/unmarshallers. type Generator struct { out *bytes.Buffer @@ -39,7 +34,6 @@ type Generator struct { noStdMarshalers bool omitEmpty bool fieldNamer FieldNamer - functionNamer FunctionNamer // package path to local alias map for tracking imports imports map[string]string @@ -67,7 +61,6 @@ func NewGenerator(filename string) *Generator { "encoding/json": "json", }, fieldNamer: DefaultFieldNamer{}, - functionNamer: CamelCaseFunctionNamer{}, marshallers: make(map[reflect.Type]bool), typesSeen: make(map[reflect.Type]bool), functionNames: make(map[string]reflect.Type), @@ -98,8 +91,8 @@ func (g *Generator) SetFieldNamer(n FieldNamer) { g.fieldNamer = n } -// UseSnakeCaseFieldNamer sets snake_case field naming strategy. -func (g *Generator) UseSnakeCaseFieldNamer() { +// UseSnakeCase sets snake_case field naming strategy. +func (g *Generator) UseSnakeCase() { g.fieldNamer = SnakeCaseFieldNamer{} } @@ -280,7 +273,7 @@ func (g *Generator) safeName(t reflect.Type) string { part = []rune{} } } - return g.functionNamer.GetName(false, parts...) + return joinFunctionNameParts(false, parts...) } // functionName returns a function name for a given type with a given prefix. If a function @@ -288,8 +281,8 @@ func (g *Generator) safeName(t reflect.Type) string { // // Method is used to track encoder/decoder names for the type. func (g *Generator) functionName(prefix string, t reflect.Type) string { - prefix = g.functionNamer.GetName(true, "easyjson", g.hashString, prefix) - name := g.functionNamer.GetName(true, prefix, g.safeName(t)) + prefix = joinFunctionNameParts(true, "easyjson", g.hashString, prefix) + name := joinFunctionNameParts(true, prefix, g.safeName(t)) // Most of the names will be unique, try a shortcut first. if e, ok := g.functionNames[name]; !ok || e == t { @@ -384,10 +377,7 @@ func (SnakeCaseFieldNamer) GetJSONFieldName(t reflect.Type, f reflect.StructFiel return camelToSnake(f.Name) } -// CamelCaseFunctionNamer implements FunctionNamer interface with CamelCase format -type CamelCaseFunctionNamer struct{} - -func (CamelCaseFunctionNamer) GetName(keepFirst bool, parts ...string) string { +func joinFunctionNameParts(keepFirst bool, parts ...string) string { buf := bytes.NewBufferString("") for i, part := range parts { if i == 0 && keepFirst { diff --git a/gen/generator_test.go b/gen/generator_test.go index ee77d17..d7b6b0f 100644 --- a/gen/generator_test.go +++ b/gen/generator_test.go @@ -4,13 +4,6 @@ import ( "testing" ) -type functionNamerCase struct { - keepFirst bool - parts []string - camelCaseOut string - underScoreOut string -} - func TestCamelToSnake(t *testing.T) { for i, test := range []struct { In, Out string @@ -35,23 +28,22 @@ func TestCamelToSnake(t *testing.T) { } } -func getFunctionNamerCases() []functionNamerCase { - return []functionNamerCase{ - functionNamerCase{false, []string{}, "", ""}, - functionNamerCase{false, []string{"a"}, "A", "a"}, - functionNamerCase{false, []string{"simple", "example"}, "SimpleExample", "simple_example"}, - functionNamerCase{true, []string{"first", "example"}, "firstExample", "first_example"}, - functionNamerCase{false, []string{"some", "UPPER", "case"}, "SomeUPPERCase", "some_UPPER_case"}, - functionNamerCase{false, []string{"number", "123"}, "Number123", "number_123"}, - } -} - -func TestCamelCaseFunctionNamer(t *testing.T) { - namer := CamelCaseFunctionNamer{} - for i, test := range getFunctionNamerCases() { - got := namer.GetName(test.keepFirst, test.parts...) - if got != test.camelCaseOut { - t.Errorf("[%d] CamelCaseFunctionNamer.GetName(%v) = %s; want %s", i, test.parts, got, test.camelCaseOut) +func TestJoinFunctionNameParts(t *testing.T) { + for i, test := range []struct { + keepFirst bool + parts []string + out string + }{ + {false, []string{}, ""}, + {false, []string{"a"}, "A"}, + {false, []string{"simple", "example"}, "SimpleExample"}, + {true, []string{"first", "example"}, "firstExample"}, + {false, []string{"some", "UPPER", "case"}, "SomeUPPERCase"}, + {false, []string{"number", "123"}, "Number123"}, + } { + got := joinFunctionNameParts(test.keepFirst, test.parts...) + if got != test.out { + t.Errorf("[%d] joinFunctionNameParts(%v) = %s; want %s", i, test.parts, got, test.out) } } }