Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
fe59d86d
Commit
fe59d86d
authored
Aug 12, 2011
by
David Symonds
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: fix implementation of printValue.
R=r CC=golang-dev
https://golang.org/cl/4878042
parent
3cca9e0b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
9 deletions
+18
-9
src/pkg/exp/template/exec.go
src/pkg/exp/template/exec.go
+13
-9
src/pkg/exp/template/exec_test.go
src/pkg/exp/template/exec_test.go
+5
-0
No files found.
src/pkg/exp/template/exec.go
View file @
fe59d86d
...
...
@@ -620,19 +620,23 @@ func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
// printValue writes the textual representation of the value to the output of
// the template.
func
(
s
*
state
)
printValue
(
n
parse
.
Node
,
v
reflect
.
Value
)
{
if
v
.
Kind
()
==
reflect
.
Ptr
{
v
,
_
=
indirect
(
v
)
// fmt.Fprint handles nil.
}
if
!
v
.
IsValid
()
{
fmt
.
Fprint
(
s
.
wr
,
"<no value>"
)
return
}
if
!
v
.
Type
()
.
Implements
(
fmtStringerType
)
{
if
v
.
CanAddr
()
&&
reflect
.
PtrTo
(
v
.
Type
())
.
Implements
(
fmtStringerType
)
{
v
=
v
.
Addr
()
}
else
{
switch
v
.
Kind
()
{
case
reflect
.
Ptr
:
v
,
_
=
indirect
(
v
)
// fmt.Fprint handles nil.
case
reflect
.
Chan
,
reflect
.
Func
,
reflect
.
Interface
:
case
reflect
.
Chan
,
reflect
.
Func
:
s
.
errorf
(
"can't print %s of type %s"
,
n
,
v
.
Type
())
}
// If it's a value but the pointer implements Stringer, use the pointer.
if
v
.
Kind
()
!=
reflect
.
Ptr
&&
v
.
CanAddr
()
&&
reflect
.
PtrTo
(
v
.
Type
())
.
Implements
(
fmtStringerType
)
{
v
=
v
.
Addr
()
}
}
fmt
.
Fprint
(
s
.
wr
,
v
.
Interface
())
}
src/pkg/exp/template/exec_test.go
View file @
fe59d86d
...
...
@@ -48,6 +48,8 @@ type T struct {
Empty4
interface
{}
// Non-empty interface.
NonEmptyInterface
I
// Stringer.
Str
fmt
.
Stringer
// Pointers
PI
*
int
PSI
*
[]
int
...
...
@@ -92,6 +94,7 @@ var tVal = &T{
Empty3
:
[]
int
{
7
,
8
},
Empty4
:
&
U
{
"UinEmpty"
},
NonEmptyInterface
:
new
(
T
),
Str
:
os
.
NewError
(
"foozle"
),
PI
:
newInt
(
23
),
PSI
:
newIntSlice
(
21
,
22
,
23
),
Tmpl
:
Must
(
New
(
"x"
)
.
Parse
(
"test template"
)),
// "x" is the value of .X
...
...
@@ -396,6 +399,8 @@ var execTests = []execTest{
{
"bug3"
,
"{{with $}}{{.Method0}}{{end}}"
,
"M0"
,
tVal
,
true
},
// Nil interface values in if.
{
"bug4"
,
"{{if .Empty0}}non-nil{{else}}nil{{end}}"
,
"nil"
,
tVal
,
true
},
// Stringer.
{
"bug5"
,
"{{.Str}}"
,
"foozle"
,
tVal
,
true
},
}
func
zeroArgs
()
string
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment