-module(radish@command). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]). -export([hello/1, keys/1, exists/1, get/1, mget/1, append/2, set/3, mset/1, del/1, incr/1, incr_by/2, incr_by_float/2, decr/1, decr_by/2]). -export_type([set_option/0]). -type set_option() :: nx | xx | get | keepttl | {ex, integer()} | {px, integer()} | {exat, integer()} | {pxat, integer()}. -spec hello(integer()) -> bitstring(). hello(Protocol) -> _pipe = [<<"HELLO"/utf8>>, gleam@int:to_string(Protocol)], radish@utils:prepare(_pipe). -spec keys(binary()) -> bitstring(). keys(Pattern) -> _pipe = [<<"KEYS"/utf8>>, Pattern], radish@utils:prepare(_pipe). -spec exists(list(binary())) -> bitstring(). exists(Keys) -> _pipe = [<<"EXISTS"/utf8>>], _pipe@1 = gleam@list:append( _pipe, gleam@list:map(Keys, fun(Key) -> Key end) ), radish@utils:prepare(_pipe@1). -spec get(binary()) -> bitstring(). get(Key) -> _pipe = [<<"GET"/utf8>>, Key], radish@utils:prepare(_pipe). -spec mget(list(binary())) -> bitstring(). mget(Keys) -> _pipe = [<<"MGET"/utf8>>], _pipe@1 = gleam@list:append( _pipe, gleam@list:map(Keys, fun(Key) -> Key end) ), radish@utils:prepare(_pipe@1). -spec append(binary(), binary()) -> bitstring(). append(Key, Value) -> _pipe = [<<"APPEND"/utf8>>, Key, Value], radish@utils:prepare(_pipe). -spec set(binary(), binary(), list(set_option())) -> bitstring(). set(Key, Value, Options) -> _pipe = gleam@list:fold( Options, [<<"SET"/utf8>>, Key, Value], fun(Cmd, Option) -> case Option of nx -> gleam@list:append(Cmd, [<<"NX"/utf8>>]); xx -> gleam@list:append(Cmd, [<<"XX"/utf8>>]); get -> gleam@list:append(Cmd, [<<"GET"/utf8>>]); keepttl -> gleam@list:append(Cmd, [<<"KEEPTTL"/utf8>>]); {ex, Value@1} -> gleam@list:append( Cmd, [<<"EX"/utf8>>, gleam@int:to_string(Value@1)] ); {px, Value@2} -> gleam@list:append( Cmd, [<<"PX"/utf8>>, gleam@int:to_string(Value@2)] ); {exat, Value@3} -> gleam@list:append( Cmd, [<<"EXAT"/utf8>>, gleam@int:to_string(Value@3)] ); {pxat, Value@4} -> gleam@list:append( Cmd, [<<"PXAT"/utf8>>, gleam@int:to_string(Value@4)] ) end end ), radish@utils:prepare(_pipe). -spec mset(list({binary(), binary()})) -> bitstring(). mset(Kv_list) -> _pipe = Kv_list, _pipe@1 = gleam@list:map( _pipe, fun(Kv) -> [erlang:element(1, Kv), erlang:element(2, Kv)] end ), _pipe@2 = gleam@list:flatten(_pipe@1), _pipe@3 = gleam@list:append([<<"MSET"/utf8>>], _pipe@2), radish@utils:prepare(_pipe@3). -spec del(list(binary())) -> bitstring(). del(Keys) -> _pipe = [<<"DEL"/utf8>>], _pipe@1 = gleam@list:append( _pipe, gleam@list:map(Keys, fun(Key) -> Key end) ), radish@utils:prepare(_pipe@1). -spec incr(binary()) -> bitstring(). incr(Key) -> _pipe = [<<"INCR"/utf8>>, Key], radish@utils:prepare(_pipe). -spec incr_by(binary(), integer()) -> bitstring(). incr_by(Key, Value) -> _pipe = [<<"INCRBY"/utf8>>, Key, gleam@int:to_string(Value)], radish@utils:prepare(_pipe). -spec incr_by_float(binary(), float()) -> bitstring(). incr_by_float(Key, Value) -> _pipe = [<<"INCRBYFLOAT"/utf8>>, Key, gleam@float:to_string(Value)], radish@utils:prepare(_pipe). -spec decr(binary()) -> bitstring(). decr(Key) -> _pipe = [<<"DECR"/utf8>>, Key], radish@utils:prepare(_pipe). -spec decr_by(binary(), integer()) -> bitstring(). decr_by(Key, Value) -> _pipe = [<<"DECRBY"/utf8>>, Key, gleam@int:to_string(Value)], radish@utils:prepare(_pipe).