From 04d9160b3b79c3bb16f41557031b50150d586175 Mon Sep 17 00:00:00 2001 From: vvakame Date: Tue, 5 Mar 2019 18:42:06 +0900 Subject: [PATCH 1/4] Take care about commonInitialisms in ToCamel --- codegen/templates/templates.go | 33 ++++++++++++++++++++++++----- codegen/templates/templates_test.go | 15 ++++++++++++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index 8522824e5c..74085e0fd3 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -232,29 +232,52 @@ func ToCamel(s string) string { if s == "_" { return "_" } - buffer := make([]rune, 0, len(s)) + buf := bytes.NewBuffer(make([]byte, 0, len(s))) upper := true lastWasUpper := false + var maxCommonInitialismsLen int + for word := range commonInitialisms { + if l := len(word); maxCommonInitialismsLen < l { + maxCommonInitialismsLen = l + } + } - for _, c := range s { +outer: + for i, rs := 0, []rune(s); i < len(rs); i++ { + c := rs[i] if isDelimiter(c) { upper = true continue } if !lastWasUpper && unicode.IsUpper(c) { + tail := len(rs) - i + if maxCommonInitialismsLen < tail { + tail = maxCommonInitialismsLen + } + for j := tail; j != 0; j-- { + word := string(rs[i : i+j]) + if commonInitialisms[word] { + buf.WriteString(word) + i += j - 1 + upper = false + lastWasUpper = false // IDFoo will be IDFoo, not IDfoo + continue outer + } + } + upper = true } if upper { - buffer = append(buffer, unicode.ToUpper(c)) + buf.WriteRune(unicode.ToUpper(c)) } else { - buffer = append(buffer, unicode.ToLower(c)) + buf.WriteRune(unicode.ToLower(c)) } upper = false lastWasUpper = unicode.IsUpper(c) } - return string(buffer) + return buf.String() } func ToGo(name string) string { diff --git a/codegen/templates/templates_test.go b/codegen/templates/templates_test.go index 31f518b4bc..5debc545ab 100644 --- a/codegen/templates/templates_test.go +++ b/codegen/templates/templates_test.go @@ -6,12 +6,25 @@ import ( "github.com/stretchr/testify/require" ) -func TestToUpper(t *testing.T) { +func TestToCamel(t *testing.T) { require.Equal(t, "ToCamel", ToCamel("TO_CAMEL")) require.Equal(t, "ToCamel", ToCamel("to_camel")) require.Equal(t, "ToCamel", ToCamel("toCamel")) require.Equal(t, "ToCamel", ToCamel("ToCamel")) require.Equal(t, "ToCamel", ToCamel("to-camel")) + + require.Equal(t, "RelatedURLs", ToCamel("RelatedURLs")) + require.Equal(t, "ImageIDs", ToCamel("ImageIDs")) + require.Equal(t, "FooID", ToCamel("FooID")) + require.Equal(t, "IDFoo", ToCamel("IDFoo")) + require.Equal(t, "FooASCII", ToCamel("FooASCII")) + require.Equal(t, "ASCIIFoo", ToCamel("ASCIIFoo")) + require.Equal(t, "FooUTF8", ToCamel("FooUTF8")) + require.Equal(t, "UTF8Foo", ToCamel("UTF8Foo")) + + require.Equal(t, "A", ToCamel("A")) + require.Equal(t, "ID", ToCamel("ID")) + require.Equal(t, "", ToCamel("")) } func TestCenter(t *testing.T) { From 87648c5ed39dc9b465a0f561795ca27a293c89dd Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 6 Mar 2019 18:56:42 +0900 Subject: [PATCH 2/4] address comment --- codegen/generated!.gotpl | 6 +- codegen/templates/templates.go | 128 +++++++++++++++++----------- codegen/templates/templates_test.go | 102 +++++++++++++++++----- 3 files changed, 166 insertions(+), 70 deletions(-) diff --git a/codegen/generated!.gotpl b/codegen/generated!.gotpl index ca9e754929..7c8afe436e 100644 --- a/codegen/generated!.gotpl +++ b/codegen/generated!.gotpl @@ -45,7 +45,7 @@ type DirectiveRoot struct { type ComplexityRoot struct { {{ range $object := .Objects }} {{ if not $object.IsReserved -}} - {{ $object.Name|toCamel }} struct { + {{ $object.Name|go }} struct { {{ range $field := $object.Fields -}} {{ if not $field.IsReserved -}} {{ $field.GoFieldName }} {{ $field.ComplexitySignature }} @@ -87,7 +87,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in {{ range $field := $object.Fields }} {{ if not $field.IsReserved }} case "{{$object.Name}}.{{$field.GoFieldName}}": - if e.complexity.{{$object.Name|toCamel}}.{{$field.GoFieldName}} == nil { + if e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}} == nil { break } {{ if $field.Args }} @@ -96,7 +96,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return 0, false } {{ end }} - return e.complexity.{{$object.Name|toCamel}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true + return e.complexity.{{$object.Name|go}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true {{ end }} {{ end }} {{ end }} diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index 74085e0fd3..fe51caaf5a 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -129,7 +129,6 @@ func Funcs() template.FuncMap { "lcFirst": lcFirst, "quote": strconv.Quote, "rawQuote": rawQuote, - "toCamel": ToCamel, "dump": Dump, "ref": ref, "ts": TypeIdentifier, @@ -228,64 +227,97 @@ func Call(p *types.Func) string { return pkg + p.Name() } -func ToCamel(s string) string { - if s == "_" { - return "_" - } - buf := bytes.NewBuffer(make([]byte, 0, len(s))) - upper := true - lastWasUpper := false - var maxCommonInitialismsLen int - for word := range commonInitialisms { - if l := len(word); maxCommonInitialismsLen < l { - maxCommonInitialismsLen = l +func ToGo(name string) string { + runes := make([]rune, 0, len(name)) + + wordWalker(name, func(word string, hasCommonInitial bool) { + if !hasCommonInitial { + word = ucFirst(strings.ToLower(word)) } - } + runes = append(runes, []rune(word)...) + }) -outer: - for i, rs := 0, []rune(s); i < len(rs); i++ { - c := rs[i] - if isDelimiter(c) { - upper = true - continue + return string(runes) +} + +func ToGoPrivate(name string) string { + runes := make([]rune, 0, len(name)) + + first := true + wordWalker(name, func(word string, hasCommonInitial bool) { + if first { + word = strings.ToLower(word) + first = false + } else if !hasCommonInitial { + word = ucFirst(strings.ToLower(word)) } - if !lastWasUpper && unicode.IsUpper(c) { - tail := len(rs) - i - if maxCommonInitialismsLen < tail { - tail = maxCommonInitialismsLen + runes = append(runes, []rune(word)...) + }) + + return sanitizeKeywords(string(runes)) +} + +func wordWalker(str string, f func(word string, hasCommonInitial bool)) { + + skipRune := func(r rune) bool { + switch r { + case '-', '_': + return true + default: + return false + } + } + + runes := []rune(str) + w, i := 0, 0 // index of start of word, scan + hasCommonInitial := false + for i+1 <= len(runes) { + eow := false // whether we hit the end of a word + if i+1 == len(runes) { + eow = true + } else if skipRune(runes[i+1]) { + // underscore; shift the remainder forward over any run of underscores + eow = true + n := 1 + for i+n+1 < len(runes) && skipRune(runes[i+n+1]) { + n++ } - for j := tail; j != 0; j-- { - word := string(rs[i : i+j]) - if commonInitialisms[word] { - buf.WriteString(word) - i += j - 1 - upper = false - lastWasUpper = false // IDFoo will be IDFoo, not IDfoo - continue outer - } + + // Leave at most one underscore if the underscore is between two digits + if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) { + n-- } - upper = true + copy(runes[i+1:], runes[i+n+1:]) + runes = runes[:len(runes)-n] + } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { + // lower->non-lower + eow = true } + i++ - if upper { - buf.WriteRune(unicode.ToUpper(c)) - } else { - buf.WriteRune(unicode.ToLower(c)) + // [w,i) is a word. + word := string(runes[w:i]) + if !eow && commonInitialisms[word] && !unicode.IsLower(runes[i]) { + // through + // split IDFoo → ID, Foo + // but URLs → URLs + } else if !eow { + if commonInitialisms[word] { + hasCommonInitial = true + } + continue } - upper = false - lastWasUpper = unicode.IsUpper(c) - } - return buf.String() -} - -func ToGo(name string) string { - return lintName(ToCamel(name)) -} + if u := strings.ToUpper(word); commonInitialisms[u] { + hasCommonInitial = true + word = u + } -func ToGoPrivate(name string) string { - return lintName(sanitizeKeywords(lcFirst(ToCamel(name)))) + f(word, hasCommonInitial) + hasCommonInitial = false + w = i + } } var keywords = []string{ diff --git a/codegen/templates/templates_test.go b/codegen/templates/templates_test.go index 5debc545ab..47700b4ef5 100644 --- a/codegen/templates/templates_test.go +++ b/codegen/templates/templates_test.go @@ -6,25 +6,89 @@ import ( "github.com/stretchr/testify/require" ) -func TestToCamel(t *testing.T) { - require.Equal(t, "ToCamel", ToCamel("TO_CAMEL")) - require.Equal(t, "ToCamel", ToCamel("to_camel")) - require.Equal(t, "ToCamel", ToCamel("toCamel")) - require.Equal(t, "ToCamel", ToCamel("ToCamel")) - require.Equal(t, "ToCamel", ToCamel("to-camel")) - - require.Equal(t, "RelatedURLs", ToCamel("RelatedURLs")) - require.Equal(t, "ImageIDs", ToCamel("ImageIDs")) - require.Equal(t, "FooID", ToCamel("FooID")) - require.Equal(t, "IDFoo", ToCamel("IDFoo")) - require.Equal(t, "FooASCII", ToCamel("FooASCII")) - require.Equal(t, "ASCIIFoo", ToCamel("ASCIIFoo")) - require.Equal(t, "FooUTF8", ToCamel("FooUTF8")) - require.Equal(t, "UTF8Foo", ToCamel("UTF8Foo")) - - require.Equal(t, "A", ToCamel("A")) - require.Equal(t, "ID", ToCamel("ID")) - require.Equal(t, "", ToCamel("")) +func TestToGo(t *testing.T) { + require.Equal(t, "ToCamel", ToGo("TO_CAMEL")) + require.Equal(t, "ToCamel", ToGo("to_camel")) + require.Equal(t, "ToCamel", ToGo("toCamel")) + require.Equal(t, "ToCamel", ToGo("ToCamel")) + require.Equal(t, "ToCamel", ToGo("to-camel")) + + require.Equal(t, "RelatedURLs", ToGo("RelatedURLs")) + require.Equal(t, "ImageIDs", ToGo("ImageIDs")) + require.Equal(t, "FooID", ToGo("FooID")) + require.Equal(t, "IDFoo", ToGo("IDFoo")) + require.Equal(t, "FooASCII", ToGo("FooASCII")) + require.Equal(t, "ASCIIFoo", ToGo("ASCIIFoo")) + require.Equal(t, "FooUTF8", ToGo("FooUTF8")) + require.Equal(t, "UTF8Foo", ToGo("UTF8Foo")) + require.Equal(t, "JSONEncoding", ToGo("JSONEncoding")) + + require.Equal(t, "A", ToGo("A")) + require.Equal(t, "ID", ToGo("ID")) + require.Equal(t, "", ToGo("")) + + require.Equal(t, "RelatedUrls", ToGo("RelatedUrls")) +} + +func TestToGoPrivate(t *testing.T) { + require.Equal(t, "toCamel", ToGoPrivate("TO_CAMEL")) + require.Equal(t, "toCamel", ToGoPrivate("to_camel")) + require.Equal(t, "toCamel", ToGoPrivate("toCamel")) + require.Equal(t, "toCamel", ToGoPrivate("ToCamel")) + require.Equal(t, "toCamel", ToGoPrivate("to-camel")) + + require.Equal(t, "relatedURLs", ToGoPrivate("RelatedURLs")) + require.Equal(t, "imageIDs", ToGoPrivate("ImageIDs")) + require.Equal(t, "fooID", ToGoPrivate("FooID")) + require.Equal(t, "idFoo", ToGoPrivate("IDFoo")) + require.Equal(t, "fooASCII", ToGoPrivate("FooASCII")) + require.Equal(t, "asciiFoo", ToGoPrivate("ASCIIFoo")) + require.Equal(t, "fooUTF8", ToGoPrivate("FooUTF8")) + require.Equal(t, "utf8Foo", ToGoPrivate("UTF8Foo")) + require.Equal(t, "jsonEncoding", ToGoPrivate("JSONEncoding")) + + require.Equal(t, "rangeArg", ToGoPrivate("Range")) + + require.Equal(t, "a", ToGoPrivate("A")) + 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}) + }) + 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")) } func TestCenter(t *testing.T) { From a2716dfd0ee6f60ed16436d7329a1f596c10890e Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 6 Mar 2019 19:13:32 +0900 Subject: [PATCH 3/4] 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 fe51caaf5a..a919a829ec 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 47700b4ef5..cea36a3018 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) { From 855e41b979da8c918b5608ff39adeeeb12f42105 Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 6 Mar 2019 19:19:16 +0900 Subject: [PATCH 4/4] fix ci --- codegen/templates/templates.go | 84 ++-------------------------------- 1 file changed, 4 insertions(+), 80 deletions(-) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index a919a829ec..5f0fda9d64 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -269,17 +269,9 @@ type wordInfo struct { HasCommonInitial bool } +// This function is based on the following code. +// https://github.com/golang/lint/blob/06c8688daad7faa9da5a0c2f163a3d14aac986ca/lint.go#L679 func wordWalker(str string, f func(*wordInfo)) { - - skipRune := func(r rune) bool { - switch r { - case '-', '_': - return true - default: - return false - } - } - runes := []rune(str) w, i := 0, 0 // index of start of word, scan hasCommonInitial := false @@ -287,11 +279,11 @@ func wordWalker(str string, f func(*wordInfo)) { eow := false // whether we hit the end of a word if i+1 == len(runes) { eow = true - } else if skipRune(runes[i+1]) { + } else if isDelimiter(runes[i+1]) { // underscore; shift the remainder forward over any run of underscores eow = true n := 1 - for i+n+1 < len(runes) && skipRune(runes[i+n+1]) { + for i+n+1 < len(runes) && isDelimiter(runes[i+n+1]) { n++ } @@ -376,74 +368,6 @@ func sanitizeKeywords(name string) string { return name } -// copy from https://github.com/golang/lint/blob/06c8688daad7faa9da5a0c2f163a3d14aac986ca/lint.go#L679 -func lintName(name string) string { - // Fast path for simple cases: "_" and all lowercase. - if name == "_" { - return name - } - allLower := true - for _, r := range name { - if !unicode.IsLower(r) { - allLower = false - break - } - } - if allLower { - return name - } - - // Split camelCase at any lower->upper transition, and split on underscores. - // Check each word for common initialisms. - runes := []rune(name) - w, i := 0, 0 // index of start of word, scan - for i+1 <= len(runes) { - eow := false // whether we hit the end of a word - if i+1 == len(runes) { - eow = true - } else if runes[i+1] == '_' { - // underscore; shift the remainder forward over any run of underscores - eow = true - n := 1 - for i+n+1 < len(runes) && runes[i+n+1] == '_' { - n++ - } - - // Leave at most one underscore if the underscore is between two digits - if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) { - n-- - } - - copy(runes[i+1:], runes[i+n+1:]) - runes = runes[:len(runes)-n] - } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) { - // lower->non-lower - eow = true - } - i++ - if !eow { - continue - } - - // [w,i) is a word. - word := string(runes[w:i]) - if u := strings.ToUpper(word); commonInitialisms[u] { - // Keep consistent case, which is lowercase only at the start. - if w == 0 && unicode.IsLower(runes[w]) { - u = strings.ToLower(u) - } - // All the common initialisms are ASCII, - // so we can replace the bytes exactly. - copy(runes[w:], []rune(u)) - } else if w > 0 && strings.ToLower(word) == word { - // already all lowercase, and not the first word, so uppercase the first character. - runes[w] = unicode.ToUpper(runes[w]) - } - w = i - } - return string(runes) -} - // commonInitialisms is a set of common initialisms. // Only add entries that are highly unlikely to be non-initialisms. // For instance, "ID" is fine (Freudian code is rare), but "AND" is not.