-module(examples@search).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/search.gleam").
-export([main/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Run a full-text search and decode the dynamic payload.\n"
"\n"
" `feed.search` returns the raw `Dynamic` value because the schema is open\n"
" and varies per query — this example shows the idiomatic way to decode it.\n"
"\n"
" Run with:\n"
" gleam run -m examples/search\n"
).
-file("src/examples/search.gleam", 17).
-spec main() -> nil.
main() ->
Client = rocksky:new(),
Payload@1 = case begin
_pipe = rocksky@feed:search(<<"radiohead"/utf8>>),
rocksky:send(_pipe, Client)
end of
{ok, Payload} -> Payload;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"examples/search"/utf8>>,
function => <<"main"/utf8>>,
line => 20,
value => _assert_fail,
start => 471,
'end' => 555,
pattern_start => 482,
pattern_end => 493})
end,
Result_decoder = begin
gleam@dynamic@decode:optional_field(
<<"artists"/utf8>>,
[],
gleam@dynamic@decode:list(rocksky@decoders:artist()),
fun(Artists) ->
gleam@dynamic@decode:optional_field(
<<"songs"/utf8>>,
[],
gleam@dynamic@decode:list(rocksky@decoders:song()),
fun(Songs) ->
gleam@dynamic@decode:success({Artists, Songs})
end
)
end
)
end,
{Artists@2, Songs@2} = case gleam@dynamic@decode:run(
Payload@1,
Result_decoder
) of
{ok, {Artists@1, Songs@1}} -> {Artists@1, Songs@1};
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"examples/search"/utf8>>,
function => <<"main"/utf8>>,
line => 38,
value => _assert_fail@1,
start => 847,
'end' => 917,
pattern_start => 858,
pattern_end => 879})
end,
gleam_stdlib:println(<<"artists:"/utf8>>),
gleam@list:each(
Artists@2,
fun(Artist) ->
gleam_stdlib:println(
<<" - "/utf8,
(gleam@option:unwrap(
erlang:element(4, Artist),
<<"?"/utf8>>
))/binary>>
)
end
),
gleam_stdlib:println(<<"songs:"/utf8>>),
gleam@list:each(
Songs@2,
fun(Song) ->
gleam_stdlib:println(
<<<<<<" - "/utf8,
(gleam@option:unwrap(
erlang:element(3, Song),
<<"?"/utf8>>
))/binary>>/binary,
" — "/utf8>>/binary,
(gleam@option:unwrap(erlang:element(4, Song), <<"?"/utf8>>))/binary>>
)
end
).