-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from inaka/jfacorro.12.egithub_json.behavior
[#12] egithub_json behavior
- Loading branch information
Showing
7 changed files
with
145 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-module(egithub_jiffy). | ||
|
||
-behaviour(egithub_json). | ||
|
||
-export([ | ||
encode/1, | ||
decode/1 | ||
]). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% egithub_json callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec encode(map()) -> binary(). | ||
encode(Map) -> | ||
jiffy:encode(Map). | ||
|
||
-spec decode(binary()) -> map(). | ||
decode(Json) -> | ||
jiffy:decode(Json, [return_maps]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
%% @doc This module defines a behavior that needs to be implemented using a | ||
%% specific JSON encoding/decoding library. | ||
%% The module implementing the behavior that will be used is determined | ||
%% by this application's `json` configuration value. | ||
-module(egithub_json). | ||
|
||
%% API | ||
-export([ | ||
encode/1, | ||
decode/1 | ||
]). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Behavior definition | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-callback encode(map()) -> binary(). | ||
-callback decode(binary()) -> map(). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% API functions | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec encode(map()) -> binary(). | ||
encode(Map) -> | ||
Module = json_module(), | ||
Module:encode(Map). | ||
|
||
-spec decode(map()) -> binary(). | ||
decode(Map) -> | ||
Module = json_module(), | ||
Module:decode(Map). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Internal functions | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec json_module() -> atom(). | ||
json_module() -> | ||
application:get_env(json, egithub, egithub_jiffy). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
-module(egithub_json_SUITE). | ||
|
||
-export([ | ||
all/0, | ||
init_per_suite/1, | ||
end_per_suite/1 | ||
]). | ||
|
||
-export([ | ||
encode_callback/1, | ||
decode_callback/1 | ||
]). | ||
|
||
-define(EXCLUDED_FUNS, | ||
[ | ||
module_info, | ||
all, | ||
test, | ||
init_per_suite, | ||
end_per_suite | ||
]). | ||
|
||
-type config() :: [{atom(), term()}]. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Common test | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec all() -> [atom()]. | ||
all() -> | ||
Exports = ?MODULE:module_info(exports), | ||
[F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)]. | ||
|
||
-spec init_per_suite(config()) -> config(). | ||
init_per_suite(Config) -> | ||
Config. | ||
|
||
-spec end_per_suite(config()) -> config(). | ||
end_per_suite(_Config) -> | ||
ok. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Test cases | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
encode_callback(_Config) -> | ||
application:set_env(json, egithub, egithub_json_example), | ||
1 = egithub_json:encode(#{}). | ||
|
||
decode_callback(_Config) -> | ||
application:set_env(json, egithub, egithub_json_example), | ||
2 = egithub_json:decode(#{}). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-module(egithub_json_example). | ||
|
||
-behaviour(egithub_json). | ||
|
||
-export([ | ||
encode/1, | ||
decode/1 | ||
]). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% egithub_json callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec encode(map()) -> binary(). | ||
encode(_Map) -> | ||
1. | ||
|
||
-spec decode(binary()) -> map(). | ||
decode(_Json) -> | ||
2. |