Skip to content

fix: parse typeids with multiple underscores in the suffix #467

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

Open
wants to merge 1 commit 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
44 changes: 26 additions & 18 deletions typeid/typeid-sql/sql/03_typeid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,42 @@ language plpgsql
immutable;

-- Function that parses a string into a typeid.
create or replace function typeid_parse(typeid_str text)
returns typeid
as $$
create or replace function public.typeid_parse(typeid_str text)
returns uuid
language plpgsql
immutable
as $function$
declare
parts text[]; -- array to hold split parts of the string
prefix text;
suffix 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;
parts := string_to_array(typeid_str, '_');
if array_length(parts, 1) = 1 then
suffix := parts[1];
prefix := '';
else
suffix := parts[array_length(parts, 1)]; -- last part is the suffix
prefix := array_to_string(parts[1:array_length(parts, 1)-1], '_'); -- join all but last part with '_'
if prefix = '' then
raise exception 'typeid prefix cannot be empty with a delimiter';
end if;
end if;
prefix = split_part(typeid_str, '_', 1);
suffix = split_part(typeid_str, '_', 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}';
if prefix != '' then
-- updated regex allows underscores in prefix, e.g., "doctor_note_schema"
if not prefix ~ '^[a-z]+(_[a-z]+)*$' then
raise exception 'typeid prefix must match the regular expression ^[a-z]+(_[a-z]+)*$';
end if;
if length(prefix) > 63 then
raise exception 'typeid prefix must be at most 63 characters';
end if;
end if;

return (prefix, base32_decode(suffix))::typeid;
return base32_decode(suffix)::uuid;
end
$$
language plpgsql
immutable;
$function$

-- Function that serializes a typeid into a string.
create or replace function typeid_print(tid typeid)
Expand Down