From ec172c24c2d6d523acc024307ea4ae4eb02a960a Mon Sep 17 00:00:00 2001 From: Shamir Khodzha Date: Mon, 22 Nov 2021 12:11:21 +0300 Subject: [PATCH] implemented agent id parsing with binary splits --- src/mqttgw.erl | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/mqttgw.erl b/src/mqttgw.erl index 29e69a8..4495c00 100644 --- a/src/mqttgw.erl +++ b/src/mqttgw.erl @@ -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(<>, Acc) -> - parse_agent_label(R, <>); -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(<>, AgentLabel, Acc) -> - parse_account_label(R, AgentLabel, <>); -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) ->