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

gregdove pushed a commit to branch amf_updates
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/amf_updates by this push:
     new ae9a24f  [AMFBinaryData] Added support for IDynamicPropertyWriter, 
added Unit testing to verify implementation (test example is in 
flexUnitTests/network/support/DynamicPropertyWriter.as)
ae9a24f is described below

commit ae9a24fa1bc8a9198af096edc2ac820745b52873
Author: greg-dove <[email protected]>
AuthorDate: Sun Mar 3 20:35:44 2019 +1300

    [AMFBinaryData] Added support for IDynamicPropertyWriter, added Unit 
testing to verify implementation (test example is in 
flexUnitTests/network/support/DynamicPropertyWriter.as)
---
 .../royale/net/remoting/amf/AMFBinaryData.as       | 140 +++++++++++++++------
 .../royale/net/utils/IDynamicPropertyOutput.as     |  23 +++-
 .../royale/net/utils/IDynamicPropertyWriter.as     |  20 +++
 .../network/AMFBinaryDataTesterTest.as             |  41 +++++-
 ...ynamicTestClass.as => DynamicPropertyWriter.as} |  36 ++++--
 .../network/support/DynamicTestClass.as            |   9 --
 .../{DynamicTestClass.as => DynamicTestClass2.as}  |  11 +-
 7 files changed, 203 insertions(+), 77 deletions(-)

diff --git 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/remoting/amf/AMFBinaryData.as
 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/remoting/amf/AMFBinaryData.as
