Y6JGJ7676WTHGES5N3VV7L5D4ZJWPTWYUFGZXQOKUMYYUOKS2EMAC T3VWIP2EHZMGXM6UQMK77WB225WRNTAW7IWXZ5ZHUZ6PE3I7AATQC QT6LHFTXOG7V3TNZN6SNB7GXIVM454C3GSCAQ74M7P6GQL6B4Q7AC 755UGKECZ3PFYEA2TFFOUZTF27CRQTBZWO7UYFU6WQDJMZDBVPRAC MMWEGAYYQ4CX3U6JJH4MNEZHTAYK6WQBGXIVQ7DL7MVSPGNS7V7AC OJ6KWAG7XUCYNQ6T3FRJK2QOP7DBO26PZEWDVH3Q2MJKIBMNWRPQC CZG24QKIB45EL7PK4UA6XVG3VEVKI7OAQXN4YMFUKKI2MXLHRANQC YA3ELS7AQZ2XCZZVNMZOM3TSVCWU7HSBVMDXFCIOFTXAYFZSRL2AC -module(examples_from_json).-export([post_request/0, cancel_request/0, channel_time_range_request/0,channel_state_request/0, channel_list_request/0,hash_response/0, post_response/0, channel_list_response/0,post_text/0, post_delete/0, post_info/0, post_topic/0, post_join/0, post_leave/0]).post_request() ->{ok, D} = unpack_data(2),%% assert the json is still formatted as expected<<"post request">> = proplists:get_value(name, D),D.cancel_request() ->{ok, D} = unpack_data(3),<<"cancel request">> = proplists:get_value(name, D),D.channel_time_range_request() ->{ok, D} = unpack_data(4),<<"channel time range request">> = proplists:get_value(name, D),D.channel_state_request() ->{ok, D} = unpack_data(5),<<"channel state request">> = proplists:get_value(name, D),D.channel_list_request() ->{ok, D} = unpack_data(6),<<"channel list request">> = proplists:get_value(name, D),D.%% TODO: moderation_state_request = 7hash_response() ->{ok, D} = unpack_data(8),<<"hash response">> = proplists:get_value(name, D),D.post_response() ->{ok, D} = unpack_data(9),<<"post response">> = proplists:get_value(name, D),D.channel_list_response() ->{ok, D} = unpack_data(10),<<"channel list response">> = proplists:get_value(name, D),D.post_text() ->{ok, D} = unpack_data(11),<<"post/text">> = proplists:get_value(name, D),D.post_delete() ->{ok, D} = unpack_data(12),<<"post/delete">> = proplists:get_value(name, D),D.post_info() ->{ok, D} = unpack_data(13),<<"post/info">> = proplists:get_value(name, D),D.post_topic() ->{ok, D} = unpack_data(14),<<"post/topic">> = proplists:get_value(name, D),D.post_join() ->{ok, D} = unpack_data(15),<<"post/join">> = proplists:get_value(name, D),D.post_leave() ->{ok, D} = unpack_data(16),<<"post/leave">> = proplists:get_value(name, D),D.%% TODO:%% "post/role"%% "post/moderation"%% "post/block"%% "post/unblock"% json helper% ===========%% Load JSON data into persistent_termload() ->case file:read_file("cable-examples.json") of{ok, Binary} ->tryJson = jsone:decode(Binary),persistent_term:put(json_data, Json),{ok, Json}catch_:Error -> {error, Error}end;{error, Reason} -> {error, Reason}end.%% Retrieve JSON data from persistent_termget_data(Index) ->{ok, J} = case persistent_term:get(json_data, undefined) ofundefined -> load();Json -> {ok, Json}end,{ok, lists:nth(Index, J)}.unpack_data(Index) ->{ok, J } = get_data(Index),#{ <<"binary">> := Bin, <<"obj">> := Obj, <<"name">> := Name } = J,{ok, [{name, Name},{binary, hex:hexstr_to_bin(binary_to_list(Bin))},{obj, Obj}]}.
-module(compare_examples).-export([compare/1, compare_all/0]).%% List of functions to comparefunctions_to_compare() ->[post_request, cancel_request, channel_time_range_request,channel_state_request, channel_list_request,hash_response, post_response, channel_list_response,post_text, post_delete, post_info, post_topic, post_join, post_leave].%% Compare all functions in the listcompare_all() ->Functions = functions_to_compare(),compare_functions(Functions).%% Compare functions from old and new modulescompare_functions([]) ->io:format("Comparison complete~n");compare_functions([Function | Rest]) ->compare(Function),compare_functions(Rest).%% Compare a specific functioncompare(Function) ->OldResult = apply(examples, Function, []),NewResult = apply(examples_from_json, Function, []),io:format("Comparing ~p/0~n", [Function]),io:format("Old result: ~p~n", [OldResult]),io:format("New result: ~p~n", [NewResult]),case OldResult ofNewResult -> io:format("Results match~n");_ -> io:format("Results do not match!~n")end.
% See cable.js/complete-examples.js% TODO: deserialize that output directly and convert these funcs here to decode_entry(Index)-export([post_request/0, cancel_request/0, channel_time_range_request/0,channel_state_request/0, channel_list_request/0,hash_response/0, post_response/0, channel_list_response/0,post_text/0, post_delete/0, post_info/0, post_topic/0, post_join/0, post_leave/0
-export([post_request/0, cancel_request/0, channel_time_range_request/0,channel_state_request/0, channel_list_request/0,hash_response/0, post_response/0, channel_list_response/0,post_text/0, post_delete/0, post_info/0, post_topic/0, post_join/0, post_leave/0
D = <<"{\"name\":\"post request\",\"type\":\"request\",\"id\":2,\"binary\":\"6b020000000095050429010320265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\",\"obj\":{\"msgLen\":107,\"msgType\":2,\"reqid\":\"95050429\",\"ttl\":1,\"hashes\":[\"20265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d\",\"10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371\",\"af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\"]}}">>,decode_example_json(D).
{ok, D} = unpack_data(2),%% assert the json is still formatted as expected<<"post request">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"cancel request\",\"type\":\"request\",\"id\":3,\"binary\":\"0e0300000000950504290158b041b1\",\"obj\":{\"msgLen\":14,\"msgType\":3,\"reqid\":\"95050429\",\"ttl\":1,\"cancelid\":\"58b041b1\"}}">>,decode_example_json(D).
{ok, D} = unpack_data(3),<<"cancel request">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"channel time range request\",\"type\":\"request\",\"id\":4,\"binary\":\"15040000000095050429010764656661756c74006414\",\"obj\":{\"msgLen\":21,\"msgType\":4,\"reqid\":\"95050429\",\"ttl\":1,\"channel\":\"default\",\"timeStart\":0,\"timeEnd\":100,\"limit\":20}}">>,decode_example_json(D).
{ok, D} = unpack_data(4),<<"channel time range request">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"channel state request\",\"type\":\"request\",\"id\":5,\"binary\":\"13050000000095050429010764656661756c7400\",\"obj\":{\"msgLen\":19,\"msgType\":5,\"reqid\":\"95050429\",\"ttl\":1,\"channel\":\"default\",\"future\":0}}">>,decode_example_json(D).
{ok, D} = unpack_data(5),<<"channel state request">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"channel list request\",\"type\":\"request\",\"id\":6,\"binary\":\"0c060000000095050429010014\",\"obj\":{\"msgLen\":12,\"msgType\":6,\"reqid\":\"95050429\",\"ttl\":1,\"offset\":0,\"limit\":20}}">>,decode_example_json(D).
{ok, D} = unpack_data(6),<<"channel list request">> = proplists:get_value(name, D),D.%% TODO: moderation_state_request = 7
D = <<"{\"name\":\"hash response\",\"type\":\"response\",\"id\":0,\"binary\":\"6a0000000000950504290320265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\",\"obj\":{\"msgLen\":106,\"msgType\":0,\"reqid\":\"95050429\",\"hashes\":[\"20265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d\",\"10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371\",\"af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\"]}}">>,decode_example_json(D).
{ok, D} = unpack_data(8),<<"hash response">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post response\",\"type\":\"response\",\"id\":1,\"binary\":\"97010100000000950504298b0125b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0abb083ecdca569f064564942ddf1944fbf550dc27ea36a7074be798d753cb029703de77b1a9532b6ca2ec5706e297dce073d6e508eeb425c32df8431e4677805015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b305500764656661756c7400\",\"obj\":{\"msgLen\":151,\"msgType\":1,\"reqid\":\"95050429\",\"posts\":[\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0abb083ecdca569f064564942ddf1944fbf550dc27ea36a7074be798d753cb029703de77b1a9532b6ca2ec5706e297dce073d6e508eeb425c32df8431e4677805015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b305500764656661756c74\"]}}">>,decode_example_json(D).
{ok, D} = unpack_data(9),<<"post response">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"channel list response\",\"type\":\"response\",\"id\":7,\"binary\":\"230700000000950504290764656661756c74036465760c696e74726f64756374696f6e00\",\"obj\":{\"msgLen\":35,\"msgType\":7,\"reqid\":\"95050429\",\"channels\":[\"default\",\"dev\",\"introduction\"]}}">>,decode_example_json(D).
{ok, D} = unpack_data(10),<<"channel list response">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/text\",\"type\":\"post\",\"id\":0,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d06725733046b35fa3a7e8dc0099a2b3dff10d3fd8b0f6da70d094352e3f5d27a8bc3f5586cf0bf71befc22536c3c50ec7b1d64398d43c3f4cde778e579e88af05015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b300500764656661756c740d68e282ac6c6c6f20776f726c64\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"6725733046b35fa3a7e8dc0099a2b3dff10d3fd8b0f6da70d094352e3f5d27a8bc3f5586cf0bf71befc22536c3c50ec7b1d64398d43c3f4cde778e579e88af05\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":0,\"channel\":\"default\",\"timestamp\":80,\"text\":\"h€llo world\"}}">>,decode_example_json(D).
{ok, D} = unpack_data(11),<<"post/text">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/delete\",\"type\":\"post\",\"id\":1,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0e8fc6c809f3086627879520abe6f76a4810a8bef77a668f41046c48dc98c13ed55aa54ca1e6913076bd7791c6c97aa807850bc6be7415fa5d251b9b26febd101015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b301500320265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"e8fc6c809f3086627879520abe6f76a4810a8bef77a668f41046c48dc98c13ed55aa54ca1e6913076bd7791c6c97aa807850bc6be7415fa5d251b9b26febd101\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":1,\"timestamp\":80,\"hashes\":[\"20265674e8aac2dfddd78f86fe5a3dd68d976ca3f5ba23645ec7381480921d0d\",\"10705340e5528f2ef03a6797b72b1bb9f37f9009ad408247387c4bcc4d2a3371\",\"af700793dd51d4cb3c18a6df46f88bfe1665fba9b277487ddecd1e031441d69d\"]}}">>,decode_example_json(D).
{ok, D} = unpack_data(12),<<"post/delete">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/info\",\"type\":\"post\",\"id\":2,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d04ccb1c0063ef09a200e031ee89d874bcc99f3e6fd8fd667f5e28f4dbcf4b7de6bb1ce37d5f01cc055a7b70cef175d30feeb34531db98c91fa8b3fa4d7c5fd307015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b30250046e616d65066361626c657200\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"4ccb1c0063ef09a200e031ee89d874bcc99f3e6fd8fd667f5e28f4dbcf4b7de6bb1ce37d5f01cc055a7b70cef175d30feeb34531db98c91fa8b3fa4d7c5fd307\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":2,\"timestamp\":80,\"key\":\"name\",\"value\":\"cabler\"}}">>,decode_example_json(D).
{ok, D} = unpack_data(13),<<"post/info">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/topic\",\"type\":\"post\",\"id\":3,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0bf7578e781caee4ca708281645b291a2100c4f2138f0e0ac98bc2b4a414b4ba8dca08285751114b05f131421a1745b648c43b17b05392593237dfacc8dff5208015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b303500764656661756c743b696e74726f6475636520796f757273656c6620746f2074686520667269656e646c792063726f7764206f66206c696b656d696e64656420666f6c78\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"bf7578e781caee4ca708281645b291a2100c4f2138f0e0ac98bc2b4a414b4ba8dca08285751114b05f131421a1745b648c43b17b05392593237dfacc8dff5208\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":3,\"channel\":\"default\",\"timestamp\":80,\"topic\":\"introduce yourself to the friendly crowd of likeminded folx\"}}">>,decode_example_json(D).
{ok, D} = unpack_data(14),<<"post/topic">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/join\",\"type\":\"post\",\"id\":4,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d064425f10fa34c1e14b6101491772d3c5f15f720a952dd56c27d5ad52f61f695130ce286de73e332612b36242339b61c9e12397f5dcc94c79055c7e1cb1dbfb08015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b304500764656661756c74\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"64425f10fa34c1e14b6101491772d3c5f15f720a952dd56c27d5ad52f61f695130ce286de73e332612b36242339b61c9e12397f5dcc94c79055c7e1cb1dbfb08\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":4,\"channel\":\"default\",\"timestamp\":80}}">>,decode_example_json(D).
{ok, D} = unpack_data(15),<<"post/join">> = proplists:get_value(name, D),D.
D = <<"{\"name\":\"post/leave\",\"type\":\"post\",\"id\":5,\"binary\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0abb083ecdca569f064564942ddf1944fbf550dc27ea36a7074be798d753cb029703de77b1a9532b6ca2ec5706e297dce073d6e508eeb425c32df8431e4677805015049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b305500764656661756c74\",\"obj\":{\"publicKey\":\"25b272a71555322d40efe449a7f99af8fd364b92d350f1664481b2da340a02d0\",\"signature\":\"abb083ecdca569f064564942ddf1944fbf550dc27ea36a7074be798d753cb029703de77b1a9532b6ca2ec5706e297dce073d6e508eeb425c32df8431e4677805\",\"links\":[\"5049d089a650aa896cb25ec35258653be4df196b4a5e5b6db7ed024aaa89e1b3\"],\"postType\":5,\"channel\":\"default\",\"timestamp\":80}}">>,decode_example_json(D).
{ok, D} = unpack_data(16),<<"post/leave">> = proplists:get_value(name, D),D.%% TODO:%% "post/role"%% "post/moderation"%% "post/block"%% "post/unblock"% json helper% ===========%% Load JSON data into persistent_termload() ->case file:read_file("cable-examples.json") of{ok, Binary} ->tryJson = jsone:decode(Binary),persistent_term:put(json_data, Json),{ok, Json}catch_:Error -> {error, Error}end;{error, Reason} -> {error, Reason}end.%% Retrieve JSON data from persistent_termget_data(Index) ->{ok, J} = case persistent_term:get(json_data, undefined) ofundefined -> load();Json -> {ok, Json}end,{ok, lists:nth(Index, J)}.