Skip to content

template.go: .section {@} won't output integers #1357

@gopherbot

Description

@gopherbot

by sam.irvine:

What steps will reproduce the problem?

import template "template"
import bytes "bytes"

type astruct struct {
    intval []int
    intval2 int
}

var testTemplate string =
`
{.repeated section intval}{@}{.alternates with},{.or}EMPTY!{.end}
{.section intval2}{@},{.or}EMPTY!{.end}
"{.section intval2}{@}{.end}"<- Quote should not be empty 
`

func TestEmpty(t *testing.T) {
    templ,err := template.Parse(testTemplate,nil)
    if err != nil {
        t.Errorf("Template parse error")
        return
    }
    apply := astruct{intval: []int{10,20,30,40}, intval2: 10}   
    writeBuffer := bytes.NewBuffer(new([512]byte)[0:512])
    _ = templ.Execute(apply,writeBuffer)

    fmt.Printf("Template apply results: %s\n", writeBuffer.String())
}

What is the expected output?
Template apply results: 
10,20,30,40
10
"10"<- Quote should not be empty 


What do you see instead?
Template apply results: 
10,20,30,40
EMPTY!
""<- Quote should not be empty 


Which compiler are you using (5g, 6g, 8g, gccgo)?
6g


Which operating system are you using?
Linux 2.6.35.9-64.fc14.x86_64


Which revision are you using?  (hg identify)
23006c94f1aa

Please provide any additional information below.

reflect.IntValue (and probably others) are missing from the type switch in 

template/template.go: func empty(v reflect.Value) bool 

This result defaults to true.  This causes int types to be considered empty in some
cases and thus skipped in .section template evaluation.  It does not happen all the time
but I didn't dig around long enough to figure out why.  Adding a type switch case to
handle reflect.IntValue: return false resolved the issue for me.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions