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

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-apiregions.git


The following commit(s) were added to refs/heads/master by this push:
     new c23ccb3  SLING-10090 : Add default value to property description
c23ccb3 is described below

commit c23ccb327629b6fdf0bc622c4689c658e406cd81
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Feb 12 10:12:05 2021 +0100

    SLING-10090 : Add default value to property description
---
 .../apiregions/api/config/InternalConstants.java   |  2 +
 .../apiregions/api/config/PropertyDescription.java | 51 ++++++++++++++++++----
 .../apiregions/api/config/package-info.java        |  2 +-
 .../api/config/PropertyDescriptionTest.java        | 13 ++++--
 4 files changed, 55 insertions(+), 13 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/InternalConstants.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/InternalConstants.java
index f742eb6..e126581 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/InternalConstants.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/InternalConstants.java
@@ -72,4 +72,6 @@ abstract class InternalConstants {
     static final String KEY_REGION = "region";
 
     static final String KEY_REGION_CACHE = "region-cache";
+
+    static final String KEY_DEFAULT = "default";
 }
diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescription.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescription.java
index 93b9e19..56bb7e1 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescription.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescription.java
@@ -28,6 +28,8 @@ import javax.json.JsonObject;
 import javax.json.JsonObjectBuilder;
 import javax.json.JsonValue;
 
+import org.apache.felix.cm.json.Configurations;
+
 /**
  * Instances of this class represent a single configuration property
  * This class is not thread safe.
@@ -61,6 +63,12 @@ public class PropertyDescription extends DescribableEntity {
        /** Required? */
        private boolean required;
     
+    /** 
+     * The optional default value 
+     * @since 1.2
+     */
+    private Object defaultValue;
+
     /**
      * Create a new description
      */
@@ -87,6 +95,7 @@ public class PropertyDescription extends DescribableEntity {
                this.setExcludes(null);
                this.setOptions(null);
                this.setRegex(null);
+        this.setDefaultValue(null);
     }
 
        /**
@@ -140,6 +149,10 @@ public class PropertyDescription extends DescribableEntity 
{
                                this.setOptions(list);
                        }
                        
this.setRegex(this.getString(InternalConstants.KEY_REGEX));
+            final JsonValue dv = 
this.getAttributes().remove(InternalConstants.KEY_DEFAULT);
+            if ( dv != null ) {
+                this.setDefaultValue(Configurations.convertToObject(dv));
+            }
                } catch (final JsonException | IllegalArgumentException e) {
             throw new IOException(e);
         }
@@ -166,32 +179,34 @@ public class PropertyDescription extends 
DescribableEntity {
                }
            this.setString(objectBuilder, InternalConstants.KEY_VARIABLE, 
this.getVariable());
                
-               if ( this.range != null ) {
-                       objectBuilder.add(InternalConstants.KEY_RANGE, 
this.range.toJSONObject());
+               if ( this.getRange() != null ) {
+                       objectBuilder.add(InternalConstants.KEY_RANGE, 
this.getRange().toJSONObject());
                }
-               if ( this.includes != null && this.includes.length > 0 ) {
+               if ( this.getIncludes() != null && this.getIncludes().length > 
0 ) {
                        final JsonArrayBuilder arrayBuilder = 
Json.createArrayBuilder();
-                       for(final String v : this.includes) {
+                       for(final String v : this.getIncludes()) {
                                arrayBuilder.add(v);
                        }
                        objectBuilder.add(InternalConstants.KEY_INCLUDES, 
arrayBuilder);
                }
-               if ( this.excludes != null && this.excludes.length > 0 ) {
+               if ( this.getExcludes() != null && this.getExcludes().length > 
0 ) {
                        final JsonArrayBuilder arrayBuilder = 
Json.createArrayBuilder();
-                       for(final String v : this.excludes) {
+                       for(final String v : this.getExcludes()) {
                                arrayBuilder.add(v);
                        }
                        objectBuilder.add(InternalConstants.KEY_EXCLUDES, 
arrayBuilder);
                }
-               if ( this.options != null && !this.options.isEmpty()) {
+               if ( this.getOptions() != null && !this.getOptions().isEmpty()) 
{
                        final JsonArrayBuilder arrayBuilder = 
Json.createArrayBuilder();
-            for(final Option o : this.options) {
+            for(final Option o : this.getOptions()) {
                                arrayBuilder.add(o.toJSONObject());
                        }
                        objectBuilder.add(InternalConstants.KEY_OPTIONS, 
arrayBuilder);
                }
                this.setString(objectBuilder, InternalConstants.KEY_REGEX, 
this.getRegex());
-               
+               if ( this.getDefaultValue() != null ) {
+            objectBuilder.add(InternalConstants.KEY_DEFAULT, 
Configurations.convertToJsonValue(this.getDefaultValue()));
+        }
                return objectBuilder;
        }
 
@@ -359,4 +374,22 @@ public class PropertyDescription extends DescribableEntity 
{
        public void setRequired(final boolean flag) {
                this.required = flag;
        }
+
+    /**
+     * Get the optional default value.
+     * @return The default value or {@code null}
+     * @since 1.2
+     */
+    public Object getDefaultValue() {
+        return this.defaultValue;
+    }
+    
+    /**
+     * Set the optional default value.
+     * @param val The default value
+     * @since 1.2
+     */
+    public void setDefaultValue(final Object val) {
+        this.defaultValue = val;
+    }
 }
