<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2016 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

-module(lasp_kv_resource).
-author("<NAME> <<EMAIL>>").

-export([init/1,
         allowed_methods/2,
         process_post/2,
         content_types_provided/2,
         resource_exists/2,
         to_msgpack/2]).

-include("lasp.hrl").
-include_lib("webmachine/include/webmachine.hrl").

-record(ctx, {id, type, object}).

-spec init(list()) -> {ok, term()}.
init(_) ->
    {ok, #ctx{}}.

allowed_methods(ReqData, Ctx) ->
    {['GET', 'POST', 'HEAD'], ReqData, Ctx}.

content_types_provided(ReqData, Ctx) ->
    {[{"application/msgpack", to_msgpack}], ReqData, Ctx}.

process_post(ReqData, Ctx) ->
    Body = wrq:req_body(ReqData),

    Id = wrq:path_info(id, ReqData),
    Type = wrq:path_info(type, ReqData),

    case {Id, Type} of
        {undefined, _} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, undefined} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, _} ->
            Decoded = Type:decode(msgpack:unpack(Body)),

            case lasp:bind({binary(Id), atomize(Type)}, Decoded) of
                {ok, Object} ->
                    {true, ReqData, Ctx#ctx{id=Id, type=Type, object=Object}};
                Error ->
                    lager:info("Received error response: ~p", [Error]),
                    {false, ReqData, Ctx#ctx{id=Id, type=Type}}
            end
    end.

resource_exists(ReqData, Ctx) ->
    Id = wrq:path_info(id, ReqData),
    Type = wrq:path_info(type, ReqData),

    case {Id, Type} of
        {undefined, _} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, undefined} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, _} ->
            case lasp:read({binary(Id), atomize(Type)}, undefined) of
                {ok, Object} ->
                    {true, ReqData, Ctx#ctx{id=Id, type=Type, object=Object}};
                Error ->
                    lager:info("Received error response: ~p", [Error]),
                    {false, ReqData, Ctx#ctx{id=Id, type=Type}}
            end
   end.

to_msgpack(ReqData, #ctx{type=Type, object=Object}=Ctx) ->
    Encoded = msgpack:pack(Type:encode(Object)),
    {Encoded, ReqData, Ctx}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

%% @private
atomize(Type) when is_list(Type) ->
    list_to_existing_atom(Type).

%% @private
binary(Id) when is_list(Id) ->
    term_to_binary(Id).
<commit_msg>Assume different encode/decode API.<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2016 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

-module(lasp_kv_resource).
-author("<NAME> <<EMAIL>>").

-export([init/1,
         allowed_methods/2,
         process_post/2,
         content_types_provided/2,
         resource_exists/2,
         to_msgpack/2]).

-include("lasp.hrl").
-include_lib("webmachine/include/webmachine.hrl").

-record(ctx, {id, type, object}).

-spec init(list()) -> {ok, term()}.
init(_) ->
    {ok, #ctx{}}.

allowed_methods(ReqData, Ctx) ->
    {['GET', 'POST', 'HEAD'], ReqData, Ctx}.

content_types_provided(ReqData, Ctx) ->
    {[{"application/msgpack", to_msgpack}], ReqData, Ctx}.

process_post(ReqData, Ctx) ->
    Body = wrq:req_body(ReqData),

    Id = wrq:path_info(id, ReqData),
    Type = wrq:path_info(type, ReqData),

    case {Id, Type} of
        {undefined, _} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, undefined} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, _} ->
            Decoded = Type:decode(msgpack, Body),

            case lasp:bind({binary(Id), atomize(Type)}, Decoded) of
                {ok, Object} ->
                    {true, ReqData, Ctx#ctx{id=Id, type=Type, object=Object}};
                Error ->
                    lager:info("Received error response: ~p", [Error]),
                    {false, ReqData, Ctx#ctx{id=Id, type=Type}}
            end
    end.

resource_exists(ReqData, Ctx) ->
    Id = wrq:path_info(id, ReqData),
    Type = wrq:path_info(type, ReqData),

    case {Id, Type} of
        {undefined, _} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, undefined} ->
            {false, ReqData, Ctx#ctx{id=Id, type=Type}};
        {_, _} ->
            case lasp:read({binary(Id), atomize(Type)}, undefined) of
                {ok, Object} ->
                    {true, ReqData, Ctx#ctx{id=Id, type=Type, object=Object}};
                Error ->
                    lager:info("Received error response: ~p", [Error]),
                    {false, ReqData, Ctx#ctx{id=Id, type=Type}}
            end
   end.

to_msgpack(ReqData, #ctx{type=Type, object=Object}=Ctx) ->
    Encoded = Type:encode(msgpack, Object),
    {Encoded, ReqData, Ctx}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

%% @private
atomize(Type) when is_list(Type) ->
    list_to_existing_atom(Type).

%% @private
binary(Id) when is_list(Id) ->
    term_to_binary(Id).

{'fraction_non_alphanumeric': 0.2269009391093608, 'fraction_numerical': 0.004241139048773099, 'mean_word_length': 2.3861538461538463, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3677959', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2022, 'n_words': 489}