github.com/tobgu/qframe@v0.4.0/internal/ecolumn/generator.go (about)

     1  package ecolumn
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/tobgu/qframe/filter"
     7  	"github.com/tobgu/qframe/internal/maps"
     8  	"github.com/tobgu/qframe/internal/template"
     9  )
    10  
    11  //go:generate qfgenerate -source=efilter -dst-file=filters_gen.go
    12  //go:generate qfgenerate -source=edoc -dst-file=doc_gen.go
    13  
    14  const basicColConstComparison = `
    15  func {{.name}}(index index.Int, column []enumVal, comparatee enumVal, bIndex index.Bool) {
    16  	for i, x := range bIndex {
    17  		if !x {
    18  			enum := column[index[i]]
    19  			bIndex[i] = !enum.isNull() && enum.compVal() {{.operator}} comparatee.compVal()
    20  		}
    21  	}
    22  }
    23  `
    24  
    25  const basicColColComparison = `
    26  func {{.name}}(index index.Int, col, col2 []enumVal, bIndex index.Bool) {
    27  	for i, x := range bIndex {
    28  		if !x {
    29  			enum, enum2 := col[index[i]], col2[index[i]]
    30  			bIndex[i] = !enum.isNull() && !enum2.isNull() && enum.compVal() {{.operator}} enum2.compVal()
    31  		}
    32  	}
    33  }
    34  `
    35  
    36  func spec(name, operator, templateStr string) template.Spec {
    37  	return template.Spec{
    38  		Name:     name,
    39  		Template: templateStr,
    40  		Values:   map[string]interface{}{"name": name, "operator": operator}}
    41  }
    42  
    43  func colConstComparison(name, operator string) template.Spec {
    44  	return spec(name, operator, basicColConstComparison)
    45  }
    46  
    47  func colColComparison(name, operator string) template.Spec {
    48  	return spec(name, operator, basicColColComparison)
    49  }
    50  
    51  func GenerateFilters() (*bytes.Buffer, error) {
    52  	// If adding more filters here make sure to also add a reference to them
    53  	// in the corresponding filter map so that they can be looked up.
    54  	return template.GenerateFilters("ecolumn", []template.Spec{
    55  		colConstComparison("lt", filter.Lt),
    56  		colConstComparison("lte", filter.Lte),
    57  		colConstComparison("gt", filter.Gt),
    58  		colConstComparison("gte", filter.Gte),
    59  		colConstComparison("eq", "=="), // Go eq ("==") differs from qframe eq ("=")
    60  		colColComparison("lt2", filter.Lt),
    61  		colColComparison("lte2", filter.Lte),
    62  		colColComparison("gt2", filter.Gt),
    63  		colColComparison("gte2", filter.Gte),
    64  		colColComparison("eq2", "=="), // Go eq ("==") differs from qframe eq ("=")
    65  	})
    66  }
    67  
    68  func GenerateDoc() (*bytes.Buffer, error) {
    69  	return template.GenerateDocs(
    70  		"ecolumn",
    71  		maps.StringKeys(filterFuncs0, filterFuncs1, filterFuncs2, multiFilterFuncs, multiInputFilterFuncs),
    72  		maps.StringKeys())
    73  }