Skip to content

Commit

Permalink
Make ToGoName prefix configurable when first char is not a letter
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fredbi committed Jul 4, 2019
1 parent beaa044 commit 49551d5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
20 changes: 19 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package swag

import (
"reflect"
"regexp"
"strings"
"unicode"
)
Expand All @@ -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{
Expand Down Expand Up @@ -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) {
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 49551d5

Please sign in to comment.