Skip to content

Commit

Permalink
Let a data object to be converted from JSON with a factory method ins…
Browse files Browse the repository at this point in the history
…tead of a constructor. This enables interfaces to be data object and be convertible from JSON in data objects containing them.

In addition we also allow an interface to be annotated both with @VertxGen and @dataobject, this can be useful for avoiding to break backward compatibility.
  • Loading branch information
vietj committed Nov 8, 2023
1 parent b02f0ca commit 2df3f55
Show file tree
Hide file tree
Showing 12 changed files with 1,440 additions and 933 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.vertx.test.codegen.converter;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;

import java.util.Objects;

@DataObject
public interface AutoMapped {

static AutoMapped fromJson(JsonObject json) {
int port = json.getInteger("port", -1);
String host = json.getString("host");
return of(host, port);
}

static AutoMapped of(String host, int port) {
return new AutoMapped() {
@Override
public int port() {
return port;
}
@Override
public String host() {
return host;
}
@Override
public int hashCode() {
return host.hashCode() + port;
}
@Override
public boolean equals(Object obj) {
AutoMapped that = (AutoMapped) obj;
return Objects.equals(host(), that.host()) && port() == that.port();
}
};
}

int port();

String host();

default JsonObject toJson() {
return new JsonObject().put("port", port()).put("host", host());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.vertx.test.codegen.converter;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.json.JsonObject;

import java.util.Objects;

@DataObject
@VertxGen
public interface AutoMappedWithVertxGen {

static AutoMappedWithVertxGen fromJson(JsonObject json) {
int port = json.getInteger("port", -1);
String host = json.getString("host");
return of(host, port);
}

static AutoMappedWithVertxGen of(String host, int port) {
return new AutoMappedWithVertxGen() {
@Override
public int port() {
return port;
}
@Override
public String host() {
return host;
}
@Override
public int hashCode() {
return host.hashCode() + port;
}
@Override
public boolean equals(Object obj) {
AutoMappedWithVertxGen that = (AutoMappedWithVertxGen) obj;
return Objects.equals(host(), that.host()) && port() == that.port();
}
};
}


int port();

String host();

default JsonObject toJson() {
return new JsonObject().put("port", port()).put("host", host());
}
}

Large diffs are not rendered by default.

Loading

0 comments on commit 2df3f55

Please sign in to comment.