catalog-model: enable usage of : and / in entity names

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-03-08 17:28:28 +01:00
parent 257af4c80e
commit 2952566587
3 changed files with 39 additions and 12 deletions
+18 -5
View File
@@ -34,11 +34,24 @@ describe('ref', () => {
it('rejects bad inputs', () => {
expect(() => parseEntityRef(null as any)).toThrow();
expect(() => parseEntityRef(7 as any)).toThrow();
expect(() => parseEntityRef('a:b:c')).toThrow();
expect(() => parseEntityRef('a/b/c')).toThrow();
expect(() => parseEntityRef('a/b:c')).toThrow();
expect(() => parseEntityRef('a:b/c/d')).toThrow();
expect(() => parseEntityRef('a:b/c:d')).toThrow();
});
it('allows names with : and /', () => {
expect(
parseEntityRef('a:b:c', { defaultKind: 'k', defaultNamespace: 'ns' }),
).toEqual({ kind: 'a', namespace: 'ns', name: 'b:c' });
expect(
parseEntityRef('a/b/c', { defaultKind: 'k', defaultNamespace: 'ns' }),
).toEqual({ kind: 'k', namespace: 'a', name: 'b/c' });
expect(
parseEntityRef('a/b:c', { defaultKind: 'k', defaultNamespace: 'ns' }),
).toEqual({ kind: 'k', namespace: 'a', name: 'b:c' });
expect(
parseEntityRef('a:b/c/d', { defaultKind: 'k', defaultNamespace: 'ns' }),
).toEqual({ kind: 'a', namespace: 'b', name: 'c/d' });
expect(
parseEntityRef('a:b/c:d', { defaultKind: 'k', defaultNamespace: 'ns' }),
).toEqual({ kind: 'a', namespace: 'b', name: 'c:d' });
});
it('rejects empty parts in strings', () => {
+14 -7
View File
@@ -23,18 +23,25 @@ function parseRefString(ref: string): {
namespace?: string;
name: string;
} {
const match = /^([^:/]+:)?([^:/]+\/)?([^:/]+)$/.exec(ref.trim());
if (!match) {
let colonI = ref.indexOf(':');
const slashI = ref.indexOf('/');
// If the / is ahead of the :, treat the rest as the name
if (slashI !== -1 && slashI < colonI) {
colonI = -1;
}
const kind = colonI === -1 ? undefined : ref.slice(0, colonI);
const namespace = slashI === -1 ? undefined : ref.slice(colonI + 1, slashI);
const name = ref.slice(Math.max(colonI + 1, slashI + 1));
if (kind === '' || namespace === '' || name === '') {
throw new TypeError(
`Entity reference "${ref}" was not on the form [<kind>:][<namespace>/]<name>`,
);
}
return {
kind: match[1]?.slice(0, -1),
namespace: match[2]?.slice(0, -1),
name: match[3],
};
return { kind, namespace, name };
}
/**