Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sumo_rest_doc:from_ctx/1 callback #66

Closed
6 tasks done
elbrujohalcon opened this issue Feb 22, 2017 · 0 comments · Fixed by #67
Closed
6 tasks done

Add sumo_rest_doc:from_ctx/1 callback #66

elbrujohalcon opened this issue Feb 22, 2017 · 0 comments · Fixed by #67
Assignees

Comments

@elbrujohalcon
Copy link
Member

elbrujohalcon commented Feb 22, 2017

The idea is to give model implementors access to what currently is on sumo_rest handlers state.
The tasks are as follows:

  • Abstract sr_entities_handler and sr_single_entity_handler states to a new ADT called sr_state:state(). Defined as follows:
-module(sr_state).

-opaque state() ::
  #{opts := sumo_rest:options()
   , id => binary()
   , entity => sumo:user_doc()
   , module := module()
   , user_opts := map()
   }.
-export_type([state/0]).

%% constructor, getters and setters
-export([new/2, id/1, id/2, entity/1, entity/2, module/1, opts/1]).
%% functions to work with user opts
-export([set/3, retrieve/3, remove/2]).
-spec set(Key::term(), Value::term(), state()) -> state().
-spec retrieve(Key::term(), state(), Default::T) -> T.
-spec remove(Key::term(), state()) -> term().
  • Create a new ADT for request data, defined as follows.
-module(sr_request).
-opaque req() ::
  #{body => sr_json:json()
   , headers := [{binary(), binary()}]
   , path := binary()
   , bindings := #{atom() => binary()}
   }.
-export_type [req/0].

-export([new/1]).

-spec new(cowboy_req:req()) -> req().
  • Add a new type and callback to sumo_rest_doc, as follows:
-type context() :: #{req := sr_request:req(), state := sr_state:state()}.
-callback from_ctx(context()) -> sumo:user_doc().
-optional_callbacks([from_ctx/1]).
  • Turn from_json/1 into an optional callback

  • Change the handlers so that, whenever Module:from_json/1 is called now, before doing that, if Module:from_ctx/1 is defined, they use that one.

  • Update the docs and examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants