Skip to content
Permalink
Browse files

Property computation in computing context.

  • Loading branch information...
pvlasov committed Sep 7, 2019
1 parent 5db1c06 commit 223c461e7b4663aef202afed12ab9c4ae423928d
@@ -484,16 +484,98 @@ default Context computingContext() {

return new Context() {

@SuppressWarnings("unchecked")
@Override
public Object get(String key) {
Object ret = Context.this.get(key);
return ret instanceof PropertyComputer ? ((PropertyComputer) ret).compute(this, key, Object.class) : ret;
if (ret instanceof PropertyComputer) {
return ((PropertyComputer) ret).compute(this, key, Object.class);
}

if (ret != null) {
return ret;
}

int lastSlash = key.lastIndexOf('/');
if (lastSlash == -1) {
return null;
}

String parentKey = key.substring(0, lastSlash);
Object parentProperty = get(parentKey);
if (parentProperty instanceof PropertyComputer) {
return ((PropertyComputer) parentProperty).compute(this, parentKey, Object.class);
}
String subKey = key.substring(lastSlash + 1);
if (parentProperty instanceof Context) {
return ((Context) parentProperty).get(subKey);
}
if (parentProperty instanceof Map) {
return ((Map<?,?>) parentProperty).get(subKey);
}
if (parentProperty instanceof List) {
try {
int idx = Integer.parseInt(subKey);
if (idx >= 0 && idx < ((List<?>) parentProperty).size()) {
return ((List<?>) parentProperty).get(idx);
}
} catch (NumberFormatException e) {
// NOP
}
}
if (parentProperty instanceof Function) {
return ((Function<String, Object>) parentProperty).apply(subKey);
}

return null;
}

@SuppressWarnings("unchecked")
@Override
public <T> T get(String key, Class<T> type) {
T ret = Context.this.get(key, type);
return ret instanceof PropertyComputer ? ((PropertyComputer) ret).compute(this, key, type) : ret;
Object ret = Context.this.get(key, type);
if (ret instanceof PropertyComputer) {
return ((PropertyComputer) ret).compute(this, key, type);
}

Converter converter = get(Converter.class, DefaultConverter.INSTANCE);

if (ret != null) {
return converter.convert(ret, type);
}

int lastSlash = key.lastIndexOf('/');
if (lastSlash == -1) {
return null;
}

String parentKey = key.substring(0, lastSlash);
Object parentProperty = get(parentKey);
if (parentProperty instanceof PropertyComputer) {
return ((PropertyComputer) parentProperty).compute(this, parentKey, type);
}
String subKey = key.substring(lastSlash + 1);
if (parentProperty instanceof Context) {
return ((Context) parentProperty).get(subKey, type);
}
if (parentProperty instanceof Map) {
return converter.convert(((Map<?,?>) parentProperty).get(subKey), type);
}
if (parentProperty instanceof List) {
try {
int idx = Integer.parseInt(subKey);
if (idx >= 0 && idx < ((List<?>) parentProperty).size()) {
return converter.convert(((List<?>) parentProperty).get(idx), type);
}
} catch (NumberFormatException e) {
// NOP
}
}
if (parentProperty instanceof Function) {
return converter.convert(((Function<String, Object>) parentProperty).apply(subKey), type);
}

return null;
}

@Override
@@ -559,11 +641,7 @@ public Object get(String key) {
return null;
}

String parentKey = key.substring(0, lastSlash);
Object parentProperty = get(parentKey);
if (parentProperty instanceof PropertyComputer) {
parentProperty = ((PropertyComputer) parentProperty).compute(this, parentKey, Object.class);
}
Object parentProperty = get(key.substring(0, lastSlash));
String subKey = key.substring(lastSlash + 1);
if (parentProperty instanceof Context) {
return ((Context) parentProperty).get(subKey);
@@ -68,11 +68,7 @@ public Object get(String key) {
return null;
}

String parentKey = key.substring(0, lastSlash);
Object parentProperty = get(parentKey);
if (parentProperty instanceof PropertyComputer) {
parentProperty = ((PropertyComputer) parentProperty).compute(this, parentKey, Object.class);
}
Object parentProperty = get(key.substring(0, lastSlash));
String subKey = key.substring(lastSlash + 1);
if (parentProperty instanceof Context) {
return ((Context) parentProperty).get(subKey);
@@ -219,17 +219,16 @@ public void testJSONObjectAsMap() {

@Test
public void testJavaExpressionPropertyComputer() {
MutableContext context = Context.singleton("name", "World").fork();
context.put("eval", new JavaExpressionPropertyComputer());
assertEquals("Hello World", context.get("eval/\"Hello \"+context.get(\"name\")"));
Context context = Context.singleton("name", "World").compose(Context.singleton("eval", new JavaExpressionPropertyComputer()));
assertEquals("Hello World", context.computingContext().get("eval/\"Hello \"+context.get(\"name\")"));
}

@Test
public void testJavaExpressionPropertyComputerInSimpleMutableContext() {
MutableContext context = new SimpleMutableContext();
context.put("eval", new JavaExpressionPropertyComputer());
context.put("name", "World");
assertEquals("Hello World", context.get("eval/\"Hello \"+context.get(\"name\")"));
}
assertEquals("Hello World", context.computingContext().get("eval/\"Hello \"+context.get(\"name\")"));
}

}

0 comments on commit 223c461

Please sign in to comment.
You can’t perform that action at this time.