Skip to content

Commit

Permalink
fix(redis): cluster list crash (scaleway#3687)
Browse files Browse the repository at this point in the history
Co-authored-by: Jules Castéran <jules.casteran@gmail.com>
Co-authored-by: Jules Casteran <jcasteran@scaleway.com>
  • Loading branch information
3 people authored and jremy42 committed Apr 3, 2024
1 parent 47f2278 commit 8a37c07
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
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

0 comments on commit 8a37c07

Please sign in to comment.