diff options
| author | Rob Pike <r@golang.org> | 2012-04-24 13:11:59 +1000 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2012-04-24 13:11:59 +1000 |
| commit | 11820899a58094be1afa22987ce080cb2fb66b86 (patch) | |
| tree | 9d48a00acf665ff9e1881798e2f3a7f9000c63a7 /src/pkg/text/template/exec.go | |
| parent | cc5cbee1b6a942d2f55c01697f464be9d2a56818 (diff) | |
| download | go-11820899a58094be1afa22987ce080cb2fb66b86.tar.xz | |
text/template: improve the error reporting for unexported fields.
Changes suggested by rsc after last CL.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6117044
Diffstat (limited to 'src/pkg/text/template/exec.go')
| -rw-r--r-- | src/pkg/text/template/exec.go | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index b8d23d43f9..0c633e6bab 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -12,8 +12,6 @@ import ( "sort" "strings" "text/template/parse" - "unicode" - "unicode/utf8" ) // state represents the state of an execution. It's not part of the @@ -426,17 +424,16 @@ func (s *state) evalField(dot reflect.Value, fieldName string, args []parse.Node tField, ok := receiver.Type().FieldByName(fieldName) if ok { field := receiver.FieldByIndex(tField.Index) - if tField.PkgPath == "" { // field is exported - // If it's a function, we must call it. - if hasArgs { - s.errorf("%s has arguments but cannot be invoked as function", fieldName) - } - return field + if tField.PkgPath != "" { // field is unexported + s.errorf("%s is an unexported field of struct type %s", fieldName, typ) } + // If it's a function, we must call it. + if hasArgs { + s.errorf("%s has arguments but cannot be invoked as function", fieldName) + } + return field } - if !isExported(fieldName) { - s.errorf("%s is not an exported field of struct type %s", fieldName, typ) - } + s.errorf("%s is not a field of struct type %s", fieldName, typ) case reflect.Map: // If it's a map, attempt to use the field name as a key. nameVal := reflect.ValueOf(fieldName) @@ -451,12 +448,6 @@ func (s *state) evalField(dot reflect.Value, fieldName string, args []parse.Node panic("not reached") } -// isExported reports whether the field name (which starts with a period) can be accessed. -func isExported(fieldName string) bool { - r, _ := utf8.DecodeRuneInString(fieldName[1:]) // drop the period - return unicode.IsUpper(r) -} - var ( errorType = reflect.TypeOf((*error)(nil)).Elem() fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() |
