From 14f479fec5565d973af6f1b8f0e16c8ed645f750 Mon Sep 17 00:00:00 2001 From: Sam Jakos <23317824+sjakos@users.noreply.github.com> Date: Wed, 7 Aug 2024 22:11:09 +0000 Subject: [PATCH 1/2] update regex and parse to support multiple underscores in prefix --- typeid/typeid-sql/sql/03_typeid.sql | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/typeid/typeid-sql/sql/03_typeid.sql b/typeid/typeid-sql/sql/03_typeid.sql index b827707a..5b9bcab4 100644 --- a/typeid/typeid-sql/sql/03_typeid.sql +++ b/typeid/typeid-sql/sql/03_typeid.sql @@ -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 + raise exception 'typeid prefix must match the regular expression [a-z_]{0,63}'; end if; return (prefix, uuid_generate_v7())::typeid; end @@ -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 @@ -80,14 +80,16 @@ begin 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; @@ -109,8 +111,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; From 18caf64d97362877002aa4e4f8ab0b8e31c762eb Mon Sep 17 00:00:00 2001 From: Sam Jakos <23317824+sjakos@users.noreply.github.com> Date: Thu, 8 Aug 2024 12:50:29 +0000 Subject: [PATCH 2/2] fix missing parse matches declaration --- typeid/typeid-sql/sql/03_typeid.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/typeid/typeid-sql/sql/03_typeid.sql b/typeid/typeid-sql/sql/03_typeid.sql index 5b9bcab4..5e088188 100644 --- a/typeid/typeid-sql/sql/03_typeid.sql +++ b/typeid/typeid-sql/sql/03_typeid.sql @@ -73,6 +73,7 @@ as $$ declare prefix text; suffix text; + matches text[]; begin if (typeid_str is null) then return null;