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

     1  package generator
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  
     7  	"github.com/tobgu/qframe/internal/template"
     8  )
     9  
    10  //go:generate qfgenerate -source=qframe -dst-file=../../../qframe_gen.go
    11  
    12  const viewTemplate = `
    13  // {{.type}}View provides a "view" into an {{.lowerType}} column and can be used for access to individual elements.
    14  type {{.type}}View struct {
    15  	{{.package}}.View
    16  }
    17  
    18  // {{.type}}View returns a view into an {{.lowerType}} column identified by name.
    19  //
    20  // colName - Name of the column.
    21  //
    22  // Returns an error if the column is missing or of wrong type.
    23  // Time complexity O(1).
    24  func (qf QFrame) {{.type}}View(colName string) ({{.type}}View, error) {
    25  	namedColumn, ok := qf.columnsByName[colName]
    26  	if !ok {
    27  		return {{.type}}View{}, qerrors.New("{{.type}}View", "unknown column: %s", colName)
    28  	}
    29  
    30  	col, ok := namedColumn.Column.({{.package}}.Column)
    31  	if !ok {
    32  		return {{.type}}View{}, qerrors.New(
    33  			"{{.type}}View",
    34  			"invalid column type, expected: %s, was: %s", "{{.lowerType}}", namedColumn.DataType())
    35  	}
    36  
    37  	return  {{.type}}View{View: col.View(qf.index)}, nil
    38  }
    39  
    40  // Must{{.type}}View returns a view into an {{.lowerType}} column identified by name.
    41  //
    42  // colName - Name of the column.
    43  //
    44  // Panics if the column is missing or of wrong type.
    45  // Time complexity O(1).
    46  func (qf QFrame) Must{{.type}}View(colName string) {{.type}}View {
    47  	view, err := qf.{{.type}}View(colName)
    48  	if err != nil {
    49  		panic(qerrors.Propagate("Must{{.type}}View", err))
    50  	}
    51  	return view
    52  }
    53  
    54  `
    55  
    56  func spec(typeName, srcPackage string) template.Spec {
    57  	return template.Spec{
    58  		Name:     typeName,
    59  		Template: viewTemplate,
    60  		Values:   map[string]interface{}{"type": typeName, "lowerType": strings.ToLower(typeName), "package": srcPackage}}
    61  }
    62  
    63  func view(typeName, srcPackage string) template.Spec {
    64  	return spec(typeName, srcPackage)
    65  }
    66  
    67  func GenerateQFrame() (*bytes.Buffer, error) {
    68  	return template.Generate("qframe", []template.Spec{
    69  		view("Int", "icolumn"),
    70  		view("Float", "fcolumn"),
    71  		view("Bool", "bcolumn"),
    72  		view("String", "scolumn"),
    73  		view("Enum", "ecolumn"),
    74  	}, []string{
    75  		"github.com/tobgu/qframe/qerrors",
    76  		"github.com/tobgu/qframe/internal/icolumn",
    77  		"github.com/tobgu/qframe/internal/fcolumn",
    78  		"github.com/tobgu/qframe/internal/bcolumn",
    79  		"github.com/tobgu/qframe/internal/scolumn",
    80  		"github.com/tobgu/qframe/internal/ecolumn",
    81  	})
    82  }