Skip to content

Commit

Permalink
refactor func splitStructName (#1313)
Browse files Browse the repository at this point in the history
* refactor func splitStructName

Signed-off-by: sdghchj <chengjin@cmiot.chinamobile.com>
  • Loading branch information
sdghchj committed Aug 31, 2022
1 parent cf1c4a7 commit e7ccdf4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
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 == '[' {
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

0 comments on commit e7ccdf4

Please sign in to comment.