This is an automated email from the ASF dual-hosted git repository.

heneveld 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 a032a06  support raw access to map/list config keys
a032a06 is described below

commit a032a06b172aec36d2544abaf88572c9498461ff
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed May 5 13:01:45 2021 +0100

    support raw access to map/list config keys
---
 .../core/config/internal/AbstractConfigMapImpl.java    | 16 ++++++++++++++--
 .../config/MapListAndOtherStructuredConfigKeyTest.java | 18 +++++++++++++++++-
 .../apache/brooklyn/core/entity/EntityConfigTest.java  |  6 ++++--
 .../java/org/apache/brooklyn/config/ConfigMap.java     |  8 +++++---
 4 files changed, 40 insertions(+), 8 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
 
b/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
index 5bcc5f6..2f4623f 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/config/internal/AbstractConfigMapImpl.java
@@ -230,9 +230,9 @@ public abstract class AbstractConfigMapImpl<TContainer 
extends BrooklynObject> i
         return (BrooklynObjectInternal) getParent();
     }
 
-    @Override @Deprecated
+    @Override
     public Maybe<Object> getConfigRaw(ConfigKey<?> key, boolean 
includeInherited) {
-        // does not currently respect inheritance modes
+        // NB: does not respect inheritance modes; see methods in 
ConfigMap.ConfigInheritance
         if (ownConfig.containsKey(key)) {
             return Maybe.of(ownConfig.get(key));
         }
@@ -246,6 +246,18 @@ public abstract class AbstractConfigMapImpl<TContainer 
extends BrooklynObject> i
                 return Maybe.of(ownConfig.get(deprecatedKey));
             }
         }
+        if (key instanceof AbstractStructuredConfigKey) {
+            // for structured keys, compute the raw value
+            Object result = ((AbstractStructuredConfigKey) 
key).rawValue(ownConfig);
+            if (result instanceof Iterable) {
+                if (!((Iterable)result).iterator().hasNext()) return 
Maybe.absent("No value for structured collection key "+key);
+            } else if (result instanceof Map) {
+                if (((Map)result).isEmpty()) return Maybe.absent("No value for 
structured map key "+key);
+            } else {
+                LOG.warn("Unsupported structured config key "+key+"; may 
return default empty value if unset");
+            }
+            return Maybe.ofDisallowingNull(result);
+        }
         if (!includeInherited || getParent()==null) return Maybe.absent();
         return 
getParentInternal().config().getInternalConfigMap().getConfigRaw(key, 
includeInherited);
     }
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/config/MapListAndOtherStructuredConfigKeyTest.java
 
b/core/src/test/java/org/apache/brooklyn/core/config/MapListAndOtherStructuredConfigKeyTest.java
index 091b6f4..9b609af 100644
--- 
a/core/src/test/java/org/apache/brooklyn/core/config/MapListAndOtherStructuredConfigKeyTest.java
+++ 
b/core/src/test/java/org/apache/brooklyn/core/config/MapListAndOtherStructuredConfigKeyTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.core.config;
 
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.util.guava.Maybe;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 
@@ -87,7 +89,7 @@ public class MapListAndOtherStructuredConfigKeyTest extends 
BrooklynAppUnitTestS
         assertEquals(entity.getConfig(TestEntity.CONF_MAP_THING), 
ImmutableMap.of("akey","aval","bkey","bval"));
         
assertEquals(entity.getConfig(TestEntity.CONF_MAP_THING.subKey("akey")), 
"aval");
     }
-    
+
     @Test
     public void testMapConfigKeyCanStoreAndRetrieveFutureValsPutByKeys() 
throws Exception {
         final AtomicReference<String> bval = new 
AtomicReference<String>("bval-too-early");
@@ -104,6 +106,20 @@ public class MapListAndOtherStructuredConfigKeyTest 
extends BrooklynAppUnitTestS
     }
 
     @Test
+    public void testMapConfigKeyGetRaw() throws Exception {
+        final AtomicReference<String> bval = new 
AtomicReference<String>("bval-too-early");
+        entity.config().set(TestEntity.CONF_MAP_THING.subKey("akey"), 
DependentConfiguration.whenDone(Callables.returning("aval")));
+        app.start(locs);
+
+        Maybe<Object> rawMM = 
entity.config().getLocalRaw(TestEntity.CONF_MAP_THING);
+        Assert.assertTrue(rawMM.isPresent());
+        Map rawM = (Map) rawMM.get();
+        assertEquals(rawM.size(), 1);
+        assertEquals(rawM.keySet(), ImmutableSet.of("akey"));
+        Asserts.assertInstanceOf(rawM.get("akey"), Task.class);
+    }
+
+    @Test
     public void testMapConfigKeyCanStoreAndRetrieveFutureValsPutAsMap() throws 
