Skip to content

Commit

Permalink
✨ Add template support for table renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Jul 5, 2023
1 parent eaa8c3a commit 54aa3fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/glazed/handlers/datatables/datatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-go-golems/glazed/pkg/formatters/json"
table_formatter "github.com/go-go-golems/glazed/pkg/formatters/table"
"github.com/go-go-golems/glazed/pkg/middlewares/row"
"github.com/go-go-golems/glazed/pkg/middlewares/table"
"github.com/go-go-golems/glazed/pkg/types"
"github.com/go-go-golems/parka/pkg/glazed"
"github.com/go-go-golems/parka/pkg/glazed/handlers"
Expand Down Expand Up @@ -61,6 +62,11 @@ type DataTables struct {

// AdditionalData to be passed to the rendering engine
AdditionalData map[string]interface{}
// StreamRows is used to control whether the rows should be streamed or not.
// If set to false, a TableMiddleware used (which collects all rows, but thus also all column names)
// into memory before passing them to the template.
// This is useful when the rows are "ragged" (i.e. not all rows have the same number of columns).
StreamRows bool
}

//go:embed templates/*
Expand Down Expand Up @@ -162,6 +168,12 @@ func WithAdditionalData(data map[string]interface{}) QueryHandlerOption {
}
}

func WithStreamRows(streamRows bool) QueryHandlerOption {
return func(h *QueryHandler) {
h.dt.StreamRows = streamRows
}
}

func (qh *QueryHandler) Handle(c *gin.Context, w io.Writer) error {
pc := glazed.NewCommandContext(qh.cmd)

Expand Down Expand Up @@ -208,9 +220,14 @@ func (qh *QueryHandler) Handle(c *gin.Context, w io.Writer) error {
return err
}

gp.ReplaceTableMiddleware()
gp.AddRowMiddleware(row.NewColumnsChannelMiddleware(columnsC, true))
gp.AddRowMiddleware(row.NewOutputChannelMiddleware(of, rowC))
if dt_.StreamRows {
gp.ReplaceTableMiddleware()
gp.AddRowMiddleware(row.NewColumnsChannelMiddleware(columnsC, true))
gp.AddRowMiddleware(row.NewOutputChannelMiddleware(of, rowC))
} else {
gp.AddTableMiddleware(table.NewColumnsChannelMiddleware(columnsC))
gp.AddTableMiddleware(table.NewOutputChannelMiddleware(of, rowC))
}

ctx := c.Request.Context()
ctx2, cancel := context.WithCancel(ctx)
Expand Down
1 change: 1 addition & 0 deletions pkg/handlers/command-dir/command-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error {
datatables.WithTemplateLookup(cd.TemplateLookup),
datatables.WithTemplateName(cd.TemplateName),
datatables.WithAdditionalData(cd.AdditionalData),
datatables.WithStreamRows(cd.Stream),
}

datatables.CreateDataTablesHandler(sqlCommand, path, commandPath, options...)(c)
Expand Down

0 comments on commit 54aa3fc

Please sign in to comment.