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

fix: segment in path template can contain wildcard #849

Merged
merged 3 commits into from
Jun 8, 2020
Merged
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
14 changes: 11 additions & 3 deletions src/pathTemplate.ts
Original file line number Diff line number Diff line change
@@ -182,9 +182,6 @@ export class PathTemplate {
index = index + 1;
if (segment === '**') {
wildCardCount = wildCardCount + 1;
if (wildCardCount > 1) {
throw new TypeError('Can not have more than one wildcard.');
}
}
}
// {project}~{location} -> {project=*}~{location=*}
@@ -208,6 +205,14 @@ export class PathTemplate {
this.bindings[variable![0]] = '*';
segments.push(`{${variable![0]}=*}`);
}
// {project=**} -> segments.push('{project=**}');
// -> bindings['project'] = '**'
else if (segment.match(/(?<={)[0-9a-zA-Z-.~_]+(?=(=\*\*)})/)) {
const variable = segment.match(/(?<={)[0-9a-zA-Z-.~_]+(?=(=\*\*)})/);
this.bindings[variable![0]] = '**';
segments.push(`{${variable![0]}=**}`);
wildCardCount = wildCardCount + 1;
}
// {hello=/what} -> segments.push('{hello=/what}');
// -> no binding in this case
else if (segment.match(/(?<={)[0-9a-zA-Z-.~_]+=[^*]+(?=})/)) {
@@ -218,6 +223,9 @@ export class PathTemplate {
else if (segment.match(/[0-9a-zA-Z-.~_]+/)) {
segments.push(segment);
}
if (wildCardCount > 1) {
throw new TypeError('Can not have more than one wildcard.');
}
});
return segments;
}
2 changes: 1 addition & 1 deletion test/system-test/test.clientlibs.ts
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ async function runSystemTest(packageName: string): Promise<void> {
stdio: 'inherit',
});
}

// nodejs-kms does not have system test.
async function runSamplesTest(packageName: string): Promise<void> {
await execa('npm', ['run', 'samples-test'], {
cwd: packageName,
18 changes: 18 additions & 0 deletions test/unit/pathTemplate.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,13 @@ describe('PathTemplate', () => {
};
assert.throws(shouldFail, TypeError);
});

it('should fail on multiple path wildcards', () => {
const shouldFail = () => {
return new PathTemplate('buckets/*/**/{project=**}/objects/*');
};
assert.throws(shouldFail, TypeError);
});
});

describe('method `match`', () => {
@@ -152,6 +159,17 @@ describe('PathTemplate', () => {
const want = 'buckets/f/o/o/objects/google.com:a-b';
assert.strictEqual(template.render(params), want);
});
it('should render atomic resource', () => {
const template = new PathTemplate(
'projects/{project}/metricDescriptors/{metric_descriptor=**}'
);
const params = {
project: 'project-name',
metric_descriptor: 'descriptor',
};
const want = 'projects/project-name/metricDescriptors/descriptor';
assert.strictEqual(template.render(params), want);
});
it('should render atomic resource', () => {
const template = new PathTemplate('buckets/{project=*}');
const params = {