-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.v
69 lines (54 loc) · 960 Bytes
/
command.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module vredis
import proto
struct BaseCmd {
mut:
args []proto.Any
key_pos i8
err string
}
pub fn (cmd BaseCmd) args() []proto.Any {
return cmd.args
}
pub fn (mut cmd BaseCmd) set_err(err string) {
cmd.err = err
}
pub fn (cmd BaseCmd) err() string {
return cmd.err
}
pub struct Cmd {
BaseCmd
mut:
val proto.Any
}
pub struct ResultCmd[T] {
cmd &Cmd
}
fn (r ResultCmd[T]) value() !T {
return proto.scan[T](r.cmd.val)
}
// TODO: proto.Any arguments
pub fn new_cmd(args ...proto.Any) &Cmd {
return &Cmd{
BaseCmd: BaseCmd{
args: args
}
}
}
pub fn (cmd Cmd) value() proto.Any {
return cmd.val
}
pub fn (mut cmd Cmd) set_val(val string) {
cmd.val = val
}
pub fn (cmd Cmd) result() ?proto.Any {
if cmd.err != '' {
return error(cmd.err)
}
return cmd.val
}
fn (mut cmd Cmd) read_reply(mut rd proto.Reader) ! {
cmd.val = rd.read_reply()!
}
fn write_cmd(mut wr proto.Writer, cmd Cmd) ! {
return wr.write_args(cmd.args())
}