gnormal / gnorm

A database-first code generator for any language
https://gnorm.org
Other
488 stars 40 forks source link

Add template based filter #94

Open daniel-reed opened 6 years ago

daniel-reed commented 6 years ago

I would like to add generic template based filter functions to Columns, Tables, Indexes, and ForeignKeys. I find myself often wanting to filter these slices on one or more properties but I feel directly coding it each time I need a new filter will be cumbersome.

It would look something like:

func (c Columns) FilterT(t string) Columns {
  //for column
  //execute template t
  //if result is truthy
  //add to result
  //return result
}

At this point, every user can define templates that render a truthy or falsey string. Then, they can easily filter in templates like this:

{{$noDefaults := .Table.Columns.FilterT "noDefaultValue.gotmpl" -}}
{{$noAutoIncrement := .Table.Columns.FilterT "noAutoIncrement.gotmpl" -}}

Feedback or alternative solutions are welcome. I don't love this implementation but I feel it is better than adding a bunch of custom filters to the code base every time we want something different.