Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/inflection/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
EXTENSION = pgpm-inflection
DATA = sql/pgpm-inflection--0.15.5.sql
DATA = sql/pgpm-inflection--0.30.0.sql

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
Expand Down
30 changes: 30 additions & 0 deletions packages/inflection/__tests__/inflection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,34 @@ describe('inflection', () => {
{ name: 'information', result: 'information' }
]
);

cases(
'dns_1123',
async (opts: { name: string; result: string }) => {
const { dns_1123 } = await pg.one(
'SELECT * FROM inflection.dns_1123($1)',
[opts.name]
);
expect(dns_1123).toEqual(opts.result);
},
[
{ name: 'api', result: 'api' },
{ name: 'MyService', result: 'myservice' },
{ name: 'my_service', result: 'my-service' },
{ name: 'org:api', result: 'org--api' },
{ name: 'Hello, World!', result: 'helloworld' },
{ name: '__leading', result: 'leading' },
{ name: 'trailing__', result: 'trailing' },
{ name: '---', result: '' },
{
name: 'a'.repeat(80),
result: 'a'.repeat(63)
},
{
// truncation lands on a '-'; trailing non-alphanumerics are dropped
name: `${'a'.repeat(62)}-bcd`,
result: 'a'.repeat(62)
}
]
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- Deploy schemas/inflection/procedures/dns_1123 to pg

-- requires: schemas/inflection/schema

-- Normalizes a value into a Kubernetes DNS-1123 label (RFC 1123):
-- lowercase alphanumerics and '-', at most 63 characters, and no leading or
-- trailing '-'. Used to derive Knative/K8s object names from arbitrary slugs.

BEGIN;

CREATE FUNCTION inflection.dns_1123 (value text)
RETURNS text
AS $$
WITH lowercased AS (
SELECT
lower(value) AS value
),
-- ':' delimits namespaced identifiers; map to '--' so the boundary survives
namespaced AS (
SELECT
replace(value, ':', '--') AS value
FROM
lowercased
),
hyphenated AS (
SELECT
replace(value, '_', '-') AS value
FROM
namespaced
),
stripped AS (
SELECT
regexp_replace(value, '[^a-z0-9-]', '', 'g') AS value
FROM
hyphenated
),
trimmed AS (
SELECT
regexp_replace(value, '^-+|-+$', '', 'g') AS value
FROM
stripped
),
truncated AS (
SELECT
"left"(value, 63) AS value
FROM
trimmed
),
-- truncation can leave a dangling '-'; drop any trailing non-alphanumerics
final AS (
SELECT
regexp_replace(value, '[^a-z0-9]+$', '') AS value
FROM
truncated
)
SELECT
value
FROM
final;
$$
LANGUAGE SQL
STRICT IMMUTABLE;

COMMIT;
2 changes: 1 addition & 1 deletion packages/inflection/pgpm-inflection.control
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pgpm-inflection extension
comment = 'pgpm-inflection extension'
default_version = '0.15.5'
default_version = '0.30.0'
module_pathname = '$libdir/pgpm-inflection'
requires = 'plpgsql,unaccent,pgpm-verify'
relocatable = false
Expand Down
3 changes: 2 additions & 1 deletion packages/inflection/pgpm.plan
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%syntax-version=1.0.0
%project=pgpm-inflection
%uri=pgpm-inflection

schemas/inflection/schema 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/schema
schemas/inflection/procedures/no_consecutive_caps [schemas/inflection/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/procedures/no_consecutive_caps
schemas/inflection/procedures/pg_slugify [schemas/inflection/schema schemas/inflection/procedures/no_consecutive_caps] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/procedures/pg_slugify
Expand All @@ -18,3 +18,4 @@ schemas/inflection/procedures/singular [schemas/inflection/schema schemas/inflec
schemas/inflection/procedures/slugify [schemas/inflection/schema] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/procedures/slugify
schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/tables/inflection_rules/fixtures/1589249334312_fixture
schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx [schemas/inflection/schema schemas/inflection/tables/inflection_rules/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/inflection/tables/inflection_rules/indexes/inflection_rules_type_idx
schemas/inflection/procedures/dns_1123 [schemas/inflection/schema] 2026-07-11T22:14:14Z constructive <constructive@5b0c196eeb62> # Kubernetes DNS-1123 label normalization
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/inflection/procedures/dns_1123 from pg

BEGIN;

DROP FUNCTION inflection.dns_1123(text);

COMMIT;
Loading
Loading