Exception {
         final AtomicReference<String> bval = new 
AtomicReference<String>("bval-too-early");
         entity.config().set(TestEntity.CONF_MAP_THING, (Map) MutableMap.of(
diff --git 
a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java 
b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
index 3a52775..761307b 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntityConfigTest.java
@@ -258,7 +258,8 @@ public class EntityConfigTest extends 
BrooklynAppUnitTestSupport {
                 () -> (TestEntity) 
mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
                         .configure( (MapConfigKey) TestEntity.CONF_MAP_THING, 
"{mysub: {sub2: 4}}")));
 
-        
assertTrue(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING).isAbsent());
+        
assertEquals(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING).get(), 
ImmutableMap.of("mysub", ImmutableMap.of("sub2", 4)));
+
         
assertEquals(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING.subKey("mysub")).get(),
 ImmutableMap.of("sub2", 4));        // legacy code
         
Tasks.ForTestingAndLegacyCompatibilityOnly.withLegacyDeepResolutionMode(LegacyDeepResolutionMode.ALLOW_LEGACY,
 () ->
                 assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), 
ImmutableMap.of("mysub", ImmutableMap.of("sub2", 4))));
@@ -280,7 +281,8 @@ public class EntityConfigTest extends 
BrooklynAppUnitTestSupport {
                 () -> (TestEntity) 
mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class)
                         .configure( TestEntity.CONF_MAP_THING.getName(), 
"{mysub: {sub2: 4}}")));
 
-        
assertTrue(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING).isAbsent());
+        
assertEquals(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING).get(), 
ImmutableMap.of("mysub", ImmutableMap.of("sub2", 4)));
+
         
assertEquals(entity.config().getLocalRaw(TestEntity.CONF_MAP_THING.subKey("mysub")).get(),
 ImmutableMap.of("sub2", 4));        // legacy code
         
Tasks.ForTestingAndLegacyCompatibilityOnly.withLegacyDeepResolutionMode(LegacyDeepResolutionMode.ALLOW_LEGACY,
 () ->
                 assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), 
ImmutableMap.of("mysub", ImmutableMap.of("sub2", 4))));
diff --git 
a/utils/common/src/main/java/org/apache/brooklyn/config/ConfigMap.java 
b/utils/common/src/main/java/org/apache/brooklyn/config/ConfigMap.java
index f29b444..f9f755c 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/config/ConfigMap.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/config/ConfigMap.java
@@ -31,10 +31,10 @@ import com.google.common.base.Predicate;
 
 public interface ConfigMap {
     
-    /** @see #getConfig(ConfigKey, Object), with default value as per the key, 
or null */
+    /** returns the config, resolved and inheritance rules applied, with 
default value as per the key, or null */
     public <T> T getConfig(ConfigKey<T> key);
     
-    /** @see #getConfig(ConfigKey, Object), with default value as per the key, 
or null */
+    /** @see #getConfig(ConfigKey), with default value as per the key, or null 
*/
     public <T> T getConfig(HasConfigKey<T> key);
 
     /** returns the value stored against the given key, 
@@ -49,7 +49,7 @@ public interface ConfigMap {
      *         
      * @deprecated since 0.10.0 in favour of {@link ConfigMapWithInheritance} 
methods
      */
-    @Deprecated  // and confirmed no uses
+    @Deprecated  // and confirmed no uses of this method on the interface 
(implementations still use this method)
     public Maybe<Object> getConfigRaw(ConfigKey<?> key, boolean 
includeInherited);
 
     /** returns the value stored against the given key, 
@@ -93,12 +93,14 @@ public interface ConfigMap {
      * if the map is associated with a container or type context where 
reference keys are defined,
      * those keys are included in the result whether or not present in the map 
(unlike {@link #findKeysPresent(Predicate)}) */
     // TODO should be findKeysDeclaredOrPresent - if you want just the 
declared ones, look up the type
+    // TODO should ignore sub element config keys, but for now the caller can 
do that
     public Set<ConfigKey<?>> findKeysDeclared(Predicate<? super ConfigKey<?>> 
filter);
 
     /** as {@link #findKeysDeclared(Predicate)} but restricted to keys 
actually present in the map
      * <p>
      * if there is a container or type context defining reference keys, those 
key definitions will be
      * preferred over any config keys used as keys in this map. */
+    // TODO should include structured config keys if they have a sub element 
config present
     public Set<ConfigKey<?>> findKeysPresent(Predicate<? super ConfigKey<?>> 
filter);
 
     /** returns a read-only map view which has string keys (corresponding to 
the config key names);

Reply via email to