Skip to content

Commit

Permalink
implemented agent id parsing with binary splits
Browse files Browse the repository at this point in the history
  • Loading branch information
khodzha committed Nov 23, 2021
1 parent 2a4c850 commit ec172c2
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/mqttgw.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1528,27 +1528,21 @@ parse_connection_params(ClientId, Properties) ->

-spec parse_agent_id(binary()) -> mqttgw_id:agent_id().
parse_agent_id(Val) ->
parse_agent_label(Val, <<>>).

-spec parse_agent_label(binary(), binary()) -> mqttgw_id:agent_id().
parse_agent_label(<<$., _/bits>>, <<>>) ->
error(missing_agent_label);
parse_agent_label(<<$., R/bits>>, Acc) ->
parse_account_label(R, Acc, <<>>);
parse_agent_label(<<C, R/bits>>, Acc) ->
parse_agent_label(R, <<Acc/bits, C>>);
parse_agent_label(<<>>, Acc) ->
error({bad_agent_label, [Acc]}).

-spec parse_account_label(binary(), binary(), binary()) -> mqttgw_id:agent_id().
parse_account_label(<<$., _/bits>>, _AgentLabel, <<>>) ->
error(missing_account_label);
parse_account_label(<<$., R/bits>>, AgentLabel, Acc) ->
parse_audience(R, AgentLabel, Acc);
parse_account_label(<<C, R/bits>>, AgentLabel, Acc) ->
parse_account_label(R, AgentLabel, <<Acc/binary, C>>);
parse_account_label(<<>>, AgentLabel, Acc) ->
error({bad_account_label, [AgentLabel, Acc]}).
parse_agent_label(Val).

-spec parse_agent_label(binary()) -> mqttgw_id:agent_id().
parse_agent_label(AgentId) ->
case binary:split(AgentId, <<".">>) of
[_] -> error(bad_agent_label);
[AgentLabel, AccountId] -> parse_account_label(AccountId, AgentLabel)
end.

-spec parse_account_label(binary(), binary()) -> mqttgw_id:agent_id().
parse_account_label(AccountId, AgentLabel) ->
case binary:split(AccountId, <<".">>) of
[_] -> error(bad_account_label);
[AccountLabel, Audience] -> parse_audience(Audience, AgentLabel, AccountLabel)
end.

-spec parse_audience(binary(), binary(), binary()) -> mqttgw_id:agent_id().
parse_audience(<<>>, _AgentLabel, _AccountLabel) ->
Expand Down

0 comments on commit ec172c2

Please sign in to comment.