Skip to content

Commit

Permalink
[KATC] Add snake case row transform step (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Jul 3, 2024
1 parent d678648 commit ec8bef7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
18 changes: 18 additions & 0 deletions ee/katc/case.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package katc

import (
"context"
"log/slog"

"github.com/serenize/snaker"
)

func camelToSnake(_ context.Context, _ *slog.Logger, row map[string][]byte) (map[string][]byte, error) {
snakeCaseRow := make(map[string][]byte)
for k, v := range row {
snakeCaseKey := snaker.CamelToSnake(k)
snakeCaseRow[snakeCaseKey] = v
}

return snakeCaseRow, nil
}
41 changes: 41 additions & 0 deletions ee/katc/case_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package katc

import (
"context"
"testing"

"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/require"
)

func Test_camelToSnake(t *testing.T) {
t.Parallel()

for _, tt := range []struct {
testCaseName string
input string
expectedOutput string
}{
{
testCaseName: "basic camelcase column name",
input: "emailAddress",
expectedOutput: "email_address",
},
{
testCaseName: "already snake case",
input: "email_address",
expectedOutput: "email_address",
},
} {
tt := tt
t.Run(tt.testCaseName, func(t *testing.T) {
t.Parallel()

outputRows, err := camelToSnake(context.TODO(), multislogger.NewNopLogger(), map[string][]byte{
tt.input: nil,
})
require.NoError(t, err)
require.Contains(t, outputRows, tt.expectedOutput)
})
}
}
5 changes: 5 additions & 0 deletions ee/katc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type rowTransformStep struct {
const (
snappyDecodeTransformStep = "snappy"
deserializeFirefoxTransformStep = "deserialize_firefox"
camelToSnakeTransformStep = "camel_to_snake"
)

func (r *rowTransformStep) UnmarshalJSON(data []byte) error {
Expand All @@ -77,6 +78,10 @@ func (r *rowTransformStep) UnmarshalJSON(data []byte) error {
r.name = deserializeFirefoxTransformStep
r.transformFunc = deserializeFirefox
return nil
case camelToSnakeTransformStep:
r.name = camelToSnakeTransformStep
r.transformFunc = camelToSnake
return nil
default:
return fmt.Errorf("unknown data processing step %s", s)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/katc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestConstructKATCTables(t *testing.T) {
"columns": ["col1", "col2"],
"source": "/some/path/to/a/different/db.sqlite",
"query": "SELECT col1, col2 FROM some_table;",
"row_transform_steps": []
"row_transform_steps": ["camel_to_snake"]
}`, runtime.GOOS),
},
expectedPluginCount: 2,
Expand Down

0 comments on commit ec8bef7

Please sign in to comment.