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

Remove schema from Service API #916

Merged
merged 1 commit into from
May 19, 2023
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
19 changes: 1 addition & 18 deletions src/examples/java/io/nats/examples/service/ServiceExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public static void main(String[] args) throws IOException {
Endpoint epEcho = Endpoint.builder()
.name("EchoEndpoint")
.subject("echo")
.schemaRequest("echo schema request info") // optional
.schemaResponse("echo schema response info") // optional
.build();

// Sort is going to be grouped. This will affect the actual subject
Expand All @@ -71,8 +69,6 @@ public static void main(String[] args) throws IOException {
.group(sortGroup)
.endpointName("SortEndpointAscending")
.endpointSubject("ascending")
.endpointSchemaRequest("sort ascending schema request info") // optional
.endpointSchemaResponse("sort ascending schema response info") // optional
.handler(msg -> handleSortAscending(nc, msg, "S1A"))
.build();

Expand All @@ -88,7 +84,6 @@ public static void main(String[] args) throws IOException {
Service service1 = new ServiceBuilder()
.connection(nc)
.name("Service1")
.apiUrl("Service1 Api Url") // optional
.description("Service1 Description") // optional
.version("0.0.1")
.addServiceEndpoint(seEcho1)
Expand Down Expand Up @@ -122,7 +117,7 @@ public static void main(String[] args) throws IOException {
String subject = "echo";
CompletableFuture<Message> reply = nc.request(subject, request.getBytes());
String response = new String(reply.get().getData());
System.out.println("" + x + ". Called " + subject + " with [" + request + "] Received " + response);
System.out.println(x + ". Called " + subject + " with [" + request + "] Received " + response);
}

// sort subjects are formed this way because the endpoints have groups
Expand Down Expand Up @@ -165,18 +160,6 @@ public static void main(String[] args) throws IOException {
infoResponses = discovery.info("Service2");
printDiscovery("Info", "Service2", infoResponses);

// ----------------------------------------------------------------------------------------------------
// schema discover variations
// ----------------------------------------------------------------------------------------------------
List<SchemaResponse> schemaResponsList = discovery.schema();
printDiscovery("Schema", "[All]", schemaResponsList);

schemaResponsList = discovery.schema("Service1");
printDiscovery("Schema", "Service1", schemaResponsList);

schemaResponsList = discovery.schema("Service2");
printDiscovery("Schema", "Service2", schemaResponsList);

// ----------------------------------------------------------------------------------------------------
// stats discover variations
// ----------------------------------------------------------------------------------------------------
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/io/nats/service/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,6 @@ public InfoResponse info(String serviceName, String serviceId) {
return jsonBytes == null ? null : new InfoResponse(jsonBytes);
}

// ----------------------------------------------------------------------------------------------------
// schema
// ----------------------------------------------------------------------------------------------------
public List<SchemaResponse> schema() {
return schema(null);
}

public List<SchemaResponse> schema(String serviceName) {
List<SchemaResponse> list = new ArrayList<>();
discoverMany(SRV_SCHEMA, serviceName, jsonBytes -> list.add(new SchemaResponse(jsonBytes)));
return list;
}

public SchemaResponse schema(String serviceName, String serviceId) {
byte[] jsonBytes = discoverOne(SRV_SCHEMA, serviceName, serviceId);
return jsonBytes == null ? null : new SchemaResponse(jsonBytes);
}

// ----------------------------------------------------------------------------------------------------
// stats
// ----------------------------------------------------------------------------------------------------
Expand Down
62 changes: 6 additions & 56 deletions src/main/java/io/nats/service/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

import static io.nats.client.support.ApiConstants.*;
import static io.nats.client.support.JsonUtils.endJson;
import static io.nats.client.support.JsonValueUtils.*;
import static io.nats.client.support.JsonValueUtils.readString;
import static io.nats.client.support.JsonValueUtils.readStringStringMap;
import static io.nats.client.support.Validator.validateIsRestrictedTerm;
import static io.nats.client.support.Validator.validateSubject;

Expand All @@ -32,27 +33,18 @@
public class Endpoint implements JsonSerializable {
private final String name;
private final String subject;
private final Schema schema;
private final Map<String, String> metadata;

public Endpoint(String name, String subject, Schema schema) {
this(name, subject, schema, null, true);
}

public Endpoint(String name) {
this(name, null, null, null, true);
this(name, null, null, true);
}

public Endpoint(String name, String subject) {
this(name, subject, null, null, true);
}

public Endpoint(String name, String subject, String schemaRequest, String schemaResponse) {
this(name, subject, Schema.optionalInstance(schemaRequest, schemaResponse), null, true);
this(name, subject, null, true);
}

// internal use constructors
Endpoint(String name, String subject, Schema schema, Map<String, String> metadata, boolean validate) {
Endpoint(String name, String subject, Map<String, String> metadata, boolean validate) {
if (validate) {
this.name = validateIsRestrictedTerm(name, "Endpoint Name", true);
if (subject == null) {
Expand All @@ -66,27 +58,24 @@ public Endpoint(String name, String subject, String schemaRequest, String schema
this.name = name;
this.subject = subject;
}
this.schema = schema;
this.metadata = metadata == null || metadata.size() == 0 ? null : metadata;
}

Endpoint(JsonValue vEndpoint) {
name = readString(vEndpoint, NAME);
subject = readString(vEndpoint, SUBJECT);
schema = Schema.optionalInstance(readValue(vEndpoint, SCHEMA));
metadata = readStringStringMap(vEndpoint, METADATA);
}

Endpoint(Builder b) {
this(b.name, b.subject, Schema.optionalInstance(b.schemaRequest, b.schemaResponse), b.metadata, true);
this(b.name, b.subject, b.metadata, true);
}

@Override
public String toJson() {
StringBuilder sb = JsonUtils.beginJson();
JsonUtils.addField(sb, NAME, name);
JsonUtils.addField(sb, SUBJECT, subject);
JsonUtils.addField(sb, SCHEMA, schema);
JsonUtils.addField(sb, METADATA, metadata);
return endJson(sb).toString();
}
Expand All @@ -104,10 +93,6 @@ public String getSubject() {
return subject;
}

public Schema getSchema() {
return schema;
}

public Map<String, String> getMetadata() {
return metadata;
}
Expand All @@ -119,22 +104,11 @@ public static Builder builder() {
public static class Builder {
private String name;
private String subject;
private String schemaRequest;
private String schemaResponse;
private Map<String, String> metadata;

public Builder endpoint(Endpoint endpoint) {
name = endpoint.getName();
subject = endpoint.getSubject();
Schema s = endpoint.getSchema();
if (s == null) {
schemaRequest = null;
schemaResponse = null;
}
else {
schemaRequest = s.getRequest();
schemaResponse = s.getResponse();
}
return this;
}

Expand All @@ -148,28 +122,6 @@ public Builder subject(String subject) {
return this;
}

public Builder schemaRequest(String schemaRequest) {
this.schemaRequest = schemaRequest;
return this;
}

public Builder schemaResponse(String schemaResponse) {
this.schemaResponse = schemaResponse;
return this;
}

public Builder schema(Schema schema) {
if (schema == null) {
schemaRequest = null;
schemaResponse = null;
}
else {
schemaRequest = schema.getRequest();
schemaResponse = schema.getResponse();
}
return this;
}

public Builder metadata(Map<String, String> metadata) {
this.metadata = metadata;
return this;
Expand All @@ -189,15 +141,13 @@ public boolean equals(Object o) {

if (!Objects.equals(name, that.name)) return false;
if (!Objects.equals(subject, that.subject)) return false;
if (!Objects.equals(schema, that.schema)) return false;
return JsonUtils.mapEquals(metadata, that.metadata);
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (subject != null ? subject.hashCode() : 0);
result = 31 * result + (schema != null ? schema.hashCode() : 0);
result = 31 * result + (metadata != null ? metadata.hashCode() : 0);
return result;
}
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/io/nats/service/EndpointResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public class EndpointResponse implements JsonSerializable {
private final String name;
private final String subject;
private final Schema schema;
private final long numRequests;
private final long numErrors;
private final long processingTime;
Expand All @@ -50,7 +49,6 @@ static List<EndpointResponse> listOf(JsonValue vEndpointStats) {
EndpointResponse(String name, String subject, long numRequests, long numErrors, long processingTime, String lastError, JsonValue data, ZonedDateTime started) {
this.name = name;
this.subject = subject;
this.schema = null;
this.numRequests = numRequests;
this.numErrors = numErrors;
this.processingTime = processingTime;
Expand All @@ -61,10 +59,9 @@ static List<EndpointResponse> listOf(JsonValue vEndpointStats) {
}

// This is for schema
EndpointResponse(String name, String subject, Schema schema) {
EndpointResponse(String name, String subject) {
this.name = name;
this.subject = subject;
this.schema = schema;
this.numRequests = 0;
this.numErrors = 0;
this.processingTime = 0;
Expand All @@ -77,7 +74,6 @@ static List<EndpointResponse> listOf(JsonValue vEndpointStats) {
EndpointResponse(JsonValue vEndpointResponse) {
name = readString(vEndpointResponse, NAME);
subject = readString(vEndpointResponse, SUBJECT);
schema = Schema.optionalInstance(readValue(vEndpointResponse, SCHEMA));
numRequests = readLong(vEndpointResponse, NUM_REQUESTS, 0);
numErrors = readLong(vEndpointResponse, NUM_ERRORS, 0);
processingTime = readLong(vEndpointResponse, PROCESSING_TIME, 0);
Expand All @@ -92,7 +88,6 @@ public String toJson() {
StringBuilder sb = beginJson();
JsonUtils.addField(sb, NAME, name);
JsonUtils.addField(sb, SUBJECT, subject);
JsonUtils.addField(sb, SCHEMA, schema);
JsonUtils.addFieldWhenGtZero(sb, NUM_REQUESTS, numRequests);
JsonUtils.addFieldWhenGtZero(sb, NUM_ERRORS, numErrors);
JsonUtils.addFieldWhenGtZero(sb, PROCESSING_TIME, processingTime);
Expand All @@ -111,10 +106,6 @@ public String getSubject() {
return subject;
}

public Schema getSchema() {
return schema;
}

public long getNumRequests() {
return numRequests;
}
Expand Down Expand Up @@ -161,7 +152,6 @@ public boolean equals(Object o) {
if (averageProcessingTime != that.averageProcessingTime) return false;
if (!Objects.equals(name, that.name)) return false;
if (!Objects.equals(subject, that.subject)) return false;
if (!Objects.equals(schema, that.schema)) return false;
if (!Objects.equals(lastError, that.lastError)) return false;
if (!Objects.equals(data, that.data)) return false;
return Objects.equals(started, that.started);
Expand All @@ -171,7 +161,6 @@ public boolean equals(Object o) {
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (subject != null ? subject.hashCode() : 0);
result = 31 * result + (schema != null ? schema.hashCode() : 0);
result = 31 * result + (int) (numRequests ^ (numRequests >>> 32));
result = 31 * result + (int) (numErrors ^ (numErrors >>> 32));
result = 31 * result + (int) (processingTime ^ (processingTime >>> 32));
Expand Down
Loading