Skip to content

Commit

Permalink
fromStringToArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Nov 13, 2023
1 parent 550167e commit 66f0471
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 18 additions & 0 deletions haxe/ui/data/DataSourceFactory.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ class DataSourceFactory<T> {
return ds;
}

public function fromStringToArray<T>(data:String):Array<T> {
var array:Array<T> = [];
if (StringTools.startsWith(data, "<")) { // xml
var xml:Xml = Xml.parse(data).firstElement();
for (el in xml.elements()) {
var o:Dynamic = xml2Object(el);
array.push(o);
}
} else if (StringTools.startsWith(data, "[")) { // json array
var json:Array<Dynamic> = Json.parse(StringTools.replace(data, "'", "\""));
for (o in json) {
array.push(o);
}
}

return array;
}

private function xml2Object(el:Xml, addId:Bool = true):Dynamic {
var o = {};

Expand Down
5 changes: 2 additions & 3 deletions haxe/ui/macros/ComponentMacros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -957,10 +957,9 @@ class ComponentMacros {
}

private static function buildDataSourceCode(builder:CodeBuilder, c:ComponentInfo, dsVarName:String, componentVarName:String) {
var ds = new haxe.ui.data.DataSourceFactory<Dynamic>().fromString(c.dataString, haxe.ui.data.ArrayDataSource);
var items = new haxe.ui.data.DataSourceFactory<Dynamic>().fromStringToArray(c.dataString);
builder.add(macro var $dsVarName = new haxe.ui.data.ArrayDataSource<Dynamic>());
for (i in 0...ds.size) {
var item = ds.get(i);
for (item in items) {
var hasExpression:Bool = false;
// lets first find out if any of the items are expressions
for (f in Reflect.fields(item)) {
Expand Down

0 comments on commit 66f0471

Please sign in to comment.