Project

General

Profile

worm class generator - suggestions for a template engine design?

Added by a person over 13 years ago

hi all,

i have been working on a class generator called worm (for now ..) its at:

git@github.com:erikwinn/worm.git

right now wormgen can connect to a database (currently mysql or sqlite) with an

existing schema, collect all the metadata and generate classes that work

with Dbo. they are still just simple header files like the first one in

the tutorial but internally support for foreign keys is there - i am at the

moment working out a templating system (file templates, not C template<>) to

control the generation.

so, the question is: are there any ideas/suggestions for how to implement this?

my initial idea is that the template looks something like this:

<code>
#include <Wt/Dbo/Dbo>

<% if has_string %>
#include <string>
<% endif %>
namespace dbo = Wt::Dbo;

class <% $className %> {

    public:
        <% foreach columns %>
        <% if type_unsupported %>//unsupported <% endif %>$unsigned $type $variablename;
        <% endforeach %>

    template<class Action> void persist(Action& a)
    {
        <% foreach columns %>
        <% if type_unsupported %>//unsupported <% endif %>dbo::field(a, $variablename,"$columnname");
        <% endforeach %>
    }
};

</code>

this is not set in stone (in fact i have not done much on the grammar yet)

the snippet above is just psuedocode thinking.

anyway, since Dbo is the first supported target output i thought people here

might have an interest. i am about to start on the lexer/parser - this is

domain specific and i would like to keep it quite simple at least to start;

basically its just a way to make the output flexible so that, for instance,

one might make the variables private with accessors, etc. additionally it

should work for generating CRUD scaffolding from the same metadata.

suggestions, ideas welcome.

cheer,

-e


Replies (9)

RE: worm class generator - suggestions for a template engine design? - Added by Koen Deforche over 13 years ago

Hey,

I think it is a great idea to abstract this generation process with a template language.

Personally, I would try to find a solution where you integrate a syntax subset of an existing scripting/template language, or possibly even a complete interpreter for it. These days, they comes very cheaply. Almost at every occasion that I didn't go this route, I regretted it in the end.

A quick google suggests "Cheetah" as an interesting one (even if only to get syntax ideas from) ?

Regards,

koen

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

Personally, I would try to find a solution where you integrate a syntax subset of an existing scripting/template language, or possibly even a complete interpreter for it. These days, they comes very cheaply. Almost at every occasion that I didn't go this route, I regretted it in the end.

agreed - the question is whether to bring in outside dependencies or write an engine .. unless there is real interest in a general purpose template engine i am inclined to write a simple hand-rolled engine for worm. however, if Wt could use a general purpose engine (but what about WTemplate?) that could be a fun project - it would just push back the timeline on worm quite a bit.

A quick google suggests "Cheetah" as an interesting one (even if only to get syntax ideas from) ?

i looked a cheetah and a few others - i am a little hesitant to bring in python, for one thing i am not very good at it. and i am also hesitant about outside dependencies, but that is flexible. however, i have found a few that are candidates in C: Poco's page compiler and Teng are powerful but maybe overkill and a bit difficult to use, ctemplate is light but would need additional functionality (doesn't do loops) .. there are a couple of other smaller solutions of questionable stability but might work as a starting point..

which leaves a couple of questions:

A. whether to do a quick domain specific engine for worm generator or general purpose template engine?

B. if general purpose, which existing solution to integrate (or reimplement ..) or whether to write a new one?

and A.1, if domain specific what kind of syntax is preferred: ASPish as shown above or sh/PHP/PYTHONish like cheetah?

just thoughts.

-e

RE: worm class generator - suggestions for a template engine design? - Added by Koen Deforche over 13 years ago

Hey,

As to WTemplate, in our experience the only thing that might be missing is conditions. I believe that loops are not only incovenient, given that C is a statically typed language, but also usually a single 'item' is better confined in its own widget (which could again be a WTemplate). This binding problem does not exist in worm since there you have a fixed data structure.

External dependencies are certainly a worry --- but you could start with one, and later always decide to re-implement the template engine.

So as to your questions:

A. adopt a general purpose template engine, possibly subset what you use from it, for worm

