views:

225

answers:

1

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?

+1  A: 

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.

Herms
hhmm… That's not a bad idea. I could even have a getter in Type's clients, something like `function get type():Type { return _type.canonical; }`. Thanks!
David Wolever
Now, you mention that I could convert them internally… Is there some way I can trigger that conversion automatically? Do the `Remoting` tools provide any 'after this instance has been deserailiezd' type of hooks?
David Wolever
I was talking about an isolation layer. If your communication layer to python is isolated (only one class that handles the messages from it) then you can do the conversion there before passing the data off to whatever would be handling it. Kind of hard to say where you'd put it without knowing a lot more detail about your code.
Herms