traefik / yaegi

Yaegi is Another Elegant Go Interpreter
https://pkg.go.dev/github.com/traefik/yaegi
Apache License 2.0
6.78k stars 341 forks source link

Overloading of functions does not work. #1603

Open krombel opened 6 months ago

krombel commented 6 months ago

The following program sample.go triggers an unexpected result

package main

import (
    "encoding/json"
    "fmt"
    "time"
)

type jsonTime time.Time

func (j *jsonTime) UnmarshalJSON(b []byte) error {
    var n json.Number
    if err := json.Unmarshal(b, &n); err != nil {
        return err
    }
    var unix int64

    if t, err := n.Int64(); err == nil {
        unix = t
    } else {
        f, err := n.Float64()
        if err != nil {
            return err
        }
        unix = int64(f)
    }
    *j = jsonTime(time.Unix(unix, 0))
    return nil
}

type data struct {
    A jsonTime  `json:"a"`
    B *jsonTime `json:"b"`
}

func main() {
    string := []byte(`{"a":1704303550,"b":1704303250}`)

    var parsed data

    err := json.Unmarshal(string, &parsed)
    fmt.Printf("Err: %v\n", err)

    fmt.Printf("a: %s\n", time.Time(parsed.A))
    fmt.Printf("b: %v\n", time.Time(*parsed.B))
}

Expected result

Err: <nil>
a: 2024-01-03 17:39:10 +0000 UTC
b: 2024-01-03 17:34:10 +0000 UTC

Got

Err: json: cannot unmarshal string into Go struct field .a of type time.Time
a: 0001-01-01 00:00:00 +0000 UTC
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4be9ae]

Yaegi Version

v0.15.1

Additional Notes

No response