Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

fix: enable complex resource ids in instantiate() #159

Merged
merged 1 commit into from Jul 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/com/google/api/pathtemplate/PathTemplate.java
Expand Up @@ -200,6 +200,11 @@ private static Segment create(SegmentKind kind, String value) {
return new AutoValue_PathTemplate_Segment(kind, value, "");
}

/** Creates a segment of given kind, value, and complex separator. */
private static Segment create(SegmentKind kind, String value, String complexSeparator) {
return new AutoValue_PathTemplate_Segment(kind, value, complexSeparator);
}

private static Segment wildcardCreate(String complexSeparator) {
return new AutoValue_PathTemplate_Segment(
SegmentKind.WILDCARD,
Expand Down Expand Up @@ -737,10 +742,13 @@ private String instantiate(Map<String, String> values, boolean allowPartial) {
boolean continueLast = true; // Whether to not append separator
boolean skip = false; // Whether we are substituting a binding and segments shall be skipped.
ListIterator<Segment> iterator = segments.listIterator();
String prevSeparator = "";
while (iterator.hasNext()) {
Segment seg = iterator.next();
if (!skip && !continueLast) {
result.append(seg.separator());
String separator = prevSeparator.isEmpty() ? seg.separator() : prevSeparator;
result.append(separator);
prevSeparator = seg.complexSeparator().isEmpty() ? seg.separator() : seg.complexSeparator();
}
continueLast = false;
switch (seg.kind()) {
Expand Down Expand Up @@ -1066,7 +1074,7 @@ private static List<Segment> parseComplexResourceId(String seg) {
currIteratorIndex < separatorIndices.size()
? separatorIndices.get(currIteratorIndex)
: "";
segments.add(Segment.create(SegmentKind.BINDING, subVarName));
segments.add(Segment.create(SegmentKind.BINDING, subVarName, complexDelimiter));
segments.add(Segment.wildcardCreate(complexDelimiter));
segments.add(Segment.END_BINDING);
subVarName = null;
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
Expand Up @@ -37,6 +37,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -377,6 +378,26 @@ public void complexResourceIdInParent() {
Truth.assertThat(match.get("cell2")).isEqualTo("broomba");
}

@Test
public void complexResourcePathTemplateVariables() {
String pattern =
"projects/{foo}_{bar}/zones/{zone_a}-{zone_b}_{zone_c}/machines/{cell1}.{cell2}";
PathTemplate template = PathTemplate.create(pattern);
Set<String> variables = template.vars();
Truth.assertThat(variables)
.containsExactly("foo", "bar", "zone_a", "zone_b", "zone_c", "cell1", "cell2");

pattern = "projects/{foo}_{bar}/zones/*";
template = PathTemplate.create(pattern);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variables probably shouldn't be reused

Map<String, String> match =
template.match("https://www.googleapis.com/compute/v1/projects/foo1_bar2/zones/azone");
Truth.assertThat(match).isNotNull();
Truth.assertThat(match.get("foo")).isEqualTo("foo1");
Truth.assertThat(match.get("bar")).isEqualTo("bar2");
variables = template.vars();
System.out.println("DEL: vars: " + variables);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be here

}

@Test
public void complexResourceBasicInvalidIds() {
thrown.expect(ValidationException.class);
Expand Down Expand Up @@ -575,6 +596,57 @@ public void instantiateWithUnusualCharactersNoEncoding() {
Truth.assertThat(instance).isEqualTo("bar/asdf:;`~,.<>[]!@#$%^&*()");
}

@Test
public void instantiateWithComplexResourceId_basic() {
PathTemplate template = PathTemplate.create("projects/{project}/zones/{zone_a}~{zone_b}");
String instance =
template.instantiate("project", "a/b/c", "zone_a", "apple", "zone_b", "baseball");
Truth.assertThat(instance).isEqualTo("projects/a%2Fb%2Fc/zones/apple~baseball");
}

@Test
public void instantiateWithComplexResourceId_mixedSeparators() {
PathTemplate template =
PathTemplate.create(
"projects/{project}/zones/{zone_a}~{zone_b}.{zone_c}-{zone_d}~{zone_e}");
String instance =
template.instantiate(
"project",
"a/b/c",
"zone_a",
"apple",
"zone_b",
"baseball/basketball",
"zone_c",
"cat/kitty",
"zone_d",
"dog/hound",
"zone_e",
"12345");
Truth.assertThat(instance)
.isEqualTo(
"projects/a%2Fb%2Fc/zones/apple~baseball%2Fbasketball.cat%2Fkitty-dog%2Fhound~12345");
}

@Test
public void instantiateWithComplexResourceId_mixedSeparatorsInParent() {
PathTemplate template =
PathTemplate.create("projects/{project_a}~{project_b}.{project_c}/zones/{zone_a}~{zone_b}");
String instance =
template.instantiate(
"project_a",
"a/b/c",
"project_b",
"foo",
"project_c",
"bar",
"zone_a",
"apple",
"zone_b",
"baseball");
Truth.assertThat(instance).isEqualTo("projects/a%2Fb%2Fc~foo.bar/zones/apple~baseball");
}

// Other
// =====

Expand Down