Skip to content

Commit

Permalink
Combine table definition structs
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Jul 15, 2024
1 parent 8c46927 commit 606ade6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
15 changes: 8 additions & 7 deletions ee/katc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ type (
// katcTableConfig is the configuration for a specific KATC table. The control server
// sends down these configurations.
katcTableConfig struct {
Columns []string `json:"columns"`
SourceType katcSourceType `json:"source_type"`
SourcePaths []string `json:"source_paths"` // Describes how to connect to source (e.g. path to db) -- % and _ wildcards supported
SourceQuery string `json:"source_query"` // Query to run against each source path
RowTransformSteps []rowTransformStep `json:"row_transform_steps"`
Overlays []katcTableConfigOverlay `json:"overlays"`
Columns []string `json:"columns"`
katcTableDefinition
Overlays []katcTableConfigOverlay `json:"overlays"`
}

katcTableConfigOverlay struct {
Filters map[string]string `json:"filters"` // determines if this overlay is applicable to this launcher installation
Filters map[string]string `json:"filters"` // determines if this overlay is applicable to this launcher installation
katcTableDefinition
}

katcTableDefinition struct {
SourceType *katcSourceType `json:"source_type,omitempty"`
SourcePaths *[]string `json:"source_paths,omitempty"` // Describes how to connect to source (e.g. path to db) -- % and _ wildcards supported
SourceQuery *string `json:"source_query,omitempty"` // Query to run against each source path
Expand Down
26 changes: 20 additions & 6 deletions ee/katc/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package katc

import (
"context"
"errors"
"fmt"
"log/slog"
"regexp"
Expand Down Expand Up @@ -44,12 +45,21 @@ func newKatcTable(tableName string, cfg katcTableConfig, slogger *slog.Logger) (
}

k := katcTable{
sourceType: cfg.SourceType,
sourcePaths: cfg.SourcePaths,
sourceQuery: cfg.SourceQuery,
rowTransformSteps: cfg.RowTransformSteps,
columnLookup: columnLookup,
slogger: slogger,
columnLookup: columnLookup,
slogger: slogger,
}

if cfg.SourceType != nil {
k.sourceType = *cfg.SourceType
}
if cfg.SourcePaths != nil {
k.sourcePaths = *cfg.SourcePaths
}
if cfg.SourceQuery != nil {
k.sourceQuery = *cfg.SourceQuery
}
if cfg.RowTransformSteps != nil {
k.rowTransformSteps = *cfg.RowTransformSteps
}

// Check overlays to see if any of the filters apply to us;
Expand Down Expand Up @@ -94,6 +104,10 @@ func filtersMatch(filters map[string]string) bool {

// generate handles queries against a KATC table.
func (k *katcTable) generate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
if k.sourceType.dataFunc == nil {
return nil, errors.New("table source type not set")
}

// Fetch data from our table source
dataRaw, err := k.sourceType.dataFunc(ctx, k.slogger, k.sourcePaths, k.sourceQuery, queryContext)
if err != nil {
Expand Down
35 changes: 20 additions & 15 deletions ee/katc/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,35 @@ func Test_generate_SqliteBackedIndexedDB(t *testing.T) {

// At long last, our source is adequately configured.
// Move on to constructing our KATC table.
sourceQuery := "SELECT data FROM object_data;"
cfg := katcTableConfig{
SourceType: katcSourceType{
name: sqliteSourceType,
dataFunc: sqliteData,
},
Columns: []string{expectedColumn},
SourcePaths: []string{filepath.Join("some", "incorrect", "path")},
SourceQuery: "SELECT data FROM object_data;",
RowTransformSteps: []rowTransformStep{
{
name: snappyDecodeTransformStep,
transformFunc: snappyDecode,
Columns: []string{expectedColumn},
katcTableDefinition: katcTableDefinition{
SourceType: &katcSourceType{
name: sqliteSourceType,
dataFunc: sqliteData,
},
{
name: deserializeFirefoxTransformStep,
transformFunc: deserializeFirefox,
SourcePaths: &[]string{filepath.Join("some", "incorrect", "path")},
SourceQuery: &sourceQuery,
RowTransformSteps: &[]rowTransformStep{
{
name: snappyDecodeTransformStep,
transformFunc: snappyDecode,
},
{
name: deserializeFirefoxTransformStep,
transformFunc: deserializeFirefox,
},
},
},
Overlays: []katcTableConfigOverlay{
{
Filters: map[string]string{
"goos": runtime.GOOS,
},
SourcePaths: &[]string{filepath.Join(databaseDir, "%.sqlite")}, // All sqlite files in the test directory
katcTableDefinition: katcTableDefinition{
SourcePaths: &[]string{filepath.Join(databaseDir, "%.sqlite")}, // All sqlite files in the test directory
},
},
},
}
Expand Down

0 comments on commit 606ade6

Please sign in to comment.