@@ -92,8 +92,13 @@ class Macros {
9292
9393 var loadExprs = [];
9494
95- inline function fullType (tname : String ) {
96- return (moduleName + " ." + Module .makeTypeName (tname )).toComplex ();
95+ var fullTypes = new Map <String , ComplexType >();
96+ function fullType (tname : String ) {
97+ if (fullTypes .exists (tname ))
98+ return fullTypes .get (tname );
99+ var type = (moduleName + " ." + Module .makeTypeName (tname )).toComplex ();
100+ fullTypes .set (tname , type );
101+ return type ;
97102 }
98103
99104 function getData (id : String ) {
@@ -126,8 +131,11 @@ class Macros {
126131 return args ;
127132 }
128133
129- inline function simpleType (t : cdb. Data . ColumnType ): Null <ComplexType >
130- return switch (t ) {
134+ var simpleTypes = new Map <cdb. Data . ColumnType , ComplexType >();
135+ function simpleType (t : cdb. Data . ColumnType ): Null <ComplexType > {
136+ if (simpleTypes .exists (t ))
137+ return simpleTypes .get (t );
138+ var type = switch (t ) {
131139 case TInt | TColor : macro :Int ;
132140 case TFloat : macro :Float ;
133141 case TBool : macro :Bool ;
@@ -141,6 +149,9 @@ class Macros {
141149 case TDynamic : macro :Dynamic ;
142150 default : null ;
143151 };
152+ simpleTypes .set (t , type );
153+ return type ;
154+ }
144155
145156 inline function makeVar (name : String , loadExpr : Expr ): Expr
146157 return {expr : EVars ([{name : name , expr : loadExpr }]), pos : pos };
@@ -225,12 +236,18 @@ class Macros {
225236 var valCol = subCols [0 ];
226237
227238 var val : Array <Dynamic > = colVal ;
228- var fields = [];
239+ var fields : Array < haxe.macro. Expr . Field > = [];
229240 var loads = [];
230241 var vars = [];
242+ var keys : Array <Expr > = [];
243+ var valueType : ComplexType = null ;
244+ var iterator = true ;
245+ var thisVar = prefix + " _ref" ;
246+ var keysVar = prefix + " _keys" ;
231247
232248 var arrVar = prefix + " _" + colName ;
233249 vars .push (makeVar (arrVar , debugPos (macro $rowExpr .$colName , ' field: $arrVar ' )));
250+ vars .push (makeVar (thisVar , macro null ));
234251
235252 for (i => row in val ) {
236253 var sid : String = Reflect .field (row , idCol .name );
@@ -251,11 +268,41 @@ class Macros {
251268 access : [AFinal ]
252269 });
253270 loads .push ({field : sid , expr : macro cast $i {itemVar }});
271+ if (iterator ) {
272+ keys .push (macro $v {sid });
273+ if (valueType == null )
274+ valueType = result .type ;
275+ // comparison relies on memoized types in fullType and simpleType
276+ else if (! Type .enumEq (valueType , result .type ))
277+ iterator = false ;
278+ }
279+ }
280+
281+ vars .push (makeVar (keysVar , {expr : EArrayDecl (keys ), pos : pos }));
282+ if (iterator ) {
283+ var iterElemType : ComplexType = TAnonymous ([
284+ {name : idCol .name , pos : pos , kind : FVar (macro :String )},
285+ {name : valCol .name , pos : pos , kind : FVar (valueType )}
286+ ]);
287+ fields .push ({
288+ name : " iterator" ,
289+ pos : pos ,
290+ kind : FFun ({args : [], ret : macro :Iterator < $iterElemType > , expr : null }),
291+ meta : [{name : " :noCompletion" , pos : pos }]
292+ });
293+ var iterBody : Expr = {expr : EObjectDecl ([{field : idCol .name , expr : macro key }, {field : valCol .name , expr : macro cast Reflect .field ($i {thisVar }, key )}]), pos : pos };
294+ loads .push ({
295+ field : " iterator" ,
296+ expr : macro () -> [for (key in $i {keysVar }) $iterBody ].iterator ()
297+ });
254298 }
299+
300+ var objExpr : Expr = {expr : EObjectDecl (loads ), pos : pos };
301+ vars .push (macro $i {thisVar } = $objExpr );
255302 return {
256303 type : TAnonymous (fields ),
257304 vars : vars ,
258- load : { expr : EObjectDecl ( loads ), pos : pos }
305+ load : macro $ i { thisVar }
259306 };
260307 } else if (subCols .length == 1 ) {
261308 var et = simpleType (subCols [0 ].type ) ?? fullType (sub .name );
@@ -367,7 +414,7 @@ class Macros {
367414 var loadBlock = [macro var __o : Dynamic = ${getData (id )}];
368415 for (d in result .vars )
369416 loadBlock .push (d );
370- loadBlock .push (macro $i {id } = ${result .load });
417+ loadBlock .push (macro $i {id } = cast ${result .load });
371418 loadExprs .push (safeWrap (id , macro $b {loadBlock }));
372419
373420 fields .push ({
0 commit comments