Skip to content

Commit

Permalink
Cluster string in Placement is optional. (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored May 17, 2024
1 parent 8da8e3a commit 54ca97f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
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

0 comments on commit 54ca97f

Please sign in to comment.