Skip to content

Commit

Permalink
Add convenience function json2:obj_fetch/3
Browse files Browse the repository at this point in the history
The json2:obj_fetch/2 either returns an object member's value or exits
with a struct_no_key error if the object member is not found. Add
json2:obj_fetch/3 that returns either the object member's value or a
default value if the object member is not found.

Add a new unit test for json2:obj_fetch/2,3 to the json2_SUITE.

Also in json2_SUITE, fix the test source directories for the
json2_decode_valid and json2_decode_invalid tests. Add asserts to
verify that one or more test files are processed by those tests.
  • Loading branch information
fnchooft authored and vinoski committed Apr 7, 2021
1 parent 2243eaf commit ca704a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/json2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

-module(json2).
-export([encode/1, decode_string/1, decode/2]).
-export([is_obj/1, obj_new/0, obj_fetch/2, obj_find/2, obj_is_key/2]).
-export([is_obj/1, obj_new/0, obj_fetch/2, obj_fetch/3, obj_find/2, obj_is_key/2]).
-export([obj_store/3, obj_from_list/1, obj_fold/3]).
-export([test/0]).
-author("Jim Larson <jalarson@amazon.com>, Robert Wai-Chi Chu <robchu@amazon.com>").
Expand Down Expand Up @@ -597,7 +597,7 @@ obj_new() ->
{struct, []}.

%% Fetch an object member's value, expecting it to be in the object.
%% Return value, runtime error if no member found with that name.
%% Return value, or runtime error if no member found with that name.

obj_fetch(Key, {struct, Props}) when is_list(Props) ->
case proplists:get_value(Key, Props) of
Expand All @@ -607,6 +607,19 @@ obj_fetch(Key, {struct, Props}) when is_list(Props) ->
Value
end.


%% Fetch an object member's value if the member exists. Return value
%% if found, or default if no member found with that name.

obj_fetch(Key, {struct, Props}, Default) when is_list(Props) ->
case proplists:get_value(Key, Props) of
undefined ->
Default;
Value ->
Value
end.


%% Fetch an object member's value, or indicate that there is no such member.
%% Return {ok, Value} or 'error'.

Expand Down
25 changes: 22 additions & 3 deletions testsuite/json2_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ all() ->
[
json2_decode_valid,
json2_decode_invalid,
json2_decode_invalid_utf8
json2_decode_invalid_utf8,
json2_obj_fetch
].

groups() ->
Expand Down Expand Up @@ -36,8 +37,9 @@ end_per_testcase(_Test, _Config) ->

%%====================================================================
json2_decode_valid(_Config) ->
Dir = filename:join(?srcdir, "json2_SUITE_data/valid"),
Dir = filename:join(?data_srcdir(?MODULE), "valid"),
Tests = filelib:wildcard(filename:join(Dir, "*.json")),
?assertMatch([_|_], Tests),
[begin
{ok, Bin} = file:read_file(T),
Str = unicode:characters_to_list(Bin),
Expand All @@ -46,8 +48,9 @@ json2_decode_valid(_Config) ->
ok.

json2_decode_invalid(_Config) ->
Dir = filename:join(?srcdir, "json2_SUITE_data/invalid"),
Dir = filename:join(?data_srcdir(?MODULE), "invalid"),
Tests = filelib:wildcard(filename:join(Dir, "*.json")),
?assertMatch([_|_], Tests),
[begin
{ok, Bin} = file:read_file(T),
Str = unicode:characters_to_list(Bin),
Expand All @@ -61,3 +64,19 @@ json2_decode_invalid_utf8(_Config) ->
?assertMatch({Str1, {error, _}}, {Str1, json2:decode_string(Str1)}),
?assertMatch({Str2, {error, _}}, {Str2, json2:decode_string(Str2)}),
ok.

json2_obj_fetch(_Config) ->
F = filename:join(?data_srcdir(?MODULE), "valid/simple-object.json"),
{ok, Bin} = file:read_file(F),
Str = unicode:characters_to_list(Bin),
{ok, Obj} = json2:decode_string(Str),
?assertEqual({array,[]}, json2:obj_fetch("a", Obj)),
NotFound = try
json2:obj_fetch("b", Obj)
catch
C:Exc ->
{C, Exc}
end,
?assertEqual({exit, {struct_no_key, "b"}}, NotFound),
?assertEqual(foo, json2:obj_fetch("b", Obj, foo)),
ok.

0 comments on commit ca704a9

Please sign in to comment.