Skip to content

Commit

Permalink
khepri_condition: Rename re_mp() and fix its use
Browse files Browse the repository at this point in the history
`re_mp()` was clear what it was only for me apparently :) While renaming
the type to something more human-friendly nad making it more precise, I
discovered that the specs were buggy (because of the lack of precision
before).

In the end, instead of having a type for the compiled regex, the type
now represents the return value of `re:compile/1`.
  • Loading branch information
dumbbell committed Oct 5, 2022
1 parent 47851c3 commit 4f3f182
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 4 additions & 2 deletions include/khepri.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@

-record(if_name_matches,
{regex = any :: any | iodata() | unicode:charlist(),
compiled = undefined :: khepri_condition:re_mp() | undefined}).
compiled = undefined :: khepri_condition:re_compile_ret() |
undefined}).

-define(KHEPRI_WILDCARD_STAR, #if_name_matches{regex = any}).

-record(if_path_matches,
{regex = any :: any | iodata() | unicode:charlist(),
compiled = undefined :: khepri_condition:re_mp() | undefined}).
compiled = undefined :: khepri_condition:re_compile_ret() |
undefined}).

-define(KHEPRI_WILDCARD_STAR_STAR, #if_path_matches{regex = any}).

Expand Down
14 changes: 9 additions & 5 deletions src/khepri_condition.erl
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,13 @@
%% This is the same as {@link keep_while()} but the paths in the map keys were
%% converted to native paths if necessary.

-type re_mp() :: tuple().
%% `mp()` type defined by {@link re} but not exported.
-type re_compile_ret() :: {ok, {re_pattern, term(), term(), term(), term()}} |
{error, {string(), non_neg_integer()}}.
%% Return value of {@link re:compile1}.
%%
%% The opaque compiled regex type, {@link re:mp()}, is unfortunately not
%% exported by {@link re}, neither is the error tuple (at least up to
%% Erlang/OTP 25.1).

-export([ensure_native_keep_while/1,
compile/1,
Expand All @@ -308,7 +313,7 @@
comparison_op/1,
keep_while/0,
native_keep_while/0,
re_mp/0]).
re_compile_ret/0]).

-spec ensure_native_keep_while(KeepWhile) -> NativeKeepWhile when
KeepWhile :: keep_while(),
Expand Down Expand Up @@ -556,8 +561,7 @@ term_matches(Term, MatchSpec) ->
-spec eval_regex(Condition, SourceRegex, CompiledRegex, Value) -> Ret when
Condition :: condition_using_regex(),
SourceRegex :: any | iodata() | unicode:charlist(),
CompiledRegex :: {ok, khepri_condition:re_mp()} |
{error, {string(), non_neg_integer()}} |
CompiledRegex :: khepri_condition:re_compile_ret() |
undefined,
Value :: atom() | iodata() | unicode:charlist(),
Ret :: true |
Expand Down

0 comments on commit 4f3f182

Please sign in to comment.