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

If B struct is embedded in A struct, how to **Parse** it by HGetAll().Scan(&A)

Open pplmx opened this issue 2 years ago • 1 comments

Expected Behavior

Can be parsed correctly

Current Behavior

B struct is some default value, cannot read from the Redis

task: {PO:{_:[] _:{} ID: CreatedTime:0 UpdatedTime:0 DeletedTime:0} TaskId:5 Status:2}

Possible Solution

None

Steps to Reproduce

image

package main

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
)

type PO struct {
	_           [0]func()
	_           struct{}
	ID          string `redis:"id"`
	CreatedTime int64  `redis:"created_time"`
	UpdatedTime int64  `redis:"updated_time"`
	DeletedTime int64  `redis:"deleted_time"`
}

type Task struct {
	PO
	TaskId int `redis:"task_id"`
	Status int `redis:"status"`
}

func main() {

	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	ctx := context.Background()
	var task Task
	err := rdb.HGetAll(ctx, "task_hash_ee1c1e1e-d928-46a9-9948-68f50a0bef6d").Scan(&task)
	if err != nil {
		return
	}
	fmt.Println(fmt.Sprintf("task: %+v", task))

}

Context (Environment)

go: 1.17.6 go-redis: v8.11.4

Detailed Description

None

Possible Implementation

None

pplmx avatar Jan 24 '22 02:01 pplmx

Here is my solution to scan multiple times based on the number of embedded structs. FYI.

hashResultSet := HGetAll()
err = hashResultSet.Scan(&A)
err = hashResultSet.Scan(&A.B)

pplmx avatar Jan 24 '22 03:01 pplmx

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Sep 22 '23 00:09 github-actions[bot]