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

Redis Hset Struct #270

Closed
tizips opened this issue Dec 19, 2024 · 5 comments
Closed

Redis Hset Struct #270

tizips opened this issue Dec 19, 2024 · 5 comments
Labels
Bug Report a reproducible bug or regression

Comments

@tizips
Copy link

tizips commented Dec 19, 2024

Hello,

I encountered an issue with the following code:

type Template struct {
	BirthDate      carbon.Carbon  `redis:"birth_date" carbon:"type:date"` // 出生日期
}

*redis.Client.HSet(ctx, key, Template{}).Result()

golang version: 1.23

carbon version: v2.5.2

redis: v9.7.0

github.com/redis/go-redis/v9

redis: can't marshal carbon.Carbon (implement encoding.BinaryMarshaler)

Thanks!

@tizips tizips added the Bug Report a reproducible bug or regression label Dec 19, 2024
@Markle-ddp
Copy link
Contributor

you can implement encoding.BinaryMarshaler on Template struct to fix this error

@gouguoyin
Copy link
Collaborator

If possible, please submit a repair PR

@Markle-ddp
Copy link
Contributor

it runs ok :

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/dromara/carbon/v2"
	"github.com/redis/go-redis/v9"
	"time"
)

type Template struct {
	BirthDate carbon.Carbon `redis:"birth_date" carbon:"type:date"` // 出生日期
}

func (t Template) MarshalBinary() ([]byte, error) {
	return json.Marshal(t)
}

func main() {
	redisCli := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
	ctx := context.TODO()

	key := "testKey"
	val, err := redisCli.Set(ctx, key, Template{
		carbon.Now(),
	}, time.Minute).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println(val)
	// output: OK

	result, err := redisCli.Get(ctx, key).Result()
	if err != nil {
		return
	}
	fmt.Println(result)
	// output: {"BirthDate":"2024-12-30 14:57:40"}
}

@Markle-ddp
Copy link
Contributor

If possible, please submit a repair PR

#274

@gouguoyin
Copy link
Collaborator

Thank you for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report a reproducible bug or regression
Projects
None yet
Development

No branches or pull requests

3 participants