Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KATC] Add snake case row transform step #1765

Merged
merged 26 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dda79df
Construct KATC tables
RebeccaMahany Jun 26, 2024
5530c17
Add support for deserializing structured_clone javascript objects
RebeccaMahany Jun 26, 2024
80b380b
Reorder function args
RebeccaMahany Jun 27, 2024
38c9e46
Rename type => source
RebeccaMahany Jun 27, 2024
90a930b
Fetch columns from query results
RebeccaMahany Jun 27, 2024
42ae269
Ensure path is included in results; reorder func args
RebeccaMahany Jun 27, 2024
4782383
Merge remote-tracking branch 'upstream/main' into becca/katc-construct
RebeccaMahany Jun 27, 2024
376714f
Merge remote-tracking branch 'upstream/main' into becca/katc-construct
RebeccaMahany Jun 28, 2024
15f7ea5
Read-only
RebeccaMahany Jun 28, 2024
f52843e
Transform entire row instead of individual data to properly unwrap in…
RebeccaMahany Jun 28, 2024
9868c16
Add source path constraint filtering so we don't run queries against …
RebeccaMahany Jun 28, 2024
9159366
Add test for constraint checks
RebeccaMahany Jun 28, 2024
1e32327
Rename function for brevity
RebeccaMahany Jun 28, 2024
af341ae
Add documentation
RebeccaMahany Jun 28, 2024
3d0c135
Add a table test
RebeccaMahany Jun 28, 2024
212b047
discard column log is way too noisy, remove it
RebeccaMahany Jun 28, 2024
f7c7eb8
Remove source type until implemented
RebeccaMahany Jun 28, 2024
3efe890
Rename to disambiguate source (type of table) and source (specific lo…
RebeccaMahany Jul 1, 2024
7fbdcb8
Rename structured clone to something more intuitive
RebeccaMahany Jul 1, 2024
97d8647
Fix dsn for sqlite
RebeccaMahany Jul 1, 2024
ccc8e32
Add row transform step to transform camel to snake case
RebeccaMahany Jul 1, 2024
62e4061
Remove unneeded check
RebeccaMahany Jul 1, 2024
eda263e
Don't need unnecessary variable, return early if constraint not met
RebeccaMahany Jul 1, 2024
3019cf0
Support LIKE syntax for source rather than glob
RebeccaMahany Jul 1, 2024
b6986a0
Merge branch 'becca/katc-construct' into becca/katc-snake-case
RebeccaMahany Jul 1, 2024
db95120
Merge remote-tracking branch 'upstream/main' into becca/katc-snake-case
RebeccaMahany Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading