Skip to content

Commit 069f95b

Browse files
committed
resolve deferred values at the right time in yoml (ie as late as possible)
1 parent fc8e46e commit 069f95b

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/yoml/BrooklynDslInYomlStringPlanTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222
import org.apache.brooklyn.api.typereg.BrooklynTypeRegistry.RegisteredTypeKind;
2323
import org.apache.brooklyn.api.typereg.RegisteredType;
2424
import org.apache.brooklyn.camp.brooklyn.AbstractYamlTest;
25+
import org.apache.brooklyn.camp.yoml.types.YomlInitializers;
2526
import org.apache.brooklyn.core.config.ConfigKeys;
27+
import org.apache.brooklyn.core.entity.EntityAsserts;
28+
import org.apache.brooklyn.core.entity.EntityInternal;
2629
import org.apache.brooklyn.core.sensor.Sensors;
2730
import org.apache.brooklyn.core.typereg.BasicBrooklynTypeRegistry;
31+
import org.apache.brooklyn.test.Asserts;
32+
import org.apache.brooklyn.util.guava.Maybe;
33+
import org.apache.brooklyn.util.time.Duration;
34+
import org.apache.brooklyn.util.time.Time;
2835
import org.apache.brooklyn.util.yoml.annotations.YomlAllFieldsTopLevel;
2936
import org.slf4j.Logger;
3037
import org.slf4j.LoggerFactory;
3138
import org.testng.Assert;
3239
import org.testng.annotations.Test;
3340

3441
import com.google.api.client.repackaged.com.google.common.base.Joiner;
42+
import com.google.common.base.Supplier;
3543
import com.google.common.collect.Iterables;
3644

3745
public class BrooklynDslInYomlStringPlanTest extends AbstractYamlTest {
@@ -73,14 +81,43 @@ public void testYomlParserRespectsDsl() throws Exception {
7381
"- type: org.apache.brooklyn.core.test.entity.TestEntity",
7482
" brooklyn.config:",
7583
" test.obj:",
84+
// with this, the yoml is resolved at retrieval time
7685
" $brooklyn:object-yoml: item-w-dsl");
7786

7887
Entity app = createStartWaitAndLogApplication(yaml);
7988
Entity entity = Iterables.getOnlyElement( app.getChildren() );
8089

8190
entity.sensors().set(Sensors.newStringSensor("test.sensor"), "bob");
91+
Maybe<Object> raw = ((EntityInternal)entity).config().getRaw(ConfigKeys.newConfigKey(Object.class, "test.obj"));
92+
Asserts.assertPresent(raw);
93+
Asserts.assertInstanceOf(raw.get(), Supplier.class);
8294
Object obj = entity.config().get(ConfigKeys.newConfigKey(Object.class, "test.obj"));
8395
Assert.assertEquals(((ItemA)obj).name, "bob");
8496
}
8597

98+
@Test
99+
public void testYomlDefersDslEvaluationForConfig() throws Exception {
100+
add(SAMPLE_TYPE_BASE);
101+
add(SAMPLE_TYPE_TEST);
102+
YomlInitializers.install(mgmt());
103+
104+
String yaml = Joiner.on("\n").join(
105+
"services:",
106+
"- type: org.apache.brooklyn.core.test.entity.TestEntity",
107+
" brooklyn.initializers:",
108+
" a-sensor:",
109+
" type: static-sensor",
110+
" value: '$brooklyn:self().attributeWhenReady(\"test.sensor\")'",
111+
" period: 100ms");
112+
113+
Entity app = createStartWaitAndLogApplication(yaml);
114+
Entity entity = Iterables.getOnlyElement( app.getChildren() );
115+
116+
entity.sensors().set(Sensors.newStringSensor("test.sensor"), "bob");
117+
// EntityAsserts.assertAttributeEqualsEventually(entity, attribute, expected);
118+
System.out.println(entity.getAttribute(Sensors.newStringSensor("a-sensor")));
119+
Time.sleep(Duration.ONE_SECOND);
120+
System.out.println(entity.getAttribute(Sensors.newStringSensor("a-sensor")));
121+
}
122+
86123
}

utils/common/src/main/java/org/apache/brooklyn/util/yoml/serializers/ConfigInMapUnderConfigSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ protected boolean setKeyValueForJavaObjectOnRead(String key, Object value, Strin
7878

7979
Object v2;
8080
try {
81-
v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, optionalType) );
81+
if (isDeferredValue(value)) v2 = value;
82+
else v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, optionalType) );
8283
} catch (Exception e) {
8384
// for config we try with the optional type, but don't insist
8485
Exceptions.propagateIfFatal(e);

utils/common/src/main/java/org/apache/brooklyn/util/yoml/serializers/FieldsInMapUnderFields.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ protected boolean setKeyValueForJavaObjectOnRead(String key, Object value, Strin
7272
String fieldType = getFieldTypeName(ff, optionalTypeConstraint);
7373
Object v2 = converter.read( ((YomlContextForRead)context).subpath("/"+key, value, fieldType) );
7474

75+
if (isDeferredValue(v2)) {
76+
Maybe<?> coerced = config.getCoercer().tryCoerce(v2, ff.getType());
77+
if (coerced.isAbsent()) {
78+
// couldn't coerce or resolve, and this is needed for fields of course
79+
throw new YomlException("Cannot interpret or coerce '"+v2+"' as "+fieldType+" for field "+ff.getName(),
80+
Maybe.getException(coerced));
81+
}
82+
v2 = coerced.get();
83+
}
84+
7585
ff.setAccessible(true);
7686
ff.set(getJavaObject(), v2);
7787
return true;

0 commit comments

Comments
 (0)