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

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