diff --git 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
index b788a51..6ac6f43 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/apiregions/api/config/package-info.java
@@ -17,7 +17,7 @@
  * under the License.
  */
 
[email protected]("1.1.0")
[email protected]("1.2.0")
 package org.apache.sling.feature.extension.apiregions.api.config;
 
 
diff --git 
a/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescriptionTest.java
 
b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescriptionTest.java
index 5e64e62..4fa0583 100644
--- 
a/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescriptionTest.java
+++ 
b/src/test/java/org/apache/sling/feature/extension/apiregions/api/config/PropertyDescriptionTest.java
@@ -51,6 +51,7 @@ public class PropertyDescriptionTest {
         entity.setRequired(true);
         entity.setVariable("var");
         entity.setType(PropertyType.BYTE);        
+        entity.setDefaultValue("default");
         entity.clear();
         assertTrue(entity.getAttributes().isEmpty());
         assertNull(entity.getDeprecated());
@@ -66,12 +67,14 @@ public class PropertyDescriptionTest {
         assertNull(entity.getVariable());
         assertFalse(entity.isRequired());
         assertEquals(PropertyType.STRING, entity.getType());
+        assertNull(entity.getDefaultValue());
     }
 
     @Test public void testFromJSONObject() throws IOException {
         final Extension ext = new Extension(ExtensionType.JSON, "a", 
ExtensionState.OPTIONAL);
         ext.setJSON("{ \"type\" : \"BYTE\", \"cardinality\": 5, \"required\" : 
true, \"variable\" : \"var\"," +
-        "\"range\" : {}, \"includes\" : [\"in\"], \"excludes\" : [\"ex\"] , 
\"options\": [{}], \"regex\": \".\"}");
+        "\"range\" : {}, \"includes\" : [\"in\"], \"excludes\" : [\"ex\"] , 
\"options\": [{}], \"regex\": \".\"," +
+        "\"default\" : \"def\"}");
 
         final PropertyDescription entity = new PropertyDescription();
         entity.fromJSONObject(ext.getJSONStructure().asJsonObject());
@@ -86,6 +89,7 @@ public class PropertyDescriptionTest {
         assertEquals(1, entity.getOptions().size());
         assertEquals(".", entity.getRegex());
         assertNotNull(entity.getRegexPattern());
+        assertEquals("def", entity.getDefaultValue());
 
         // test defaults and empty values
         ext.setJSON("{ \"variable\" : \"var\", \"regex\": \".\"}");
@@ -114,10 +118,12 @@ public class PropertyDescriptionTest {
         entity.setRequired(true);
         entity.setVariable("var");
         entity.setType(PropertyType.BYTE);
+        entity.setDefaultValue("def");
 
         final Extension ext = new Extension(ExtensionType.JSON, "a", 
ExtensionState.OPTIONAL);
         ext.setJSON("{ \"type\" : \"BYTE\", \"cardinality\": 5, \"required\" : 
true, \"variable\" : \"var\"," +
-            "\"range\" : {}, \"includes\" : [\"in\"], \"excludes\" : [\"ex\"] 
, \"options\": [{}], \"regex\": \".\"}");
+            "\"range\" : {}, \"includes\" : [\"in\"], \"excludes\" : [\"ex\"] 
, \"options\": [{}], \"regex\": \".\"," +
+            "\"default\" : \"def\"}");
 
         assertEquals(ext.getJSONStructure().asJsonObject(), 
entity.toJSONObject());
 
@@ -129,7 +135,8 @@ public class PropertyDescriptionTest {
         entity.setOptions(null);
         entity.setExcludes(null);
         entity.setIncludes(null);
-
+        entity.setDefaultValue(null);
+        
         ext.setJSON("{ \"variable\" : \"var\", \"regex\": \".\"}");
 
         assertEquals(ext.getJSONStructure().asJsonObject(), 
entity.toJSONObject());

Reply via email to