-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let a data object to be converted from JSON with a factory method ins…
…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
Showing
12 changed files
with
1,440 additions
and
933 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
vertx-codegen-json/src/test/java/io/vertx/test/codegen/converter/AutoMapped.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
vertx-codegen-json/src/test/java/io/vertx/test/codegen/converter/AutoMappedWithVertxGen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
982 changes: 520 additions & 462 deletions
982
vertx-codegen-json/src/test/java/io/vertx/test/codegen/converter/DataObjectTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.