forked from smallrye/smallrye-converters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
smallrye#5 - Prototype for single dimension converters.
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
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
60 changes: 60 additions & 0 deletions
60
implementation/src/test/java/io/smallrye/converters/type/ConvertedType.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,60 @@ | ||
package io.smallrye.converters.type; | ||
|
||
import java.io.StringReader; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
|
||
import io.smallrye.converters.SmallRyeConverters; | ||
import io.smallrye.converters.SmallRyeConvertersBuilder; | ||
import io.smallrye.converters.api.Converter; | ||
|
||
public class ConvertedType { | ||
private final Object rawValue; | ||
|
||
private final SmallRyeConverters converters = new SmallRyeConvertersBuilder() | ||
// This does not sit here. Just for prototype. | ||
.withConverter(JsonObject.class, 100, new Converter<JsonObject>() { | ||
@Override | ||
public JsonObject convert(final String value) { | ||
return Json.createReader(new StringReader(value)).readObject(); | ||
} | ||
}) | ||
.build(); | ||
|
||
public ConvertedType(final Object value) { | ||
this.rawValue = value; | ||
} | ||
|
||
public Object getRawValue() { | ||
return rawValue; | ||
} | ||
|
||
public String getAsString() { | ||
return rawValue.toString(); | ||
} | ||
|
||
public Number getAsNumber() { | ||
if (rawValue instanceof Number) { | ||
return (Number) rawValue; | ||
} | ||
|
||
final Converter<Double> converter = converters.getConverter(Double.class); | ||
return converter != null ? converter.convert(rawValue.toString()) : null; | ||
} | ||
|
||
public Boolean getAsBoolean() { | ||
return null; | ||
} | ||
|
||
// TODO - add more suppoted types | ||
|
||
public <T> T getAs(Class<T> klass) { | ||
if (klass.isInstance(rawValue)) { | ||
return (T) rawValue; | ||
} | ||
|
||
final Converter<T> converter = converters.getConverter(klass); | ||
return converter != null ? converter.convert(rawValue.toString()) : null; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
implementation/src/test/java/io/smallrye/converters/type/InputConverter.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,7 @@ | ||
package io.smallrye.converters.type; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface InputConverter<T> extends Serializable { | ||
ConvertedType convert(T value); | ||
} |
129 changes: 129 additions & 0 deletions
129
implementation/src/test/java/io/smallrye/converters/type/InputOutputConverters.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,129 @@ | ||
package io.smallrye.converters.type; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import org.eclipse.yasson.YassonJsonb; | ||
import org.junit.Test; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.yaml.snakeyaml.constructor.Constructor; | ||
|
||
public class InputOutputConverters { | ||
@Test | ||
public void integer() { | ||
final ConvertedType value = new ConvertedType("1"); | ||
assertEquals(1, new IntegerOutputConverter().convert(value).intValue()); | ||
} | ||
|
||
@Test | ||
public void jsonObject() { | ||
final JsonObject jsonObject = Json.createObjectBuilder().add("street", "abc def").add("code", 1234) | ||
.add("country", "ABC").build(); | ||
|
||
final ConvertedType value = new ConvertedType(jsonObject); | ||
final Address address = new AddressOutputConverter().convert(value); | ||
assertEquals("abc def", address.getStreet()); | ||
assertEquals(1234, address.getCode().intValue()); | ||
assertEquals("ABC", address.getCountry()); | ||
} | ||
|
||
@Test | ||
public void jsonAsString() { | ||
final String json = Json.createObjectBuilder() | ||
.add("street", "abc def") | ||
.add("code", 1234) | ||
.add("country", "ABC") | ||
.build() | ||
.toString(); | ||
|
||
final ConvertedType value = new ConvertedType(json); | ||
final Address address = new AddressOutputConverter().convert(value); | ||
assertEquals("abc def", address.getStreet()); | ||
assertEquals(1234, address.getCode().intValue()); | ||
assertEquals("ABC", address.getCountry()); | ||
} | ||
|
||
@Test | ||
public void yaml() { | ||
final String yml = "street: abc def\n" + "code: 1234\n" + "country: ABC\n"; | ||
|
||
final ConvertedType value = new ConvertedType(yml); | ||
final OutputConverter<Address> outputConverter = new OutputConverter<Address>() { | ||
@Override | ||
public Address convert(final ConvertedType value) { | ||
return (Address) new Yaml(new Constructor(Address.class)).load(value.getAsString()); | ||
} | ||
}; | ||
final Address address = outputConverter.convert(value); | ||
|
||
assertEquals("abc def", address.getStreet()); | ||
assertEquals(1234, address.getCode().intValue()); | ||
assertEquals("ABC", address.getCountry()); | ||
} | ||
|
||
public static class IntegerOutputConverter implements OutputConverter<Integer> { | ||
@Override | ||
public Integer convert(final ConvertedType value) { | ||
return value.getAsNumber().intValue(); | ||
} | ||
} | ||
|
||
public static class AddressOutputConverter implements OutputConverter<Address> { | ||
@Override | ||
public Address convert(final ConvertedType value) { | ||
final YassonJsonb jsonb = (YassonJsonb) JsonbBuilder.create(); | ||
return jsonb.fromJsonStructure(value.getAs(JsonObject.class), Address.class); | ||
} | ||
} | ||
|
||
public static class Address { | ||
private String street; | ||
private Integer code; | ||
private String country; | ||
|
||
public Address() { | ||
} | ||
|
||
public Address(final String street, final Integer code, final String country) { | ||
this.street = street; | ||
this.code = code; | ||
this.country = country; | ||
} | ||
|
||
public String getStreet() { | ||
return street; | ||
} | ||
|
||
public void setStreet(final String street) { | ||
this.street = street; | ||
} | ||
|
||
public Integer getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(final Integer code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCountry() { | ||
return country; | ||
} | ||
|
||
public void setCountry(final String country) { | ||
this.country = country; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Address{" + | ||
"street='" + street + '\'' + | ||
", code=" + code + | ||
", country='" + country + '\'' + | ||
'}'; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
implementation/src/test/java/io/smallrye/converters/type/OutputConverter.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,7 @@ | ||
package io.smallrye.converters.type; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface OutputConverter<T> extends Serializable { | ||
T convert(ConvertedType value); | ||
} |