<commit_before>%%%---------------------------------------------------------------------
%%% @copyright 2016-2017 SigScale Global Inc
%%% @author <NAME> <<EMAIL>> [http://www.sigscale.org]
%%% @end
%%%
%%% Copyright (c) 2016-2017, SigScale Global Inc
%%%
%%% All rights reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%%---------------------------------------------------------------------
%%% @doc This library module implements a user API for attributes in the
%%% 		{@link //radius. radius} application.
%%%
-module(radius_attributes).
-copyright('Copyright (c) 2016-2017 SigScale Global Inc').
-author('<EMAIL>').

%% export the radius_attributes public API
-export([new/0, store/3, add/3, fetch/2, find/2, find/3, get_all/2]).
-export([codec/1]).
-export([hide/3, unhide/3, error_cause/1]).

-export_type([attributes/0]).

%% @headerfile "radius.hrl"
-include("radius.hrl").

%%----------------------------------------------------------------------
%%  The radius_attributes public API
%%----------------------------------------------------------------------

-type attributes() :: list({Attribute :: byte(), Value :: term()}).

-spec new() -> Attributes :: attributes().
%% @doc Create a new RADIUS protocol attributes list.
%%
new() ->
	[].

-spec store(Attribute :: byte(), Value :: term(),
	Attributes :: attributes()) -> NewAttributes :: attributes().
%% @doc Add a new attribute to a RADIUS protocol attributes list.
%% 	If `Attribute' exists it is overwritten with the new `Value'.
%%
store(Attribute, Value, Attributes) when is_integer(Attribute),
		is_list(Attributes) ->
	lists:keystore(Attribute, 1, Attributes, {Attribute, Value}).

-spec add(Attribute :: byte(), Value :: term(),
	Attributes :: attributes()) -> NewAttributes :: attributes().
%% @doc Add an attribute to a RADIUS protocol attributes list.
%% 	Multiple `Attribute's are allowed.
%%
add(Attribute, Value, Attributes) when is_integer(Attribute),
		is_list(Attributes) ->
	Attributes ++ [{Attribute, Value}].

-spec fetch(Attribute :: byte(), Attributes :: attributes()) ->
	Value :: term().
%% @doc Returns the value for an attribute in a RADIUS protocol
%% 	attributes list.  Assumes that the attribute is present.
%%
fetch(Attribute, Attributes) ->
	case lists:keyfind(Attribute, 1, Attributes) of
		false ->
			exit(not_found);
		{Attribute, Value} ->
			Value
	end.

-spec find(Attribute :: byte(), Attributes :: attributes()) ->
	Result :: {ok, Value :: term()} | {error, not_found}.
%% 	Attribute = integer()
%% 	Attributes = attributes()
%% 	Result = {ok, Value} | error
%% 	Value = term()
%% @doc Searches for an attribute in a RADIUS protocol attributes list.
%%<commit_msg>radius attribute fetch function for vedor specific attributes<commit_after>%%%---------------------------------------------------------------------
%%% @copyright 2016-2017 SigScale Global Inc
%%% @author <NAME> <<EMAIL>> [http://www.sigscale.org]
%%% @end
%%%
%%% Copyright (c) 2016-2017, SigScale Global Inc
%%%
%%% All rights reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%%---------------------------------------------------------------------
%%% @doc This library module implements a user API for attributes in the
%%% 		{@link //radius. radius} application.
%%%
-module(radius_attributes).
-copyright('Copyright (c) 2016-2017 SigScale Global Inc').
-author('<EMAIL>').

%% export the radius_attributes public API
-export([new/0, store/3, add/3, fetch/2, fetch/3,
		find/2, find/3, get_all/2]).
-export([codec/1]).
-export([hide/3, unhide/3, error_cause/1]).

-export_type([attributes/0]).

%% @headerfile "radius.hrl"
-include("radius.hrl").

%%----------------------------------------------------------------------
%%  The radius_attributes public API
%%----------------------------------------------------------------------

-type attributes() :: list({Attribute :: byte(), Value :: term()}).

-spec new() -> Attributes :: attributes().
%% @doc Create a new RADIUS protocol attributes list.
%%
new() ->
	[].

-spec store(Attribute :: byte(), Value :: term(),
	Attributes :: attributes()) -> NewAttributes :: attributes().
%% @doc Add a new attribute to a RADIUS protocol attributes list.
%% 	If `Attribute' exists it is overwritten with the new `Value'.
%%
store(Attribute, Value, Attributes) when is_integer(Attribute),
		is_list(Attributes) ->
	lists:keystore(Attribute, 1, Attributes, {Attribute, Value}).

-spec add(Attribute :: byte(), Value :: term(),
	Attributes :: attributes()) -> NewAttributes :: attributes().
%% @doc Add an attribute to a RADIUS protocol attributes list.
%% 	Multiple `Attribute's are allowed.
%%
add(Attribute, Value, Attributes) when is_integer(Attribute),
		is_list(Attributes) ->
	Attributes ++ [{Attribute, Value}].

-spec fetch(Attribute :: byte(), Attributes :: attributes()) ->
	Value :: term().
%% @doc Returns the value for an attribute in a RADIUS protocol
%% 	attributes list.  Assumes that the attribute is present.
%%
fetch(Attribute, Attributes) ->
	case lists:keyfind(Attribute, 1, Attributes) of
		false ->
			exit(not_found);
		{Attribute, Value} ->
			Value
	end.

-spec fetch(Vendor :: byte(), Attribute :: byte(), Attributes :: attributes()) ->
	Value :: term().
%% @doc Returns the value for a vendor sepecific attribute in a RADIUS protocol
%% 	attributes list.  Assumes that the attribute is present.
%%
fetch(Vendor, Attribute, [{?VendorSpecific, {Vendor, {Attribute, Value}}} | _]) ->
	Value;
fetch(Vendor, Attribute, [_ | T]) ->
	fetch(Vendor, Attribute, T);
fetch(_Vendor, _Attribute, []) ->
	exit(not_found).

-spec find(Attribute :: byte(), Attributes :: attributes()) ->
	Result :: {ok, Value :: term()} | {error, not_found}.
%% 	Attribute = integer()
%% 	Attributes = attributes()
%% 	Result = {ok, Value} | error
%% 	Value = term()
%% @doc Searches for an attribute in a RADIUS protocol attributes list.
%%
{'fraction_non_alphanumeric': 0.22990167727009833, 'fraction_numerical': 0.012290341237709659, 'mean_word_length': 3.0592723004694835, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119377', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1885, 'n_words': 672}