What's a good way of deserializing data into mock objects? - apache-flex

I'm writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I'd prefer not to generate the data in code like this:
var mockData = new Array();
mockData.push(new Foo(1, "abc", "xyz"));
mockData.push(new Foo(2, "def", "xyz"));
...
Rather I'd like to store the data in a file in some format that it can be easily serialized into my strongly-typed value objects (i.e. Foo above). Ideally I'd like to create the data in a self-describing format (i.e. what data type each field is, what class it represents, etc)
Does this make sense? Any suggestions?

I would highly recommend the asx3m library. It easily allows serialization to a very readable XML format like this for an object of class Foo:
<com.example.Foo>
<myVar>Something</myVar>
<myArrList>
<string>one</string>
<string>two</string>
</myArrList>
</com.example.Foo>
The code to de-serialize looks like this:
Asx3mer.instance.fromXML(someXMLObj)
The project site has some good examples and it's not too hard to get this off the ground.

Write a method to serialize an "inflated" version of your object. Put the output of that into a file and load it up as part of your test setup. When you want to edit the values, simply edit the xml file. I dont know if this is possible in flex but I will usually include these files as a resource in my test library so that I do not need to copy the file to any specific location for a test run.

Related

Executing a method which is named via a config file

In short: I have a method name provided via a JSON configuration file. I'd like to call a method using this provided name. The method (with a matching name) will exist in the backend. What's the best way of going about this?
I am not quite sure what I should be searching for as an example.
To detail: I am working with a legacy application, hence the VB.NET. I am building a single PDF file from multiple PDF sources. Most of these are as is, I simply read the configuration and grab the relevant files and the job is done. However some require processing, I'd like the configuration file to pass in a method name to be called that will perform extra processing on the PDF, whatever that may be.
As there can be a lot of PDF files that can vary, I cannot simply use a property such as "PostProcessing: true".
Any ideas?
You could use reflection to reflect method names back and check them against the name passed from the property in the config file.
Like so
Type magicType = Type.GetType("MagicClass");
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
That would work.. but to be honest, I'd go with a case statement as you'll be hardcoding the method names anyway (because they are code), and it'll be strongly typed (less chance of typos and errors).

RazorEngine output HTML

I have a little piece of code, which gets data from a DB, and I want to parse it into a .cshtml Razor-View-Template. therefor i create a variable of a new TemplateService and execute .parse() on that. Now I want to output the file to the user. What is the best performing function to accomplish this task? I have an MVC application. And as the text above already describes, this is a little template system.
Edit:
For example:
I have a folder templates/default-tpl/, which contains a file index.cshtml
This file contains some Razor-related variables (for example #ViewData["Title"]
Now i have a Controller, which calls the RazorEngine TemplateService's Parse() function with the corresponding ViewData.
Now I have the HTML ready and parsed
So the question is, from this point, how do i output the website the most performant way? Or is there even a complete different approach possible?

extracting array of objects from xml response to js

so I'm extracting variables from my xml response and trying to reformat the objects but I want to do that the most efficient way possible. so I want to load the xml array of same objects into a js array that I can cycle thru and output the new format. I found a reference to type="nodeset" when extracting the XPath but i could not find a reference to it on the documentation.
what is the best way to load the full xml objects into a js variable and cycle thru the objects and output the new format
Thanks for any help you can give me on this.
Best way to accomplish this is with the XMLToJSON policy, a JavaScript callout in which you can mediate your payload, and then transform back with JSONToXML if you need it.
For an XML array that doesn't need filtering, you can use XPATH with type="nodeset", just as you described. This allows you to pull a node and all child nodes in a particular XPATH. As I'm sure you noticed, you can't do this by just extracting as type="string". Just know that you will need to convert the extracted variable to string before you can use the XML nodes like you do every other string. You can then do JSON.parse to take the string and manipulate the object like an array. The string conversion is as simple as calling a JS callout with the following code (if someone else has a better way, I'm all ears):
var extractedNodeSet = context.getVariable("extractedNodeSet");
var extractedNodeSetString = String(extractedNodeSet);
For an XML array that needs filtering/manipulation, I recommend to use XSLT along with the trusted <xsl:for-each select=...> element. This will let you set conditions on the XML array nodes, manipulate tags/data, and extract the data, all in one step. The only concern is that this isn't a JS array, so if you absolutely must have a JS array, then you'll need to then do an XMLtoJSON and work with the data from there.

Generating dictionary from a directory (for Lucene autocomplete)

Using Lucene 4.9 (Java), I've been looking for a way to implement an autocomplete/suggest feature. The goal is to use several of the fields data used in my indexed documents as the source of the dictionary. What is the best practice or suggested way of generating a dictionary based on this?
I tried LuceneDirectory, but the issue is that it only accepts one field, shown below:
LuceneDictionary ld = new LuceneDictionary(indexReader, "fieldname");
What i'm looking for is something similar to this, but with a possibility of being able to supply an array of string with fields to populate my dictionary.
My next step was to look at the source of the class of LuceneDirectory, hoping to make my own custom Dictionary class implementing the Lucene directory interface. This however, was outside of my scope, and I was hoping someone else might have already done an implementation of this, or know how I can proceed.
To summarize:
1: How do I create a dictionary from an existing directory, with data from multiple fields(terms)?
2: How do I keep the dictionary updated once it has been created? Should I regenerate it on a regular basis or are there any other best practices for this?
You can add multiple Dictionaries to a SpellChecker, like:
SpellChecker spellchecker = new SpellChecker(spellIndexDirectory);
spellchecker.indexDictionary(new LuceneDictionary(indexReader, "fieldname"));
spellchecker.indexDictionary(new LuceneDictionary(indexReader, "anotherfield"));

How to store object types and create objects using that information only

I would like to store MetaObjects in a list like this:
myList = QList<QMetaObject>();
myList->append(MyClass::staticMetaObject);
myList->append(MyOtherClass::staticMetaObject);
I want to keep track of these object through out the application but I don't wish to allocate them just yet. By adding some information in my classes I will be able to use the MetaObject function "classInfo(int).value()". I use this when I store them in a QListWidget. When a row in the QListWidget is pressed I would like to be able to create an object of that specific kind that is stored in the list.
(Also have to add that all the classes dervies from the same baseclass)
This sample code describes a bit of what I want to do, except in his example, you add the classes as you go along.
http://lists.qt.nokia.com/pipermail/qt-interest/2012-January/037204.html
I read through the manual and when I try things like:
MyBaseClass *test = qobject_cast<MyBaseClass*>myList->at(i).newInstance();
The project compiles but when I try to print the "test" object its null. What am I doing wrong? And is this the best way of doing this?
Was also looking at MetaType, but where would i be able to store, for example a string for the menus if I'm not allowed to create the object? Would this be a nicer solution if I have a static function that returns a string?
Edit:
I now changed so the constructors are Q_INVOKABLE which solved the problem where "test == null".
But what are the downside of this solution? Should I just use a object factory (the old fashion way with a switch case)?

Resources