Skip to content

ReflectFrom with embeded structs panics #61

@andreynering

Description

@andreynering

Reproduce script:

package main

import (
    "bytes"
    "fmt"

    "gopkg.in/ini.v1"
)

type Struct1 struct {
    Field1 string
}

type Struct2 struct {
    Struct1
}

func main() {
    v := Struct2{Struct1{"foobar"}}

    cfg := ini.Empty()
    if err := cfg.ReflectFrom(&v); err != nil {
        fmt.Printf("Ops: %v", err)
    }

    buff := bytes.NewBuffer(nil)
    cfg.WriteTo(buff)
    fmt.Println(buff.String())
}

The above gives the following output:

panic: interface conversion: interface is main.Struct1, not time.Time

goroutine 1 [running]:
panic(0x5166c0, 0x10a6bbe0)
        C:/GOROOT/src/runtime/panic.go:481 +0x326
gopkg.in/ini%2ev1.reflectWithProperType(0x8d4068, 0x514f20, 0x10a6bbc0, 0x514f20, 0x10a88168, 0x199, 0x52c970, 0x1, 0x0, 0x0)
        D:/Go/src/gopkg.in/ini.v1/struct.go:318 +0x30a
gopkg.in/ini%2ev1.(*Section).reflectFrom(0x10a66390, 0x514f80, 0x10a88168, 0x199, 0x0, 0x0)
        D:/Go/src/gopkg.in/ini.v1/struct.go:393 +0x72a
gopkg.in/ini%2ev1.(*Section).ReflectFrom(0x10a66390, 0x4ebd80, 0x10a88168, 0x0, 0x0)
        D:/Go/src/gopkg.in/ini.v1/struct.go:412 +0x149
gopkg.in/ini%2ev1.(*File).ReflectFrom(0x10a68580, 0x4ebd80, 0x10a88168, 0x0, 0x0)
        D:/Go/src/gopkg.in/ini.v1/struct.go:417 +0x51
main.main()
        C:/Users/andrey.nering/Desktop/ini_reproduce.go:22 +0x93
exit status 2

I guess the error started after 959d90a

I suspect from this and this lines.

Given that reflect.TypeOf(time.Now()).Kind() is struct, so any struct will match that line. See https://play.golang.org/p/16o9O2eDNq

Checkouting to 927d8d7 (one commit before the cited) fixed this issue for me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions