Skip to content

HMGet can't scan type time.Time #2161

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

Closed
ryan961 opened this issue Jul 19, 2022 · 8 comments
Closed

HMGet can't scan type time.Time #2161

ryan961 opened this issue Jul 19, 2022 · 8 comments

Comments

@ryan961
Copy link

ryan961 commented Jul 19, 2022

Version: github.com/go-redis/redis/v8 v8.11.4

Hi, I found the test case(https://github.com/go-redis/redis/blob/e061db8c13fd6b5e007d73370873026e360719de/redis_test.go#L274),`time.Time` can be scanned to object. But it's wrong when I used it in hmget(...).scan(&u)
image

type User struct {
	Name string `json:"name" redis:"name"`
	Age  int64  `json:"age" redis:"age"`
	CreateAt time.Time `json:"createAt" redis:"createAt"`
}

func main() {
                ......


		fmt.Println(cli.HMSet(ctx, "joe", map[string]interface{}{
			"name":     "joe",
			"age":      10,
			"createAt": time.Now(),
		}).Result())

		u := User{}
		fmt.Println(cli.HMGet(ctx, "joe", "name", "age", "city", "createAt").Scan(&u))
		fmt.Println(u)
                
                ......
}

image

@edoger
Copy link

edoger commented Nov 9, 2022

Ah, this library also has this feature! Map the data in the hash table to the structure!

@ntswamp
Copy link

ntswamp commented Nov 25, 2022

@edoger
what are you saying dude?

@edoger
Copy link

edoger commented Nov 26, 2022

what are you saying dude?

@ntswamp I never knew there was such a feature that we implemented a similar one ourselves!

@leoantony72
Copy link

Any update on this issue?

@michelemendel
Copy link

I also have issues with this. Reading the data from redis-cli, the date is correct.

Btw, according to
https://github.com/redis/go-redis/blob/master/commands_test.go#L1324
CreateAt should use redis:"time"

@ryan961
Copy link
Author

ryan961 commented Mar 16, 2023

I followed the author's update on Scanner interface and It solves the hmget scan problem perfectly. But the hset struct field (type TimeValue) is not successful.

parsing time "\x01\x00\x00\x00\x0e\xdb\xa4\xaf\xe7\x14\xf4\xe6\x10\x01\xe0" as "2006-01-02T15:04:05Z07:00": cannot parse "\x01\x00\x00\x00\x0e\xdb\xa4\xaf\xe7\x14\xf4\xe6\x10\x01\xe0" as "2006"

Does the author will add functions similar to scanner interface in the future?

func (t *TimeValue) RedisValue() string {
	return t.Time.String()
}

@monkey92t
Copy link
Collaborator

If time. Time:

type MyStruct struct {
        // call:time.UnmarshalText(data) , parseRFC3339
        Time time.Time `redis:"time"`
}

Custom time format:

type TimeValue struct {
	time.Time
}

func (t *TimeValue) ScanRedis(s string) (err error) {
	t.Time, err = time.Parse(time.Date, s)
	return
}

type data struct {
        Key1 string    `redis:"key1"`
        Key2 int       `redis:"key2"`
        Time TimeValue `redis:"time"`
}

var d data
res := client.MGet(ctx, "key1", "key2", "time")
res.Scan(&d)

@ndyakov
Copy link
Member

ndyakov commented Jun 5, 2025

This can be either resolved as @monkey92t suggested or @michelemendel, you can use newer version. This should be supported right now.

@ndyakov ndyakov closed this as completed Jun 5, 2025
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

7 participants