jcward / haxe-graphql

Utilities for working with Haxe and GraphQL.
MIT License
23 stars 6 forks source link

Generate Haxe AST instead of string buffer #17

Closed jcward closed 6 years ago

jcward commented 6 years ago

For the macro workflow, it will be necessary to generate Haxe AST (e.g. Array<TypeDefinition>) instead of writing Haxe strings directly.

jcward commented 6 years ago

Example type gen: https://github.com/ncannasse/castle/blob/master/cdb/Module.hx

import haxe.macro.Printer;
import haxe.macro.Expr;

...

    var fakePos = { file:'Untitled', min:0, max:0 };
    var td:TypeDefinition = {
      pack:[],
      name:'Foo',
      pos:fakePos,
      meta:[],
      params:[],
      kind:TDStructure,
      fields:[]
    }

    td.fields.push( { name : "abc", pos : fakePos, kind : FVar(macro :Int), meta:[{ name:':optional', pos:fakePos }] } );

    trace(td);
    var p = new Printer();

    trace(p.printTypeDefinition(td));

Prints:

typedef Foo = {
    @:optional
    var abc : Int;
}
jcward commented 6 years ago

Fixed via fragment_wip branch merge.

Introduced intermediate AST data structures, some of which are Haxe-compatible and mapped through the haxe.macro.Printer. Though I had to copy & modify Printer.hx to output the preferred short format structs.