Skip to content

Commit b3c24a0

Browse files
Laure-diCodelax
andauthored
fix(redis): cluster list crash (#3687)
Co-authored-by: Jules Castéran <[email protected]> Co-authored-by: Jules Casteran <[email protected]>
1 parent 127f1ab commit b3c24a0

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

internal/human/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func marshalStruct(value reflect.Value, opt *MarshalOpt) (string, error) {
9898
sectionsStrs := []string(nil)
9999
sectionFieldNames := map[string]bool{}
100100
for _, section := range opt.Sections {
101-
sectionStr, err := marshalSection(section, value, subOpts)
101+
sectionStr, err := marshalSection(section, value, opt.subOption(section.FieldName))
102102
if err != nil {
103103
return "", err
104104
}

internal/human/specs.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88

99
// MarshalOpt is hydrated by core.View
1010
type MarshalOpt struct {
11-
Title string
12-
Fields []*MarshalFieldOpt
13-
Sections []*MarshalSection
11+
Title string
12+
Fields []*MarshalFieldOpt
13+
Sections []*MarshalSection
14+
SubOptions map[string]*MarshalOpt
1415

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

23+
func (m *MarshalOpt) subOption(section string) *MarshalOpt {
24+
subOpt, exists := m.SubOptions[section]
25+
if exists {
26+
return subOpt
27+
}
28+
29+
return &MarshalOpt{}
30+
}
31+
2232
type MarshalFieldOpt struct {
2333
FieldName string
2434
Label string

internal/namespaces/redis/v1/custom.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ func GetCommands() *core.Commands {
1010
cmds := GetGeneratedCommands()
1111

1212
human.RegisterMarshalerFunc(redis.Cluster{}, redisClusterGetMarshalerFunc)
13-
human.RegisterMarshalerFunc(redis.Cluster{}.Endpoints, redisEndpointsClusterGetMarshalerFunc)
1413

1514
cmds.Merge(core.NewCommands(clusterWaitCommand()))
1615
cmds.MustFind("redis", "cluster", "create").Override(clusterCreateBuilder)

internal/namespaces/redis/v1/custom_cluster.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package redis
33
import (
44
"context"
55
"errors"
6-
"log"
76
"net/http"
87
"reflect"
98
"strings"
@@ -170,34 +169,9 @@ func redisSettingAddBuilder(c *core.Command) *core.Command {
170169
return c
171170
}
172171

173-
func redisEndpointsClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
174-
type tmp []*redis.Endpoint
175-
redisEndpointsClusterResponse := tmp(i.([]*redis.Endpoint))
176-
opt.Fields = []*human.MarshalFieldOpt{
177-
{
178-
FieldName: "ID",
179-
Label: "ID",
180-
},
181-
{
182-
FieldName: "Port",
183-
Label: "Port",
184-
},
185-
{
186-
FieldName: "IPs",
187-
Label: "IPs",
188-
},
189-
}
190-
str, err := human.Marshal(redisEndpointsClusterResponse, opt)
191-
if err != nil {
192-
return "", err
193-
}
194-
return str, nil
195-
}
196-
197172
func redisClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
198173
type tmp redis.Cluster
199174
redisClusterResponse := tmp(i.(redis.Cluster))
200-
log.Println("redis ", i.(redis.Cluster).Endpoints[0])
201175
opt.Sections = []*human.MarshalSection{
202176
{
203177
FieldName: "Endpoints",
@@ -208,6 +182,24 @@ func redisClusterGetMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string,
208182
Title: "ACLRules",
209183
},
210184
}
185+
opt.SubOptions = map[string]*human.MarshalOpt{
186+
"Endpoints": {
187+
Fields: []*human.MarshalFieldOpt{
188+
{
189+
FieldName: "ID",
190+
Label: "ID",
191+
},
192+
{
193+
FieldName: "Port",
194+
Label: "Port",
195+
},
196+
{
197+
FieldName: "IPs",
198+
Label: "IPs",
199+
},
200+
},
201+
},
202+
}
211203
str, err := human.Marshal(redisClusterResponse, opt)
212204
if err != nil {
213205
return "", err

0 commit comments

Comments
 (0)