Add enum string parsing
This commit is contained in:
parent
cb6de17ea6
commit
d27823de53
2 changed files with 30 additions and 0 deletions
|
@ -16,6 +16,8 @@ import (
|
|||
var (
|
||||
idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz")
|
||||
smartQuoteRgx = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9]*"?(\."?[_a-z][_a-z0-9]*"?)*(\.\*)?$`)
|
||||
|
||||
rgxEnum = regexp.MustCompile(`^enum\((,?'[^']+')+\)$`)
|
||||
)
|
||||
|
||||
var uppercaseWords = map[string]struct{}{
|
||||
|
@ -574,3 +576,16 @@ func GenerateIgnoreTags(tags []string) string {
|
|||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// ParseEnum takes a string that looks like:
|
||||
// enum('one','two') and returns the strings one, two
|
||||
func ParseEnum(s string) []string {
|
||||
if !rgxEnum.MatchString(s) {
|
||||
return nil
|
||||
}
|
||||
|
||||
s = strings.TrimPrefix(s, "enum('")
|
||||
s = strings.TrimSuffix(s, "')")
|
||||
|
||||
return strings.Split(s, "','")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package strmangle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -513,3 +514,17 @@ func TestGenerateIgnoreTags(t *testing.T) {
|
|||
t.Errorf("expected %s, got %s", exp, tags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEnum(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
vals := []string{"one", "two", "three"}
|
||||
toParse := fmt.Sprintf("enum('%s')", strings.Join(vals, "','"))
|
||||
|
||||
gotVals := ParseEnum(toParse)
|
||||
for i, v := range vals {
|
||||
if gotVals[i] != v {
|
||||
t.Errorf("%d) want: %s, got %s", i, v, gotVals[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue