apache-flexactionscriptamfpyamf

Flex: AMF and Enum Singletons – can they play well together?


I'm using Python+PyAMF to talk back and forth with Flex clients, but I've run into a problem with the psudo-Enum-Singletons I'm using:

class Type {
    public static const EMPTY:Type = new Type("empty");
    public static const FULL:Type = new Type("full");
    ...

}

When I'm using locally created instances, everything is peachy:

if (someInstance.type == Type.EMPTY) { /* do things */ }

But, if 'someInstance' has come from the Python code, it's instance of 'type' obviously won't be either Type.EMPTY or Type.FULL.

So, what's the best way to make my code work?

Is there some way I can control AMF's deserialization, so when it loads a remote Type, the correct transformation will be called? Or should I just bite the bullet and compare Types using something other than ==? Or could I somehow trick the == type cohesion into doing what I want?

Edit: Alternately, does Flex's remoting suite provide any hooks which run after an instance has been deserialized, so I could perform a conversion then?


Solution

  • Random thought: Maybe you could create a member function on Type that will return the canonical version that matches it?

    Something like:

    class Type {
      public static const EMPTY:Type = new Type("empty");
      public static const FULL:Type = new Type("full");
      ...
    
      // I'm assuming this is where that string passed
      // in to the constructor goes, and that it's unique.
      private var _typeName:String;
    
      public function get canonical():Type {
        switch(this._typeName) {
          case "empty": return EMPTY;
          case "full": return FULL;
          /*...*/
        }
      }
    }
    

    As long as you know which values come from python you would just convert them initially:

    var fromPython:Type = /*...*/
    var t:Type = fromPython.canonical;
    

    then use t after that.

    If you can't tell when things come from python and when they're from AS3 then it would get pretty messy, but if you have an isolation layer between the AS and python code you could just make sure you do the conversion there.

    It's not as clean as if you could control the deserialization, but as long as you've got a good isolation layer it should work.