Merge pull request #284 from colincross/uppercase_properties

Fix PropertyNameForField for X86.
This commit is contained in:
colincross 2020-02-05 17:26:16 -08:00 committed by GitHub
commit c42ef965a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -24,15 +24,16 @@ import (
// PropertyNameForField converts the name of a field in property struct to the property name that
// might appear in a Blueprints file. Since the property struct fields must always be exported
// to be accessed with reflection and the canonical Blueprints style is lowercased names, it
// lower cases the first rune in the field name unless the field name contains multiple runes none
// of which are lowercase, in which case it returns the field name as-is.
// lower cases the first rune in the field name unless the field name contains an uppercase rune
// after the first rune (which is always uppercase), and no lowercase runes.
func PropertyNameForField(fieldName string) string {
r, size := utf8.DecodeRuneInString(fieldName)
propertyName := string(unicode.ToLower(r))
if size == len(fieldName) {
return propertyName
}
if strings.IndexFunc(fieldName[size:], unicode.IsLower) == -1 {
if strings.IndexFunc(fieldName[size:], unicode.IsLower) == -1 &&
strings.IndexFunc(fieldName[size:], unicode.IsUpper) != -1 {
return fieldName
}
if len(fieldName) > size {

View file

@ -42,6 +42,26 @@ func TestPropertyNameForField(t *testing.T) {
input: "StRiNg",
want: "stRiNg",
},
{
name: "underscore",
input: "Under_score",
want: "under_score",
},
{
name: "uppercase underscore",
input: "UNDER_SCORE",
want: "UNDER_SCORE",
},
{
name: "x86",
input: "X86",
want: "x86",
},
{
name: "x86_64",
input: "X86_64",
want: "x86_64",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {