Skip to content

Commit

Permalink
smallrye#5 - Prototype for single dimension converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 3, 2020
1 parent 72c15a7 commit a1f27fe
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 0 deletions.
20 changes: 20 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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;
}
}
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);
}
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 + '\'' +
'}';
}
}
}
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);
}

0 comments on commit a1f27fe

Please sign in to comment.