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

Cluster string in Placement is optional. #1150

Merged
merged 5 commits into from
May 17, 2024
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
14 changes: 8 additions & 6 deletions src/main/java/io/nats/client/api/Placement.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import io.nats.client.support.JsonSerializable;
import io.nats.client.support.JsonValue;
import io.nats.client.support.Validator;

import java.util.Arrays;
import java.util.List;
Expand All @@ -24,7 +23,7 @@
import static io.nats.client.support.ApiConstants.TAGS;
import static io.nats.client.support.JsonUtils.*;
import static io.nats.client.support.JsonValueUtils.readOptionalStringList;
import static io.nats.client.support.JsonValueUtils.readString;
import static io.nats.client.support.JsonValueUtils.readStringEmptyAsNull;

/**
* Placement directives to consider when placing replicas of a stream
Expand All @@ -38,7 +37,7 @@ static Placement optionalInstance(JsonValue vPlacement) {
}

Placement(JsonValue vPlacement) {
this.cluster = readString(vPlacement, CLUSTER);
this.cluster = readStringEmptyAsNull(vPlacement, CLUSTER);
this.tags = readOptionalStringList(vPlacement, TAGS);
}

Expand All @@ -48,8 +47,12 @@ static Placement optionalInstance(JsonValue vPlacement) {
* @param tags the list of tags, may be null
*/
public Placement(String cluster, List<String> tags) {
this.cluster = cluster;
this.tags = tags == null || tags.size() == 0 ? null : tags;
this.cluster = cluster == null || cluster.isEmpty() ? null : cluster;
this.tags = tags == null || tags.isEmpty() ? null : tags;
}

public boolean hasData() {
return cluster != null || tags != null;
}

/**
Expand Down Expand Up @@ -133,7 +136,6 @@ public Builder tags(List<String> tags) {
* @return the Placement
*/
public Placement build() {
Validator.required(cluster, "Cluster");
return new Placement(cluster, tags);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/nats/client/api/StreamConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ public String toJson() {
addField(sb, TEMPLATE_OWNER, templateOwner);
addField(sb, DISCARD, discardPolicy.toString());
addFieldAsNanos(sb, DUPLICATE_WINDOW, duplicateWindow);
addField(sb, PLACEMENT, placement);
if (placement != null && placement.hasData()) {
addField(sb, PLACEMENT, placement);
}
addField(sb, REPUBLISH, republish);
addField(sb, SUBJECT_TRANSFORM, subjectTransform);
addField(sb, CONSUMER_LIMITS, consumerLimits);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/nats/client/support/JsonValueUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static String readString(JsonValue jsonValue, String key) {
return read(jsonValue, key, v -> v == null ? null : v.string);
}

public static String readStringEmptyAsNull(JsonValue jsonValue, String key) {
return read(jsonValue, key, v -> v == null ? null : (v.string.isEmpty() ? null : v.string));
}

public static String readString(JsonValue jsonValue, String key, String dflt) {
return read(jsonValue, key, v -> v == null ? dflt : v.string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,25 @@ public static void validateSubjectTransforms(List<SubjectTransform> subjectTrans

@Test
public void testPlacement() {
assertThrows(IllegalArgumentException.class, () -> Placement.builder().build());
assertFalse(Placement.builder().build().hasData());
assertFalse(Placement.builder().cluster(null).build().hasData());
assertFalse(Placement.builder().cluster("").build().hasData());
assertFalse(Placement.builder().tags((List<String>)null).build().hasData());

Placement p = Placement.builder().cluster("cluster").build();
assertEquals("cluster", p.getCluster());
assertNull(p.getTags());
assertTrue(p.hasData());

p = Placement.builder().cluster("cluster").tags("a", "b").build();
assertEquals("cluster", p.getCluster());
assertEquals(2, p.getTags().size());
assertTrue(p.hasData());

p = Placement.builder().cluster("cluster").tags(Arrays.asList("a", "b")).build();
assertEquals("cluster", p.getCluster());
assertEquals(2, p.getTags().size());
assertTrue(p.hasData());
}

@Test
Expand Down
Loading