From 2c3783f18f3b679ece4fffed18fa839977e8359d Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 6 Mar 2019 19:13:32 +0900 Subject: [PATCH] some refactoring --- codegen/templates/templates.go | 35 +++++++++++++----- codegen/templates/templates_test.go | 56 ++++++++++++++--------------- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index fe51caaf5a4..a919a829ecc 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -230,8 +230,11 @@ func Call(p *types.Func) string { func ToGo(name string) string { runes := make([]rune, 0, len(name)) - wordWalker(name, func(word string, hasCommonInitial bool) { - if !hasCommonInitial { + wordWalker(name, func(info *wordInfo) { + word := info.Word + if info.MatchCommonInitial { + word = strings.ToUpper(word) + } else if !info.HasCommonInitial { word = ucFirst(strings.ToLower(word)) } runes = append(runes, []rune(word)...) @@ -244,11 +247,14 @@ func ToGoPrivate(name string) string { runes := make([]rune, 0, len(name)) first := true - wordWalker(name, func(word string, hasCommonInitial bool) { + wordWalker(name, func(info *wordInfo) { + word := info.Word if first { - word = strings.ToLower(word) + word = strings.ToLower(info.Word) first = false - } else if !hasCommonInitial { + } else if info.MatchCommonInitial { + word = strings.ToUpper(word) + } else if !info.HasCommonInitial { word = ucFirst(strings.ToLower(word)) } runes = append(runes, []rune(word)...) @@ -257,7 +263,13 @@ func ToGoPrivate(name string) string { return sanitizeKeywords(string(runes)) } -func wordWalker(str string, f func(word string, hasCommonInitial bool)) { +type wordInfo struct { + Word string + MatchCommonInitial bool + HasCommonInitial bool +} + +func wordWalker(str string, f func(*wordInfo)) { skipRune := func(r rune) bool { switch r { @@ -309,12 +321,17 @@ func wordWalker(str string, f func(word string, hasCommonInitial bool)) { continue } - if u := strings.ToUpper(word); commonInitialisms[u] { + matchCommonInitial := false + if commonInitialisms[strings.ToUpper(word)] { hasCommonInitial = true - word = u + matchCommonInitial = true } - f(word, hasCommonInitial) + f(&wordInfo{ + Word: word, + MatchCommonInitial: matchCommonInitial, + HasCommonInitial: hasCommonInitial, + }) hasCommonInitial = false w = i } diff --git a/codegen/templates/templates_test.go b/codegen/templates/templates_test.go index 47700b4ef5c..cea36a3018f 100644 --- a/codegen/templates/templates_test.go +++ b/codegen/templates/templates_test.go @@ -25,6 +25,7 @@ func TestToGo(t *testing.T) { require.Equal(t, "A", ToGo("A")) require.Equal(t, "ID", ToGo("ID")) + require.Equal(t, "ID", ToGo("id")) require.Equal(t, "", ToGo("")) require.Equal(t, "RelatedUrls", ToGo("RelatedUrls")) @@ -51,44 +52,41 @@ func TestToGoPrivate(t *testing.T) { require.Equal(t, "a", ToGoPrivate("A")) require.Equal(t, "id", ToGoPrivate("ID")) + require.Equal(t, "id", ToGoPrivate("id")) require.Equal(t, "", ToGoPrivate("")) } func Test_wordWalker(t *testing.T) { - type Result struct { - Value string - HasCommonInitial bool - } - helper := func(str string) []*Result { - resultList := []*Result{} - wordWalker(str, func(word string, hasCommonInitial bool) { - resultList = append(resultList, &Result{word, hasCommonInitial}) + helper := func(str string) []*wordInfo { + resultList := []*wordInfo{} + wordWalker(str, func(info *wordInfo) { + resultList = append(resultList, info) }) return resultList } - require.Equal(t, []*Result{{Value: "TO"}, {Value: "CAMEL"}}, helper("TO_CAMEL")) - require.Equal(t, []*Result{{Value: "to"}, {Value: "camel"}}, helper("to_camel")) - require.Equal(t, []*Result{{Value: "to"}, {Value: "Camel"}}, helper("toCamel")) - require.Equal(t, []*Result{{Value: "To"}, {Value: "Camel"}}, helper("ToCamel")) - require.Equal(t, []*Result{{Value: "to"}, {Value: "camel"}}, helper("to-camel")) - - require.Equal(t, []*Result{{Value: "Related"}, {Value: "URLs", HasCommonInitial: true}}, helper("RelatedURLs")) - require.Equal(t, []*Result{{Value: "Image"}, {Value: "IDs", HasCommonInitial: true}}, helper("ImageIDs")) - require.Equal(t, []*Result{{Value: "Foo"}, {Value: "ID", HasCommonInitial: true}}, helper("FooID")) - require.Equal(t, []*Result{{Value: "ID", HasCommonInitial: true}, {Value: "Foo"}}, helper("IDFoo")) - require.Equal(t, []*Result{{Value: "Foo"}, {Value: "ASCII", HasCommonInitial: true}}, helper("FooASCII")) - require.Equal(t, []*Result{{Value: "ASCII", HasCommonInitial: true}, {Value: "Foo"}}, helper("ASCIIFoo")) - require.Equal(t, []*Result{{Value: "Foo"}, {Value: "UTF8", HasCommonInitial: true}}, helper("FooUTF8")) - require.Equal(t, []*Result{{Value: "UTF8", HasCommonInitial: true}, {Value: "Foo"}}, helper("UTF8Foo")) - - require.Equal(t, []*Result{{Value: "A"}}, helper("A")) - require.Equal(t, []*Result{{Value: "ID", HasCommonInitial: true}}, helper("ID")) - require.Equal(t, []*Result{{Value: "ID", HasCommonInitial: true}}, helper("id")) - require.Equal(t, []*Result{}, helper("")) - - require.Equal(t, []*Result{{Value: "Related"}, {Value: "Urls"}}, helper("RelatedUrls")) + require.Equal(t, []*wordInfo{{Word: "TO"}, {Word: "CAMEL"}}, helper("TO_CAMEL")) + require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "camel"}}, helper("to_camel")) + require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "Camel"}}, helper("toCamel")) + require.Equal(t, []*wordInfo{{Word: "To"}, {Word: "Camel"}}, helper("ToCamel")) + require.Equal(t, []*wordInfo{{Word: "to"}, {Word: "camel"}}, helper("to-camel")) + + require.Equal(t, []*wordInfo{{Word: "Related"}, {Word: "URLs", HasCommonInitial: true}}, helper("RelatedURLs")) + require.Equal(t, []*wordInfo{{Word: "Image"}, {Word: "IDs", HasCommonInitial: true}}, helper("ImageIDs")) + require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooID")) + require.Equal(t, []*wordInfo{{Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("IDFoo")) + require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "ASCII", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooASCII")) + require.Equal(t, []*wordInfo{{Word: "ASCII", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("ASCIIFoo")) + require.Equal(t, []*wordInfo{{Word: "Foo"}, {Word: "UTF8", HasCommonInitial: true, MatchCommonInitial: true}}, helper("FooUTF8")) + require.Equal(t, []*wordInfo{{Word: "UTF8", HasCommonInitial: true, MatchCommonInitial: true}, {Word: "Foo"}}, helper("UTF8Foo")) + + require.Equal(t, []*wordInfo{{Word: "A"}}, helper("A")) + require.Equal(t, []*wordInfo{{Word: "ID", HasCommonInitial: true, MatchCommonInitial: true}}, helper("ID")) + require.Equal(t, []*wordInfo{{Word: "id", HasCommonInitial: true, MatchCommonInitial: true}}, helper("id")) + require.Equal(t, []*wordInfo{}, helper("")) + + require.Equal(t, []*wordInfo{{Word: "Related"}, {Word: "Urls"}}, helper("RelatedUrls")) } func TestCenter(t *testing.T) {