Skip to content

Commit

Permalink
Property computation in computing context.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlasov committed Sep 7, 2019
1 parent 5db1c06 commit 223c461
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
94 changes: 86 additions & 8 deletions common/src/org/nasdanika/common/Context.java
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions common/src/org/nasdanika/common/SimpleMutableContext.java
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions tests/src/org/nasdanika/core/tests/TestCommon.java
Expand Up @@ -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.