Skip to content

Commit

Permalink
Merge pull request #35 from fredbi/fix-1937
Browse files Browse the repository at this point in the history
Make default prefix in ToGoName configurable so go-swagger can plug in the pascalize func.
  • Loading branch information
casualjim committed Jul 4, 2019
2 parents beaa044 + 39c4025 commit de649ff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
7 changes: 4 additions & 3 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ var initialisms []string

var isInitialism func(string) bool

// 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{
Expand Down Expand Up @@ -291,7 +299,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) {
Expand Down
37 changes: 36 additions & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -403,6 +404,7 @@ func TestCamelize(t *testing.T) {
{"ELB.HTTPLoadBalancer", "Elb.httploadbalancer"},
{"elbHTTPLoadBalancer", "Elbhttploadbalancer"},
{"ELBHTTPLoadBalancer", "Elbhttploadbalancer"},
{"12ab", "12ab"},
}

for _, sample := range samples {
Expand Down Expand Up @@ -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))
}
}

0 comments on commit de649ff

Please sign in to comment.