From 49551d5739effa65faf17a457dcb173ef7b0c1b2 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Mon, 24 Jun 2019 09:29:59 +0200 Subject: [PATCH 1/3] Make ToGoName prefix configurable when first char is not a letter This will allow go-swagger to plus in here its pascalize func and contribute fixing go-swagger/go-swagger#1937 (inconsistent naming rules) Signed-off-by: Frederic BIDON --- util.go | 20 +++++++++++++++++++- util_test.go | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 6d6cfc5..0c1685e 100644 --- a/util.go +++ b/util.go @@ -16,6 +16,7 @@ package swag import ( "reflect" + "regexp" "strings" "unicode" ) @@ -28,6 +29,20 @@ var initialisms []string var isInitialism func(string) bool +var ( + splitRex1 *regexp.Regexp + splitRex2 *regexp.Regexp + splitReplacer *strings.Replacer +) + +// GoNamePrefixFunc sets an optional rule to prefix go names +// which do not start with a letter. +// +// e.g. to help converting "123" into "{prefix}123" +// +// The default is to prefix with "X" +var GoNamePrefixFunc func(string) string + func init() { // Taken from https://github.com/golang/lint/blob/3390df4df2787994aea98de825b964ac7944b817/lint.go#L732-L769 var configuredInitialisms = map[string]bool{ @@ -291,7 +306,10 @@ func ToGoName(name string) string { // Only prefix with X when the first character isn't an ascii letter first := []rune(result)[0] if !unicode.IsLetter(first) || (first > unicode.MaxASCII && !unicode.IsUpper(first)) { - result = "X" + result + if GoNamePrefixFunc == nil { + return "X" + result + } + result = GoNamePrefixFunc(name) + result } first = []rune(result)[0] if unicode.IsLetter(first) && !unicode.IsUpper(first) { diff --git a/util_test.go b/util_test.go index 3af9f12..5af0d4d 100644 --- a/util_test.go +++ b/util_test.go @@ -16,13 +16,14 @@ package swag import ( "fmt" - "github.com/stretchr/testify/require" "log" "strings" "testing" "time" "unicode" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" ) @@ -403,6 +404,7 @@ func TestCamelize(t *testing.T) { {"ELB.HTTPLoadBalancer", "Elb.httploadbalancer"}, {"elbHTTPLoadBalancer", "Elbhttploadbalancer"}, {"ELBHTTPLoadBalancer", "Elbhttploadbalancer"}, + {"12ab", "12ab"}, } for _, sample := range samples { @@ -455,3 +457,36 @@ func TestToVarName(t *testing.T) { assert.Equalf(t, sample.out, res, "expected ToVarName(%q)=%q, got %q", sample.str, sample.out, res) } } + +func TestToGoNameUnicode(t *testing.T) { + defer func() { GoNamePrefixFunc = nil }() + GoNamePrefixFunc = func(name string) string { + // this is the pascalize func from go-swagger codegen + arg := []rune(name) + if len(arg) == 0 || arg[0] > '9' { + return "" + } + if arg[0] == '+' { + return "Plus" + } + if arg[0] == '-' { + return "Minus" + } + + return "Nr" + } + + samples := []translationSample{ + {"123_a", "Nr123a"}, + {"!123_a", "Bang123a"}, + {"+123_a", "Plus123a"}, + {"abc", "Abc"}, + {"éabc", "Éabc"}, + {":éabc", "Éabc"}, + // TODO: non unicode char + } + + for _, sample := range samples { + assert.Equal(t, sample.out, ToGoName(sample.str)) + } +} From 5c77b6af9555fdde85eaee714593649acfe1069b Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Thu, 4 Jul 2019 16:03:58 +0200 Subject: [PATCH 2/3] Fixed a few linting issues Signed-off-by: Frederic BIDON --- convert.go | 7 ++++--- json.go | 2 +- util.go | 7 ------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/convert.go b/convert.go index 4e446ff..7da35c3 100644 --- a/convert.go +++ b/convert.go @@ -39,11 +39,12 @@ func IsFloat64AJSONInteger(f float64) bool { diff := math.Abs(f - g) // more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases - if f == g { // best case + switch { + case f == g: // best case return true - } else if f == float64(int64(f)) || f == float64(uint64(f)) { // optimistic case + case f == float64(int64(f)) || f == float64(uint64(f)): // optimistic case return true - } else if f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64 { // very close to 0 values + case f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64: // very close to 0 values return diff < (epsilon * math.SmallestNonzeroFloat64) } // check the relative error diff --git a/json.go b/json.go index 62ab15e..edf93d8 100644 --- a/json.go +++ b/json.go @@ -99,7 +99,7 @@ func ConcatJSON(blobs ...[]byte) []byte { last := len(blobs) - 1 for blobs[last] == nil || bytes.Equal(blobs[last], nullJSON) { // strips trailing null objects - last = last - 1 + last-- if last < 0 { // there was nothing but "null"s or nil... return nil diff --git a/util.go b/util.go index 0c1685e..9eac16a 100644 --- a/util.go +++ b/util.go @@ -16,7 +16,6 @@ package swag import ( "reflect" - "regexp" "strings" "unicode" ) @@ -29,12 +28,6 @@ var initialisms []string var isInitialism func(string) bool -var ( - splitRex1 *regexp.Regexp - splitRex2 *regexp.Regexp - splitReplacer *strings.Replacer -) - // GoNamePrefixFunc sets an optional rule to prefix go names // which do not start with a letter. // From 39c40256c69c0c3b013b7f59fd3053dc6abbab25 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Thu, 4 Jul 2019 18:16:22 +0200 Subject: [PATCH 3/3] Completed unit test to cover the linting job Signed-off-by: Frederic BIDON --- convert_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/convert_test.go b/convert_test.go index f54ae23..18f345d 100644 --- a/convert_test.go +++ b/convert_test.go @@ -208,6 +208,10 @@ func TestIsFloat64AJSONInteger(t *testing.T) { assert.True(t, IsFloat64AJSONInteger(maxJSONFloat)) assert.True(t, IsFloat64AJSONInteger(minJSONFloat)) assert.True(t, IsFloat64AJSONInteger(1/0.01*67.15000001)) + assert.False(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64)) + assert.False(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/2)) + assert.True(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/3)) + assert.True(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/4)) } func TestFormatBool(t *testing.T) {