-
Notifications
You must be signed in to change notification settings - Fork 2
/
set.gleam
137 lines (130 loc) · 3.4 KB
/
set.gleam
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//// All timeouts are in milliseconds
import gleam/set
import gleam/int
import gleam/list
import gleam/result
import radish/resp
import radish/error
import radish/command/set as command
import radish/utils.{execute}
/// see [here](https://redis.io/commands/sadd)!
pub fn add(client, key: String, values: List(String), timeout: Int) {
command.add(key, values)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Integer(n)] -> Ok(n)
_ -> Error(error.RESPError)
}
})
|> result.flatten
}
/// see [here](https://redis.io/commands/scard)!
pub fn card(client, key: String, timeout: Int) {
command.card(key)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Integer(n)] -> Ok(n)
_ -> Error(error.RESPError)
}
})
|> result.flatten
}
/// see [here](https://redis.io/commands/scard)!
pub fn is_member(client, key: String, value: String, timeout: Int) {
command.is_member(key, value)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Integer(n)] -> Ok(n)
_ -> Error(error.RESPError)
}
})
|> result.flatten
}
/// see [here](https://redis.io/commands/smembers)!
pub fn members(client, key: String, timeout: Int) {
command.members(key)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Set(set)] -> {
use list <- result.then(
set
|> set.to_list
|> list.try_map(fn(item) {
case item {
resp.BulkString(str) -> Ok(str)
_ -> Error(error.RESPError)
}
}),
)
Ok(set.from_list(list))
}
_ -> Error(error.RESPError)
}
})
|> result.flatten
}
/// see [here](https://redis.io/commands/sscan)!
pub fn scan(client, key: String, cursor: Int, count: Int, timeout: Int) {
command.scan(key, cursor, count)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Array([resp.BulkString(new_cursor_str), resp.Array(keys)])] ->
case int.parse(new_cursor_str) {
Ok(new_cursor) -> {
use array <- result.then(list.try_map(
keys,
fn(item) {
case item {
resp.BulkString(value) -> Ok(value)
_ -> Error(error.RESPError)
}
},
))
Ok(#(array, new_cursor))
}
Error(Nil) -> Error(error.RESPError)
}
_ -> Error(error.RESPError)
}
})
|> result.flatten
}
/// see [here](https://redis.io/commands/sscan)!
pub fn scan_pattern(
client,
key: String,
cursor: Int,
pattern: String,
count: Int,
timeout: Int,
) {
command.scan_pattern(key, cursor, pattern, count)
|> execute(client, _, timeout)
|> result.map(fn(value) {
case value {
[resp.Array([resp.BulkString(new_cursor_str), resp.Array(keys)])] ->
case int.parse(new_cursor_str) {
Ok(new_cursor) -> {
use array <- result.then(list.try_map(
keys,
fn(item) {
case item {
resp.BulkString(value) -> Ok(value)
_ -> Error(error.RESPError)
}
},
))
Ok(#(array, new_cursor))
}
Error(Nil) -> Error(error.RESPError)
}
_ -> Error(error.RESPError)
}
})
|> result.flatten
}