go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

Allow struct on HSet

Open wjdqhry opened this issue 3 years ago • 6 comments

Each time, struct had to be converted to map and put it in. So I thought it would be better to embed it inside the function.

wjdqhry avatar Dec 17 '21 02:12 wjdqhry

I want to use this feature

YuriPuck avatar Jan 16 '22 09:01 YuriPuck

Should we use the "json" tag? I feel like it's better handled by the person who needs it.

func structToMap() {
    ....
}

client.Hset(ctx, "key", structToMap(items))

monkey92t avatar Mar 17 '22 14:03 monkey92t

Should we use the "json" tag? I feel like it's better handled by the person who needs it.

func structToMap() {
    ....
}

client.Hset(ctx, "key", structToMap(items))

Thank you for your reply.

I felt that the experience of using the library was reduced because the person who wanted it had to make it from the outside, and I thought it would be ideal to solve it internally.

It doesn't have to be a "json" tag, but I think we need a tag for our hash keys.

wjdqhry avatar Mar 18 '22 05:03 wjdqhry

We should think more, like tag="" or tag="-", or for types like:

type Demo struct {
    Pers []int
    Ptr *struct
    Map map[string]*struct
    ...
}

monkey92t avatar Mar 18 '22 15:03 monkey92t

I modified the tag name. The example has also been modified.

But if you don't think it's necessary, you can close the PR!

wjdqhry avatar Apr 28 '22 11:04 wjdqhry

I was wondering why this does not exist yet.

Thanks for the code - I guess you should maybe use the tag redis as it is already used for the other way round if you scan a HGetAll to a struct

michaelfeinbier avatar Sep 26 '22 08:09 michaelfeinbier

Sorry i'm just too late.. Thank you for your feedback!

I accepted your feedback and I just changed serialize key "key" to "redis"

wjdqhry avatar Jan 19 '23 04:01 wjdqhry

I added support for structure types in the appendArgs function, so that MSet, MSetNX functions can also benefit.

I added the description of using the struct type in the API comments, and added the test function.

We should note that for complex field types of structures, the encoding.BinaryMarshaler interface should be implemented.

Synchronized with v9.

monkey92t avatar Jan 19 '23 07:01 monkey92t