Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor func splitStructName #1313

Merged
merged 3 commits into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 22 additions & 20 deletions generics.go
Expand Up @@ -10,6 +10,7 @@ import (
"go/ast"
"strings"
"sync"
"unicode"
)

var genericDefinitionsMutex = &sync.RWMutex{}
Expand Down Expand Up @@ -177,39 +178,40 @@ func (pkgDefs *PackagesDefinitions) parametrizeStruct(file *ast.File, original *

// splitStructName splits a generic struct name in his parts
func splitStructName(fullGenericForm string) (string, []string) {
//remove all spaces character
fullGenericForm = strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, fullGenericForm)

// split only at the first '[' and remove the last ']'
if fullGenericForm[len(fullGenericForm)-1] != ']' {
return "", nil
}

genericParams := strings.SplitN(strings.TrimSpace(fullGenericForm)[:len(fullGenericForm)-1], "[", 2)
genericParams := strings.SplitN(fullGenericForm[:len(fullGenericForm)-1], "[", 2)
if len(genericParams) == 1 {
return "", nil
}

// generic type name
genericTypeName := genericParams[0]

// generic params
insideBrackets := 0
lastParam := ""
params := strings.Split(genericParams[1], ",")
genericParams = []string{}
for _, p := range params {
numOpened := strings.Count(p, "[")
numClosed := strings.Count(p, "]")
if numOpened == numClosed && insideBrackets == 0 {
genericParams = append(genericParams, strings.TrimSpace(p))
continue
}

insideBrackets += numOpened - numClosed
lastParam += p + ","

if insideBrackets == 0 {
genericParams = append(genericParams, strings.TrimSpace(strings.TrimRight(lastParam, ",")))
lastParam = ""
depth := 0
genericParams = strings.FieldsFunc(genericParams[1], func(r rune) bool {
if r == '[' {
Copy link
Contributor

@ubogdan ubogdan Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more elegant with a switch case

	switch r {
	case '[':
		depth++
	case ']':
		depth--
	case ',':
		if depth == 0 {
			return true
		}
	default:
		return false
	}

depth++
} else if r == ']' {
depth--
} else if r == ',' && depth == 0 {
return true
}
return false
})
if depth != 0 {
return "", nil
}

return genericTypeName, genericParams
Expand Down
6 changes: 5 additions & 1 deletion generics_test.go
Expand Up @@ -186,13 +186,17 @@ func TestSplitStructNames(t *testing.T) {
assert.Empty(t, field)
assert.Nil(t, params)

field, params = splitStructName("test.Field[string]")
field, params = splitStructName("test.Field[string] ")
assert.Equal(t, "test.Field", field)
assert.Equal(t, []string{"string"}, params)

field, params = splitStructName("test.Field[string, []string]")
assert.Equal(t, "test.Field", field)
assert.Equal(t, []string{"string", "[]string"}, params)

field, params = splitStructName("test.Field[test.Field[ string, []string] ]")
assert.Equal(t, "test.Field", field)
assert.Equal(t, []string{"test.Field[string,[]string]"}, params)
}

func TestGetGenericFieldType(t *testing.T) {
Expand Down