index c703935..720ad2b 100644
--- 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/remoting/amf/AMFBinaryData.as
+++ 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/remoting/amf/AMFBinaryData.as
@@ -20,9 +20,14 @@
 package org.apache.royale.net.remoting.amf {
        import org.apache.royale.net.utils.IDataInput;
        import org.apache.royale.net.utils.IDataOutput;
+       import org.apache.royale.net.utils.IDynamicPropertyWriter;
        import org.apache.royale.net.utils.IExternalizable;
        import org.apache.royale.utils.BinaryData;
        
+       COMPILE::SWF{
+               import flash.net.ObjectEncoding;
+       }
+       
        /**
         *  A version of BinaryData specific to AMF.
         *
@@ -35,11 +40,44 @@ package org.apache.royale.net.remoting.amf {
         *  @royalesuppresspublicvarwarning
         */
        public class AMFBinaryData extends BinaryData implements IDataInput, 
IDataOutput {
-               
//--------------------------------------------------------------------------
-               //
-               // Class Constants
-               //
-               
//--------------------------------------------------------------------------
+
+               COMPILE::JS
+               private static var _propertyWriter:IDynamicPropertyWriter;
+               
+               /**
+                * Allows greater control over the serialization of dynamic 
properties of dynamic objects.
+                * When this property is set to null, the default value, 
dynamic properties are serialized using native code,
+                * which writes all dynamic properties excluding those whose 
value is a function.
+                * This value is called only for properties of a dynamic object 
(objects declared within a dynamic class) or
+                * for objects declared using the new operator.
+                * You can use this property to exclude properties of dynamic 
objects from serialization; to write values
+                * to properties of dynamic objects; or to create new 
properties for dynamic objects. To do so,
+                * set this property to an object that implements the 
IDynamicPropertyWriter interface. For more information,
+                * see the IDynamicPropertyWriter interface.
+                */
+               public static function get 
dynamicPropertyWriter():IDynamicPropertyWriter {
+                       COMPILE::JS{
+                               return _propertyWriter;
+                       }
+                       COMPILE::SWF{
+                               var value:flash.net.IDynamicPropertyWriter =  
ObjectEncoding.dynamicPropertyWriter;
+                               var 
outValue:org.apache.royale.net.utils.IDynamicPropertyWriter = value as 
org.apache.royale.net.utils.IDynamicPropertyWriter;
+                               if (value && !outValue) {
+                                       outValue = new 
ExternallySetDynamicPropertyWriter(value);
+                               }
+                               return outValue;
+                       }
+               }
+               
+               public static function set 
dynamicPropertyWriter(value:IDynamicPropertyWriter):void {
+                       COMPILE::JS{
+                               _propertyWriter = value;
+                       }
+                       COMPILE::SWF{
+                               ObjectEncoding.dynamicPropertyWriter = value;
+                       }
+               }
+               
                
                
                public function AMFBinaryData(bytes:Object = null) {
@@ -58,13 +96,14 @@ package org.apache.royale.net.remoting.amf {
                
                
                COMPILE::JS
-               private var serializationContext:SerializationContext;
+               private var _serializationContext:SerializationContext;
                
                COMPILE::JS
                public function writeObject(v:*):void {
-                       if (!serializationContext) serializationContext = new 
SerializationContext(this);
-                       _position = serializationContext.writeObjectExternal(v, 
_position, mergeInToArrayBuffer);
-                       var err:Error = serializationContext.getError();
+                       if (!_serializationContext) _serializationContext = new 
SerializationContext(this);
+                       _serializationContext.dynamicPropertyWriter = 
_propertyWriter;
+                       _position = 
_serializationContext.writeObjectExternal(v, _position, mergeInToArrayBuffer);
+                       var err:Error = _serializationContext.getError();
                        if (err) {
                                throw new Error(err.message);
                        }
@@ -72,9 +111,9 @@ package org.apache.royale.net.remoting.amf {
                
                COMPILE::JS
                public function readObject():* {
-                       if (!serializationContext) serializationContext = new 
SerializationContext(this);
-                       var value:* = serializationContext.readObjectExternal();
-                       var err:Error = serializationContext.getError();
+                       if (!_serializationContext) _serializationContext = new 
SerializationContext(this);
+                       var value:* = 
_serializationContext.readObjectExternal();
+                       var err:Error = _serializationContext.getError();
                        if (err) {
                                throw new Error(err.message);
                        }
@@ -93,7 +132,27 @@ package org.apache.royale.net.remoting.amf {
        }
 }
 
+
+COMPILE::SWF
+class ExternallySetDynamicPropertyWriter implements IDynamicPropertyWriter{
+       
+       import flash.net.IDynamicPropertyOutput;
+       
+       private var _externalImplementation:flash.net.IDynamicPropertyWriter;
+       
+       public function 
ExternallySetDynamicPropertyWriter(original:flash.net.IDynamicPropertyWriter) {
+               _externalImplementation = original;
+       }
+       
+       public function writeDynamicProperties(obj:Object, 
output:flash.net.IDynamicPropertyOutput):void {
+               _externalImplementation.writeDynamicProperties(obj, output);
+       }
+       
+}
+
 import org.apache.royale.net.remoting.amf.AMFBinaryData;
+import org.apache.royale.net.utils.IDynamicPropertyOutput;
+import org.apache.royale.net.utils.IDynamicPropertyWriter;
 import org.apache.royale.reflection.getAliasByClass;
 import org.apache.royale.reflection.getClassByAlias;
 import org.apache.royale.reflection.getDynamicFields;
@@ -107,7 +166,7 @@ import org.apache.royale.net.utils.IDataOutput;
 
 
 COMPILE::JS
-class SerializationContext extends BinaryData  implements IDataInput, 
IDataOutput {
+class SerializationContext extends BinaryData  implements IDataInput, 
IDataOutput, IDynamicPropertyOutput {
        import goog.DEBUG;
        
        
@@ -141,6 +200,7 @@ class SerializationContext extends BinaryData  implements 
IDataInput, IDataOutpu
        private static const EMPTY_STRING:String = "";
        
        private var owner:AMFBinaryData;
+       public var dynamicPropertyWriter:IDynamicPropertyWriter;
        private var writeBuffer:Array;
        
        private var objects:Array ;
@@ -642,6 +702,17 @@ class SerializationContext extends BinaryData  implements 
IDataInput, IDataOutpu
                }
        }
        
+       /**
+        * This serialization context is passed as the 2nd parameter to an 
IDynamicPropertyWriter
+        * implementation's writeDynamicProperties method call. The resolved 
properties are written here
+        * @param name property name
+        * @param value property value
+        */
+       public function writeDynamicProperty(name:String, value:*):void {
+               this.writeStringWithoutType(name);
+               this.writeObject(value);
+       }
+       
        
        private function writeTypedObject(v:Object, localTraits:Traits):void {
                var encodedName:String = localTraits.alias && 
localTraits.alias.length ? localTraits.alias : ']:' + localTraits.qName + ":[";
@@ -678,18 +749,24 @@ class SerializationContext extends BinaryData  implements 
IDataInput, IDataOutpu
                        }
                        
                        if (localTraits.isDynamic) {
-                               var dynFields:Array = getDynamicFields(v);
-                               i = 0;
-                               l = dynFields.length;
-                               for (; i < l; i++) {
-                                       var val:* = v[dynFields[i]];
-                                       if (val is Function) {
-                                               //skip this value, don't even 
write it out as undefined (match flash)
-                                               continue;
+                               if (dynamicPropertyWriter != null) {
+                                       
dynamicPropertyWriter.writeDynamicProperties(v, this);
+                               } else {
+                                       //default implementation
+                                       var dynFields:Array = 
getDynamicFields(v);
+                                       i = 0;
+                                       l = dynFields.length;
+                                       for (; i < l; i++) {
+                                               val = v[dynFields[i]];
+                                               if (val is Function) {
+                                                       //skip this name-value 
pair, don't even write it out as undefined (match flash)
+                                                       continue;
+                                               }
+                                               
this.writeStringWithoutType(dynFields[i]);
+                                               this.writeObject(val);
                                        }
-                                       
this.writeStringWithoutType(dynFields[i]);
-                                       this.writeObject(val);
                                }
+                               //end of dynamic properties marker
                                this.writeStringWithoutType(EMPTY_STRING);
                        }
                }
@@ -1108,23 +1185,6 @@ COMPILE::JS
 class Traits {
        import goog.DEBUG;
        
-/*     public static function createInstanceVariableGetter(fromFunc:Function, 
type:String):Function{
-               if (type == "*") {
-                       return function(inst:Object):* {
-                               return fromFunc(inst, fromFunc);
-                       }
-               } else {
-                       return function(inst:Object):* {
-                               fromFunc(inst);
-                       }
-               }
-       }
-       
-       public static function createInstanceVariableSetter(toFunc:Function, 
type:String):Function{
-               return function(inst:Object, value:*):void {
-                       toFunc(inst, value);
-               }
-       }*/
        
        public static function 
createInstanceVariableGetterSetter(reflectionFunction:Function, 
type:String):Object{
                var ret:Object = {
diff --git 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyOutput.as
 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyOutput.as
index 349df35..b70e072 100644
--- 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyOutput.as
+++ 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyOutput.as
@@ -18,7 +18,12 @@
 
////////////////////////////////////////////////////////////////////////////////
 package org.apache.royale.net.utils
 {
-
+       COMPILE::SWF{
+               import flash.net.IDynamicPropertyOutput;
+       }
+       
+       
+       COMPILE::JS
     /**
      * This interface controls the serialization of dynamic properties of 
dynamic objects.
      * You use this interface with the IDynamicPropertyWriter interface to 
create an implementation for
@@ -39,5 +44,21 @@ package org.apache.royale.net.utils
                 */
                function writeDynamicProperty(name:String, value:*):void
     }
+       
+       
+       COMPILE::SWF
+       /**
+        * This interface controls the serialization of dynamic properties of 
dynamic objects.
+        * You use this interface with the IDynamicPropertyWriter interface to 
create an implementation for
+        * configuring serialization of dynamic objects.
+        *
+        * This interface is a placeholder for SWF, and cannot be used to with 
native serialization, because the native
+        * class (e.g. ByteArray) that supports serialization does not 
implement this interface (and that cannot change)
+        */
+       public interface IDynamicPropertyOutput extends 
flash.net.IDynamicPropertyOutput
+       {
+       
+
+       }
 
 }
diff --git 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyWriter.as
 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyWriter.as
index 68830b4..1f3cb71 100644
--- 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyWriter.as
+++ 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/utils/IDynamicPropertyWriter.as
@@ -19,6 +19,13 @@
 package org.apache.royale.net.utils
 {
 
+       COMPILE::SWF{
+               import flash.net.IDynamicPropertyWriter;
+               import flash.net.IDynamicPropertyOutput;
+       }
+       
+       
+       COMPILE::JS
     /**
      * This interface controls the serialization of dynamic properties of 
dynamic objects.
         * This interface is used with the IDynamicPropertyOutput interface to 
control the serialization
@@ -37,5 +44,18 @@ package org.apache.royale.net.utils
                 */
                function writeDynamicProperties(obj:Object, 
output:IDynamicPropertyOutput):void
     }
+       
+       
+       COMPILE::SWF
+       /**
+        * This interface controls the serialization of dynamic properties of 
dynamic objects.
+        * This interface is used with the IDynamicPropertyOutput interface to 
control the serialization
+        * of dynamic properties of dynamic objects. To use this interface, 
assign an object that implements
+        * the IDynamicPropertyWriter interface to the 
AMFObjectEncoding.dynamicPropertyWriter property.
+        */
+       public interface IDynamicPropertyWriter extends 
flash.net.IDynamicPropertyWriter
+       {
+       
+       }
 
 }
diff --git 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/AMFBinaryDataTesterTest.as
 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/AMFBinaryDataTesterTest.as
index 3bb5e25..d7cf945 100644
--- 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/AMFBinaryDataTesterTest.as
+++ 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/AMFBinaryDataTesterTest.as
@@ -20,6 +20,8 @@ package flexUnitTests.network
 {
        
        
+       import flexUnitTests.network.support.DynamicPropertyWriter;
+       import flexUnitTests.network.support.DynamicTestClass2;
        import flexUnitTests.network.support.TestClass1;
        import flexUnitTests.network.support.TestClass2;
        import flexUnitTests.network.support.TestClass3;
@@ -28,7 +30,6 @@ package flexUnitTests.network
        
        import flexunit.framework.Assert;
     import org.apache.royale.net.remoting.amf.AMFBinaryData;
-       
        import org.apache.royale.reflection.*;
 
 
@@ -265,10 +266,6 @@ package flexUnitTests.network
                        
                        Assert.assertTrue("post-write bytes did not match 
expected data", bytesMatchExpectedData(ba,[0]));
                        instance = ba.readObject();
-                       COMPILE::SWF{
-                               import flash.external.ExternalInterface;
-                               ExternalInterface.call('console.warn',instance, 
instance === null, instance === undefined)
-                       }
                        
                        Assert.assertTrue("post-write read did not match 
expected result", instance === null);
                        
@@ -403,6 +400,40 @@ package flexUnitTests.network
                        Assert.assertTrue("post-write read did not match 
expected data", test3Read.content[0] == test3.content[0]);
                        
                }
+               
+               
+               [Test]
+               public function testDynamicPropertyWriter():void{
+                       var ba:AMFBinaryData = new AMFBinaryData();
+                       var instance:DynamicTestClass2 = new 
DynamicTestClass2();
+                       instance['_underscore'] = 'pseudo - private value';
+                       instance['raining'] = 'cats and dogs';
+                       
+                       ba.writeObject(instance);
+                       
+                       Assert.assertEquals("post-write length was not 
correct", ba.length, 84);
+                       Assert.assertEquals("post-write position was not 
correct", ba.position, 84);
+                       
+                       //in this case the order of encoding the dynamic fields 
is not defined. So we need to account for the valid serialization options of 
either output sequence of the two fields
+                       var raining_then_underscore:Array = [10, 27, 1, 39, 
115, 101, 97, 108, 101, 100, 73, 110, 115, 116, 97, 110, 99, 101, 80, 114, 111, 
112, 49, 2, 15, 114, 97, 105, 110, 105, 110, 103, 6, 27, 99, 97, 116, 115, 32, 
97, 110, 100, 32, 100, 111, 103, 115, 23, 95, 117, 110, 100, 101, 114, 115, 99, 
111, 114, 101, 6, 45, 112, 115, 101, 117, 100, 111, 32, 45, 32, 112, 114, 105, 
118, 97, 116, 101, 32, 118, 97, 108, 117, 101, 1];
+                       var underscore_then_raining:Array = [10, 27, 1, 39, 
115, 101, 97, 108, 101, 100, 73, 110, 115, 116, 97, 110, 99, 101, 80, 114, 111, 
112, 49, 2, 23, 95, 117, 110, 100, 101, 114, 115, 99, 111, 114, 101, 6, 45, 
112, 115, 101, 117, 100, 111, 32, 45, 32, 112, 114, 105, 118, 97, 116, 101, 32, 
118, 97, 108, 117, 101, 15, 114, 97, 105, 110, 105, 110, 103, 6, 27, 99, 97, 
116, 115, 32, 97, 110, 100, 32, 100, 111, 103, 115, 1];
+                       
+                       ba.position=0;
+                       Assert.assertTrue("post-write bytes did not match 
expected data", bytesMatchExpectedData(ba, raining_then_underscore) || 
bytesMatchExpectedData(ba, underscore_then_raining));
+                       
+                       //now test the same instance with an 
IDynamicPropertyWriter that ignores the underscored field, only outputting the 
'raining' field
+                       ba.length = 0;
+                       AMFBinaryData.dynamicPropertyWriter = new 
DynamicPropertyWriter();
+                       ba.writeObject(instance);
+                       Assert.assertEquals("post-write length was not 
correct", ba.length, 48);
+                       Assert.assertEquals("post-write position was not 
correct", ba.position, 48);
+                       
+                       Assert.assertTrue("post-write bytes did not match 
expected data", bytesMatchExpectedData(ba,[10, 27, 1, 39, 115, 101, 97, 108, 
101, 100, 73, 110, 115, 116, 97, 110, 99, 101, 80, 114, 111, 112, 49, 2, 15, 
114, 97, 105, 110, 105, 110, 103, 6, 27, 99, 97, 116, 115, 32, 97, 110, 100, 
32, 100, 111, 103, 115, 1]));
+                       
+                       //remove the custom dynamicPropertyWriter
+                       AMFBinaryData.dynamicPropertyWriter = null;
+                       
+               }
 
        
                
diff --git 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicPropertyWriter.as
similarity index 59%
copy from 
manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
copy to 
manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicPropertyWriter.as
index 2032bc0..21b9d26 100644
--- 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
+++ 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicPropertyWriter.as
@@ -18,26 +18,38 @@
 
////////////////////////////////////////////////////////////////////////////////
 package flexUnitTests.network.support
 {
+       import org.apache.royale.net.utils.IDynamicPropertyWriter;
+       import org.apache.royale.reflection.*;
+       COMPILE::JS{
+               import org.apache.royale.net.utils.IDynamicPropertyOutput;
+       }
+       
+       COMPILE::SWF{
+               import flash.net.IDynamicPropertyOutput;
+       }
+       
 
-
-
-       dynamic public class DynamicTestClass
+       public class DynamicPropertyWriter implements IDynamicPropertyWriter
        {
                //Note: do not change this test class unless you change the 
related tests to
                //support any changes that might appear when testing with it
                
-
-               public var sealedInstanceProp1:Boolean;
                
+               public function DynamicPropertyWriter() {
+                       // constructor code
+               }
+               
+               public var excludeBeginningUnderscores:Boolean = true;
                
-               /*private var _sealedInstanceAccessor1:String;
-               public function get sealedInstanceAccessor1():String{
-                       return _sealedInstanceAccessor1;
+               public function writeDynamicProperties(obj:Object, 
output:IDynamicPropertyOutput):void {
+                       var dynamicProperties:Array = getDynamicFields(obj);
+                       while (dynamicProperties.length) {
+                               var prop:String = dynamicProperties.shift();
+                               if (excludeBeginningUnderscores && 
(prop.charAt(0) == '_')) continue;
+                               output.writeDynamicProperty(prop,obj[prop]);
+                       }
                }
                
-               public function set sealedInstanceAccessor1(value:String):void{
-                       _sealedInstanceAccessor1 = value;
-               }*/
-
        }
+       
 }
diff --git 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
index 2032bc0..9c29fe8 100644
--- 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
+++ 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
@@ -29,15 +29,6 @@ package flexUnitTests.network.support
 
                public var sealedInstanceProp1:Boolean;
                
-               
-               /*private var _sealedInstanceAccessor1:String;
-               public function get sealedInstanceAccessor1():String{
-                       return _sealedInstanceAccessor1;
-               }
-               
-               public function set sealedInstanceAccessor1(value:String):void{
-                       _sealedInstanceAccessor1 = value;
-               }*/
 
        }
 }
diff --git 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass2.as
similarity index 80%
copy from 
manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
copy to 
manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass2.as
index 2032bc0..fd3599e 100644
--- 
a/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass.as
+++ 
b/manualtests/UnitTests/src/main/royale/flexUnitTests/network/support/DynamicTestClass2.as
@@ -21,7 +21,7 @@ package flexUnitTests.network.support
 
 
 
-       dynamic public class DynamicTestClass
+       dynamic public class DynamicTestClass2
        {
                //Note: do not change this test class unless you change the 
related tests to
                //support any changes that might appear when testing with it
@@ -29,15 +29,6 @@ package flexUnitTests.network.support
 
                public var sealedInstanceProp1:Boolean;
                
-               
-               /*private var _sealedInstanceAccessor1:String;
-               public function get sealedInstanceAccessor1():String{
-                       return _sealedInstanceAccessor1;
-               }
-               
-               public function set sealedInstanceAccessor1(value:String):void{
-                       _sealedInstanceAccessor1 = value;
-               }*/
 
        }
 }

Reply via email to