Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloading of functions does not work. #1603

Open
krombel opened this issue Jan 3, 2024 · 0 comments
Open

Overloading of functions does not work. #1603

krombel opened this issue Jan 3, 2024 · 0 comments

Comments

@krombel
Copy link

krombel commented Jan 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant