3
3
import redis .clients .jedis .Builder ;
4
4
5
5
import java .util .Collections ;
6
+ import java .util .HashMap ;
6
7
import java .util .List ;
8
+ import java .util .Map ;
7
9
8
- import static redis .clients .jedis .BuilderFactory .STRING_LIST ;
9
10
import static redis .clients .jedis .BuilderFactory .LONG ;
11
+ import static redis .clients .jedis .BuilderFactory .STRING ;
12
+ import static redis .clients .jedis .BuilderFactory .STRING_LIST ;
10
13
11
14
public class CommandInfo {
15
+
16
+ private final String name ;
12
17
private final long arity ;
13
18
private final List <String > flags ;
14
19
private final long firstKey ;
15
20
private final long lastKey ;
16
21
private final long step ;
17
22
private final List <String > aclCategories ;
18
23
private final List <String > tips ;
19
- private final List <String > subcommands ;
24
+ private final Map <String , CommandInfo > subcommands ;
20
25
26
+ /**
27
+ * THIS IGNORES 'subcommands' parameter.
28
+ * @param subcommands WILL BE IGNORED
29
+ * @deprecated
30
+ */
31
+ @ Deprecated
21
32
public CommandInfo (long arity , List <String > flags , long firstKey , long lastKey , long step ,
22
33
List <String > aclCategories , List <String > tips , List <String > subcommands ) {
34
+ this ((String ) null , arity , flags , firstKey , lastKey , step , aclCategories , tips , (Map ) null );
35
+ }
36
+
37
+ private CommandInfo (String name , long arity , List <String > flags , long firstKey , long lastKey , long step ,
38
+ List <String > aclCategories , List <String > tips , Map <String , CommandInfo > subcommands ) {
39
+ this .name = name ;
23
40
this .arity = arity ;
24
41
this .flags = flags ;
25
42
this .firstKey = firstKey ;
@@ -30,6 +47,13 @@ public CommandInfo(long arity, List<String> flags, long firstKey, long lastKey,
30
47
this .subcommands = subcommands ;
31
48
}
32
49
50
+ /**
51
+ * Command name
52
+ */
53
+ public String getName () {
54
+ return name ;
55
+ }
56
+
33
57
/**
34
58
* Arity is the number of arguments a command expects. It follows a simple pattern:
35
59
* A positive integer means a fixed number of arguments.
@@ -90,15 +114,23 @@ public List<String> getTips() {
90
114
/**
91
115
* All the command's subcommands, if any
92
116
*/
93
- public List <String > getSubcommands () {
117
+ public Map <String , CommandInfo > getSubcommands () {
94
118
return subcommands ;
95
119
}
96
120
97
121
public static final Builder <CommandInfo > COMMAND_INFO_BUILDER = new Builder <CommandInfo >() {
98
122
@ Override
99
123
public CommandInfo build (Object data ) {
124
+ if (data == null ) {
125
+ return null ;
126
+ }
127
+
100
128
List <Object > commandData = (List <Object >) data ;
129
+ if (commandData .isEmpty ()) {
130
+ return null ;
131
+ }
101
132
133
+ String name = STRING .build (commandData .get (0 ));
102
134
long arity = LONG .build (commandData .get (1 ));
103
135
List <String > flags = STRING_LIST .build (commandData .get (2 ));
104
136
long firstKey = LONG .build (commandData .get (3 ));
@@ -110,9 +142,31 @@ public CommandInfo build(Object data) {
110
142
111
143
// (as of Redis 7.0)
112
144
List <String > tips = commandData .size ()>=8 ?STRING_LIST .build (commandData .get (7 )):Collections .emptyList ();
113
- List <String > subcommands = commandData .size ()>=10 ?STRING_LIST .build (commandData .get (9 )): Collections .emptyList ();
145
+ Map <String , CommandInfo > subcommands = commandData .size ()>=10 ?COMMAND_INFO_RESPONSE .build (commandData .get (9 )): Collections .emptyMap ();
146
+
147
+ return new CommandInfo (name , arity , flags , firstKey , lastKey , step , aclCategories , tips , subcommands );
148
+ }
149
+ };
114
150
115
- return new CommandInfo (arity , flags , firstKey , lastKey , step , aclCategories , tips , subcommands );
151
+ public static final Builder <Map <String , CommandInfo >> COMMAND_INFO_RESPONSE = new Builder <Map <String , CommandInfo >>() {
152
+ @ Override
153
+ public Map <String , CommandInfo > build (Object data ) {
154
+ if (data == null ) {
155
+ return null ;
156
+ }
157
+
158
+ List <Object > rawList = (List <Object >) data ;
159
+ Map <String , CommandInfo > map = new HashMap <>(rawList .size ());
160
+
161
+ for (Object rawCommandInfo : rawList ) {
162
+ CommandInfo info = CommandInfo .COMMAND_INFO_BUILDER .build (rawCommandInfo );
163
+ if (info != null ) {
164
+ map .put (info .getName (), info );
165
+ }
166
+ }
167
+
168
+ return map ;
116
169
}
117
170
};
171
+
118
172
}
0 commit comments