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

fix(redis): cluster list crash #3687

Merged
merged 9 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/human/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func marshalStruct(value reflect.Value, opt *MarshalOpt) (string, error) {
sectionsStrs := []string(nil)
sectionFieldNames := map[string]bool{}
for _, section := range opt.Sections {
sectionStr, err := marshalSection(section, value, subOpts)
sectionStr, err := marshalSection(section, value, opt.subOption(section.FieldName))
if err != nil {
return "", err
}
Expand Down
16 changes: 13 additions & 3 deletions internal/human/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// MarshalOpt is hydrated by core.View
type MarshalOpt struct {
Title string
Fields []*MarshalFieldOpt
Sections []*MarshalSection
Title string
Fields []*MarshalFieldOpt
Sections []*MarshalSection
SubOptions map[string]*MarshalOpt

// Is set to true if we are marshaling a table cell
TableCell bool
Expand All @@ -19,6 +20,15 @@ type MarshalOpt struct {
DisableShrinking bool
}

func (m *MarshalOpt) subOption(section string) *MarshalOpt {
subOpt, exists := m.SubOptions[section]
if exists {
return subOpt
}

return &MarshalOpt{}
}

type MarshalFieldOpt struct {
FieldName string
Label string
Expand Down
1 change: 0 additions & 1 deletion internal/namespaces/redis/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

human.RegisterMarshalerFunc(redis.Cluster{}, redisClusterGetMarshalerFunc)
human.RegisterMarshalerFunc(redis.Cluster{}.Endpoints, redisEndpointsClusterGetMarshalerFunc)

cmds.Merge(core.NewCommands(clusterWaitCommand()))
cmds.MustFind("redis", "cluster", "create").Override(clusterCreateBuilder)
Expand Down
44 changes: 18 additions & 26 deletions internal/namespaces/redis/v1/custom_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package redis
import (
"context"
"errors"
"log"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -170,34 +169,9 @@ func redisSettingAddBuilder(c *core.Command) *core.Command {
return c
}

func redisEndpointsClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp []*redis.Endpoint
redisEndpointsClusterResponse := tmp(i.([]*redis.Endpoint))
opt.Fields = []*human.MarshalFieldOpt{
{
FieldName: "ID",
Label: "ID",
},
{
FieldName: "Port",
Label: "Port",
},
{
FieldName: "IPs",
Label: "IPs",
},
}
str, err := human.Marshal(redisEndpointsClusterResponse, opt)
if err != nil {
return "", err
}
return str, nil
}

func redisClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp redis.Cluster
redisClusterResponse := tmp(i.(redis.Cluster))
log.Println("redis ", i.(redis.Cluster).Endpoints[0])
opt.Sections = []*human.MarshalSection{
{
FieldName: "Endpoints",
Expand All @@ -208,6 +182,24 @@ func redisClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string,
Title: "ACLRules",
},
}
opt.SubOptions = map[string]*human.MarshalOpt{
"Endpoints": {
Fields: []*human.MarshalFieldOpt{
{
FieldName: "ID",
Label: "ID",
},
{
FieldName: "Port",
Label: "Port",
},
{
FieldName: "IPs",
Label: "IPs",
},
},
},
}
str, err := human.Marshal(redisClusterResponse, opt)
if err != nil {
return "", err
Expand Down
Loading