Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template won't evaluate fields that are interface type as the underlying type

Using golang html/template (same behavior with text/template). If I have a struct with a member that is of an interface type, I cannot access members of the underlying type (specifically trying to access fields that are on a struct that implements interface InnerInterface but is return via the InnerInterface interface type, not the struct type).

http://play.golang.org/p/ZH8wSK83oM

package main

import "fmt"
import "os"
import "html/template"

type InnerInterface interface{ InnerSomeMethod() }

type MyInnerStruct struct { Title string }
func (mis MyInnerStruct)InnerSomeMethod() { fmt.Println("Just to show we're satisfying the interface") }

type MyOuterStruct struct { Inner InnerInterface }


func main() {

    fmt.Println("Starting")


    arg := MyOuterStruct{Inner:MyInnerStruct{Title:"test1"}}

    err := template.Must(template.New("testtmpl").Parse("{{.Inner.Title}}")).Execute(os.Stdout, arg)
    if err != nil { panic(err) }

}

Changing: type MyOuterStruct struct { Inner InnerInterface } to a totally generic interface, i.e. type MyOuterStruct struct { Inner interface{} } makes it render properly. This leads me to believe that interface{} is treated specially by the rendering engine.

Is there a better way to do this than to use interface{} whenever I want to be able to dynamically evaluate fields like this?

like image 923
Brad Peabody Avatar asked Oct 23 '13 23:10

Brad Peabody


1 Answers

You're correct with saying that interface{} is handled differently by the rendering engine. Only interface{} values are unpacked, interface values that have a method set are not. I suppose the reasoning behind this is that if you have a interface type, you specifically limit the type to the method set. Therefore, you don't want the template engine trying to access members that may lie behind that interface.

The 'problem' is caused by the function indirect in exec.go:

func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
    for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
        if v.IsNil() {
            return v, true
        }
        if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
            break
        }
    }
    return v, false
}

This method is called to get to the deepest value of a reflected value. Suppose you have a pointer on a pointer on a pointer, this function will get return the last of these. The same goes for interface values. The crux is that as soon as a interface value has more than 0 methods, the indirection stops there. Exactly the behaviour you're describing.

As this seems to be intended behaviour, what you can do is to define a Title() string method in your interface and let it return the string.

like image 71
nemo Avatar answered Sep 26 '22 13:09

nemo