Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update regex and parse to support multiple underscores in prefix #363

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions typeid/typeid-sql/sql/03_typeid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ create or replace function typeid_generate(prefix text)
returns typeid
as $$
begin
if (prefix is null) or not (prefix ~ '^[a-z]{0,63}$') then
raise exception 'typeid prefix must match the regular expression [a-z]{0,63}';
if (prefix is null) or not (prefix ~ '^[a-z_]{0,63}$') then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is not the right regex expression for two reasons:

  1. It allows underscores at the beginning
  2. It allows underscores at the end.

But underscores should only be allowed in the middle of the prefix. See https://github.com/jetify-com/typeid/tree/main/spec#type-prefix

The regex suggested there is ^([a-z]([a-z_]{0,61}[a-z])?)?$

raise exception 'typeid prefix must match the regular expression [a-z_]{0,63}';
end if;
return (prefix, uuid_generate_v7())::typeid;
end
Expand All @@ -27,8 +27,8 @@ create or replace function typeid_generate_text(prefix text)
returns text
as $$
begin
if (prefix is null) or not (prefix ~ '^[a-z]{0,63}$') then
raise exception 'typeid prefix must match the regular expression [a-z]{0,63}';
if (prefix is null) or not (prefix ~ '^[a-z_]{0,63}$') then
raise exception 'typeid prefix must match the regular expression [a-z_]{0,63}';
end if;
return typeid_print((prefix, uuid_generate_v7())::typeid);
end
Expand Down Expand Up @@ -73,21 +73,24 @@ as $$
declare
prefix text;
suffix text;
matches text[];
begin
if (typeid_str is null) then
return null;
end if;
if position('_' in typeid_str) = 0 then
return ('', base32_decode(typeid_str))::typeid;
end if;
prefix = split_part(typeid_str, '_', 1);
suffix = split_part(typeid_str, '_', 2);
matches = regexp_match(typeid_str, '^(.*)_(.*)$');
prefix = matches[1];
suffix = matches[2];

if prefix is null or prefix = '' then
raise exception 'typeid prefix cannot be empty with a delimiter';
end if;
-- prefix must match the regular expression [a-z]{0,63}
if not prefix ~ '^[a-z]{0,63}$' then
raise exception 'typeid prefix must match the regular expression [a-z]{0,63}';
-- prefix must match the regular expression [a-z_]{0,63}
if not prefix ~ '^[a-z_]{0,63}$' then
raise exception 'typeid prefix must match the regular expression [a-z_]{0,63}';
end if;

return (prefix, base32_decode(suffix))::typeid;
Expand All @@ -109,8 +112,9 @@ begin
end if;
prefix = (tid).type;
suffix = base32_encode((tid).uuid);
if (prefix is null) or not (prefix ~ '^[a-z]{0,63}$') then
raise exception 'typeid prefix must match the regular expression [a-z]{0,63}';
-- Added underscore to regex allowed characters to match spec
if (prefix is null) or not (prefix ~ '^[a-z_]{0,63}$') then
raise exception 'typeid prefix must match the regular expression [a-z_]{0,63}';
end if;
if prefix = '' then
return suffix;
Expand Down