Skip to content

Commit

Permalink
reflect: introduce a valid identifier check func
Browse files Browse the repository at this point in the history
Until now, a valid identifier check was only required for
isValidFieldName. For golang#39528, a valid identifier check will also be
required for type names.

Updates golang#39528.
  • Loading branch information
TheCount committed Jun 14, 2020
1 parent 9701910 commit bc85ee6
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2328,14 +2328,12 @@ func isLetter(ch rune) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch)
}

// isValidFieldName checks if a string is a valid (struct) field name or not.
//
// According to the language spec, a field name should be an identifier.
// isIdentifier checks if a string is a valid identifier or not.
//
// identifier = letter { letter | unicode_digit } .
// letter = unicode_letter | "_" .
func isValidFieldName(fieldName string) bool {
for i, c := range fieldName {
func isIdentifier(id string) bool {
for i, c := range id {
if i == 0 && !isLetter(c) {
return false
}
Expand All @@ -2345,7 +2343,14 @@ func isValidFieldName(fieldName string) bool {
}
}

return len(fieldName) > 0
return len(id) > 0
}

// isValidFieldName checks if a string is a valid (struct) field name or not.
//
// According to the language spec, a field name should be an identifier.
func isValidFieldName(fieldName string) bool {
return isIdentifier(fieldName)
}

// StructOf returns the struct type containing fields.
Expand Down

0 comments on commit bc85ee6

Please sign in to comment.