This is an automated email from the ASF dual-hosted git repository.
tbouron pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git
The following commit(s) were added to refs/heads/master by this push:
new 7d61a9d Add support for use of DSL when specifying the pattern for
$brooklyn:formatString()
new f2160cb Merge pull request #1081 from tbouron/fix/dsl-format-string
7d61a9d is described below
commit 7d61a9d7c8e3eb118083bc130c7b7ef62bc5088e
Author: Thomas Bouron
AuthorDate: Mon Jan 6 17:31:21 2020 +
Add support for use of DSL when specifying the pattern for
$brooklyn:formatString()
---
.../spi/dsl/methods/BrooklynDslCommon.java | 14 ++---
.../brooklyn/spi/dsl/DslParseComponentsTest.java | 12 +++
.../core/sensor/DependentConfiguration.java| 24 ++
3 files changed, 35 insertions(+), 15 deletions(-)
diff --git
a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index 518b501..3c398ab 100644
---
a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++
b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -368,10 +368,10 @@ public class BrooklynDslCommon {
* are not yet fully resolved.
*/
@DslAccessible
-public static Object formatString(final String pattern, final
Object...args) {
-if (resolved(args)) {
+public static Object formatString(final Object pattern, final
Object...args) {
+if (resolved(Lists.asList(pattern, args))) {
// if all args are resolved, apply the format string now
-return String.format(pattern, args);
+return String.format(String.valueOf(pattern), args);
} else {
return new DslFormatString(pattern, args);
}
@@ -433,16 +433,16 @@ public class BrooklynDslCommon {
/**
* Deferred execution of String formatting.
*
- * @see DependentConfiguration#formatString(String, Object...)
+ * @see DependentConfiguration#formatString(Object, Object...)
*/
protected static class DslFormatString extends
BrooklynDslDeferredSupplier {
private static final long serialVersionUID = -4849297712650560863L;
-private final String pattern;
+private final Object pattern;
private final Object[] args;
-public DslFormatString(String pattern, Object ...args) {
+public DslFormatString(Object pattern, Object ...args) {
this.pattern = pattern;
this.args = args;
}
@@ -526,7 +526,7 @@ public class BrooklynDslCommon {
@SuppressWarnings({ "serial", "unused" })
@Deprecated
private static class FormatString extends DslFormatString {
-public FormatString(String pattern, Object[] args) {
+public FormatString(Object pattern, Object[] args) {
super(pattern, args);
}
}
diff --git
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslParseComponentsTest.java
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslParseComponentsTest.java
index 1e8ccb8..ddd8a1f 100644
---
a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslParseComponentsTest.java
+++
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslParseComponentsTest.java
@@ -57,6 +57,7 @@ public class DslParseComponentsTest extends AbstractYamlTest {
" brooklyn.config:",
"dest: 1",
"dest2: 1",
+"pattern: '%s-%s'",
" brooklyn.children:",
" - type: "+BasicEntity.class.getName(),
"id: two",
@@ -133,6 +134,17 @@ public class DslParseComponentsTest extends
AbstractYamlTest {
}
@Test
+public void testFormatStringWithDslPatternEvaluation() throws Exception {
+app();
+
+Object y1 =
parseDslExpression("$brooklyn:formatString($brooklyn:config(\"pattern\"),
$brooklyn:config(\"dest\"), $brooklyn:config(\"dest2\"))");
+Assert.assertEquals(y1.toString(),
"$brooklyn:formatString(config(\"pattern\"), config(\"dest\"),
config(\"dest2\"))");
+
+String y2 = Tasks.resolveValue(y1, String.class, ((EntityInternal)
find("one")).getExecutionContext());
+Assert.assertEquals(y2.toString(), "1-1");
+}
+
+@Test
public void testEntityReferenceAndAttributeWhenReady() throws Exception {
app();
find("one").sensors().set(Attributes.ADDRESS, "1");
diff --git
a/core/src/main/java/org/apache/brooklyn/core/sensor/DependentConfiguration.java
b/core/src/main/jav