Skip to main content

src/gdo@row.erl

-module(gdo@row).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gdo/row.gleam").
-export([new/1, columns/1, column_count/1, get/2, get_at/2]).
-export_type([row/0]).

-type row() :: {row, list({binary(), gdo@value:db_value()})}.

-file("src/gdo/row.gleam", 10).
-spec new(list({binary(), gdo@value:db_value()})) -> row().
new(Columns) ->
    {row, Columns}.

-file("src/gdo/row.gleam", 14).
-spec columns(row()) -> list({binary(), gdo@value:db_value()}).
columns(Row) ->
    {row, Columns} = Row,
    Columns.

-file("src/gdo/row.gleam", 19).
-spec column_count(row()) -> integer().
column_count(Row) ->
    _pipe = Row,
    _pipe@1 = columns(_pipe),
    erlang:length(_pipe@1).

-file("src/gdo/row.gleam", 23).
-spec get(row(), binary()) -> {ok, gdo@value:db_value()} |
    {error, gdo@error:error()}.
get(Row, Column_name) ->
    case begin
        _pipe = Row,
        _pipe@1 = columns(_pipe),
        _pipe@2 = maps:from_list(_pipe@1),
        gleam_stdlib:map_get(_pipe@2, Column_name)
    end of
        {ok, Value} ->
            {ok, Value};

        {error, _} ->
            {error,
                {decode_error,
                    <<"Column not found: "/utf8, Column_name/binary>>}}
    end.

-file("src/gdo/row.gleam", 37).
-spec nth(list({binary(), gdo@value:db_value()}), integer()) -> {ok,
        gdo@value:db_value()} |
    {error, gdo@error:error()}.
nth(Columns, Index) ->
    case {Columns, Index} of
        {[], _} ->
            {error, {decode_error, <<"Column index out of bounds."/utf8>>}};

        {[{_, Value} | _], 0} ->
            {ok, Value};

        {[_ | Rest], _} ->
            nth(Rest, Index - 1)
    end.

-file("src/gdo/row.gleam", 30).
-spec get_at(row(), integer()) -> {ok, gdo@value:db_value()} |
    {error, gdo@error:error()}.
get_at(Row, Index) ->
    case Index < 0 of
        true ->
            {error, {decode_error, <<"Column index cannot be negative."/utf8>>}};

        false ->
            nth(columns(Row), Index)
    end.