B. which one, it seems you've done already some research there, so you're in a better position to pick one. I would probably consider engines that are broadly available/used and get thumbs up.

I prefer python-ish --- at least you do not arbitrarily define a new programming language in this way.

Regards,

koen

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

thanks for the input - i thought about it some more and reviewed the options again. it makes sense to have a general purpose engine and looking more closely at ctemplate i found that it can be used for iterations and conditionals but not explicitly in the template itself (which is intentional to separate logic from presentation). if a marker in the template does not have a dictionary or if the dictionary is empty it is ignored and "sections" provide iteration according to the sub-dictionaries available. the syntax is very simple - logic is mostly in how dictionaries are initialized. this means i can get it to do what is needed for worm and later it would not be difficult to reimplement.

ctemplate is quite light, easily available, obviously stable (it is the template system for google's main search page) and similar to something i might do anyway. it also fills the role of a general purpose template engine with a few extras that are nice (template caching, auto escaping against xss, etc.). so i think i will give that a whirl - should have something interesting soon :).

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

update - worm 0.2 released! ctemplate worked out quite well, it was easy to replace my half written parser .. so now wormgen uses the new generator classes and uses templates - yay!

cheer,

-e

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

hello all!

ANNOUNCING: wormgen 0.2! new supports foreign keys and templating

wormgen now uses a templating system - so that you can modify the templates to your liking. it also now supports foreign keys (mysql only at the moment) - for any given database schema the default template creates:

  • forward declarations (if needed)

  • a class declaration

  • private variables with gettors and settors

  • referenced Dbo::ptr ptrClass for foreign keys (many to one)

  • referenced Dbo::collection<Dbo::ptr> ptrClasses (one to many - pluralized even :))

  • fills out Dbo::field() for each variable

  • fills out Dbo::belongsTo() to for each foreign key

  • fills out Dbo::hasMany() to for each reverse reference

i welcome rants, raves, comments, critiques, ideas - and particularly, where to go with this from here. the next phase is perhaps scaffolding design .. witty on rails anyone?

enjoy!

-e

RE: worm class generator - suggestions for a template engine design? - Added by Koen Deforche over 13 years ago

Hey,

This seems great. I think the approach of making an entirely dynamic solution, based on the introspection, would be nice: hook it up to a database and get forms and tables ?

Regards,

koen

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

>hook it up to a database and get forms and tables ?

yep, precisely - for an example (and what has inspired the idea) see http://qcodo.com. worm is basically a reimplementation of the QCodo ORM layer in principle. QCodo is also a widget library for web but in PHP; from a given database schema the code generator creates ORM classes and fully functional CRUD classes and pages using the QCodo widgets. it is a very powerful RAD system - model the data with SQL in a schema (SQL is great for this kind of thing), run the code generator and in literally about a minute you have a full web interface to the database with a very nice API, ORM classes, SOAP/REST access. in fact, it is so powerful that when i had to use Rails for a project i was quite disappointed - Rails does little of the boilerplate and doesn't even have a proper MVC implementation with signals or widgets.

so there - the secret plan is revealed: implement a QCodo-like system but using Wt as the widget library (bwaahaahaa! and take over the world!).

trickiest part is coming up with names .. working on that ..

-e

RE: worm class generator - suggestions for a template engine design? - Added by a person over 13 years ago

an entirely dynamic solution, based on the introspection, would be nice: hook it up to a database and get forms and tables ?

yep - precisely again ;). i had answer this twice as there are two ways to think about it - the second way is to use the Qt approach which is dynamic and uses runtime introspection. to be honest i am not sure which is the better approach but implementing the Qt approach with Wt might be less work - much of the API is already there, i think it would be possible to straighten out Dbo a little bit so that it better fits the paradigm and implement metadata functionality so that it can map from the database. alternately the worm code could be expanded a little more to implement the bridge to WAbstractItemModel, although i would want to improve the DAL part of worm as it is still quite basic. either way one could achieve the Qt style dynamic solution.

so, there are two possible directions. not sure yet which is the better route ..

-e

    (1-9/9)