prereqs
- SFS2X RC2a
- Groovy 1.7.10
summary
This is a pair of groovy classes which enable a per instance pretending of properties. The init methods check to see if the methods are added to the instance before adding them, to avoid duplicate work. These classes enable the following code and similar to be written in Groovy:
Code: Select all
ISFSObject response = new SFSObject()
//enable it
SFSObjectEnabler.init(response)
//populate response
response.strings = ["hello", "world"]
//still use old put methods if you would like
response.putUtfString("oldstring", "old methods still work")
//following lines are equivalent
//response.putSFSObject("userdata", new SFSObject())
response.userdata = new SFSObject()
//set members of child sfsobjects
response.userdata.userid = 12345
response.userdata.dbid = 54321
//set an array of sfsobjects
//(actually creates an SFSArray with addSFSObject(...) calls)
response.characters = [new SFSObject(), new SFSObject()]
//add one more SFSObject to that array
response.characters << new SFSObject()
//work with the array members
response.characters[0].alias = "Joe"
response.characters[1].alias = "Bob"
response.characters[2].alias = "Ray"
println response.getDump()
This prints:
Code: Select all
(utf_string) oldstring: old methods still work
(sfs_array) characters:
(sfs_object)
(utf_string) alias: Joe
(sfs_object)
(utf_string) alias: Bob
(sfs_object)
(utf_string) alias: Ray
(sfs_object) userdata:
(int) dbid: 54321
(int) userid: 12345
(utf_string_array) strings: [hello, world]
Code for enablers follows in reply