Skip to content

Commit

Permalink
Make it possible to use subtypes of JsonValue in ASPropertyField (#4488)
Browse files Browse the repository at this point in the history
Fixes #4463
  • Loading branch information
Denis authored and ZheSun88 committed Aug 13, 2018
1 parent 04c5f1a commit 2869622
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.stream.Collectors;

import com.googlecode.gentyref.GenericTypeReflector;

import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.PropertyChangeEvent;
import com.vaadin.flow.function.SerializableBiConsumer;
Expand Down Expand Up @@ -127,13 +126,7 @@ private <C extends AbstractField<C, V>, V> SerializableBiConsumer<C, V> createWr
Boolean.FALSE);
addHandler(Element::setProperty, Element::getProperty, Integer.class,
Integer.valueOf(0));
addHandler(Element::setPropertyJson,
(element, property, defaultValue) -> {
Serializable value = element.getPropertyRaw(property);
// JsonValue is passed straight through, other primitive
// values are jsonified
return JsonCodec.encodeWithoutTypeInfo(value);
}, JsonValue.class, null);
typeHandlers.put(JsonValue.class, getHandler(JsonValue.class));
}

private final SerializableBiConsumer<C, T> propertyWriter;
Expand Down Expand Up @@ -237,18 +230,7 @@ public <P> AbstractSinglePropertyField(String propertyName, T defaultValue,
}
}

@SuppressWarnings("unchecked")
TypeHandler<P> typeHandler = (TypeHandler<P>) typeHandlers
.get(elementPropertyType);
if (typeHandler == null) {
throw new IllegalArgumentException(
"Unsupported element property type: "
+ elementPropertyType.getName()
+ ". Supported types are: "
+ typeHandlers.keySet().parallelStream()
.map(Class::getName)
.collect(Collectors.joining(", ")));
}
TypeHandler<P> typeHandler = findHandler(elementPropertyType);

Element element = getElement();

Expand All @@ -264,6 +246,23 @@ public <P> AbstractSinglePropertyField(String propertyName, T defaultValue,
SharedUtil.camelCaseToDashSeparated(propertyName) + "-changed");
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private <P> TypeHandler<P> findHandler(Class<P> clazz) {
TypeHandler<P> typeHandler = (TypeHandler<P>) typeHandlers.get(clazz);
if (typeHandler == null && JsonValue.class.isAssignableFrom(clazz)) {
typeHandler = getHandler((Class) clazz);
}
if (typeHandler == null) {
throw new IllegalArgumentException(
"Unsupported element property type: " + clazz.getName()
+ ". Supported types are: "
+ typeHandlers.keySet().parallelStream()
.map(Class::getName)
.collect(Collectors.joining(", ")));
}
return typeHandler;
}

@SuppressWarnings("unchecked")
private static <T> Class<T> findElementPropertyTypeFromTypeParameter(
Class<?> hasValueClass) {
Expand Down Expand Up @@ -334,6 +333,19 @@ protected void setPresentationValue(T newPresentationValue) {
propertyWriter.accept((C) this, newPresentationValue);
}

private static <P extends JsonValue> TypeHandler<P> getHandler(
Class<P> type) {
ElementGetter<P> getter = (element, property, defaultValue) -> {
Serializable value = element.getPropertyRaw(property);
// JsonValue is passed straight through, other primitive
// values are jsonified
return type.cast(JsonCodec.encodeWithoutTypeInfo(value));
};
ElementSetter<P> setter = (element, property, value) -> element
.setPropertyJson(property, value);
return new TypeHandler<P>(setter, getter, null);
}

private static <T> void addHandler(ElementSetter<T> setter,
ElementGetter<T> getter, Class<T> type, T typeDefaultValue) {
typeHandlers.put(type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ public JsonField() {
}
}

@Tag("tag")
private static class JsonArrayField
extends AbstractSinglePropertyField<JsonArrayField, JsonArray> {
public JsonArrayField() {
super("property", Json.createArray(), false);
}
}

@Test
public void jsonField() {
JsonField field = new JsonField();
Expand All @@ -458,6 +466,28 @@ public void jsonField() {
Assert.assertEquals("\"text\"", field.getValue().toJson());
}

@Test
public void jsonArrayField() {
JsonArrayField field = new JsonArrayField();
ValueChangeMonitor<JsonArray> monitor = new ValueChangeMonitor<>(field);

Assert.assertEquals(JsonType.ARRAY, field.getValue().getType());
Assert.assertEquals(0, field.getValue().length());
monitor.assertNoEvent();

field.setValue(
JsonUtils.createArray(Json.create("foo"), Json.create(42)));
monitor.discard();
Assert.assertEquals("[\"foo\",42]",
((JsonArray) field.getElement().getPropertyRaw("property"))
.toJson());

field.getElement().setPropertyJson("property",
JsonUtils.createArray(Json.create(37), Json.create("bar")));
monitor.discard();
Assert.assertEquals("[37,\"bar\"]", field.getValue().toJson());
}

@Test
public void noOwnPublicApi() {
List<Method> newPublicMethods = PublicApiAnalyzer
Expand Down

0 comments on commit 2869622

Please sign in to comment.