Skip to content

Commit

Permalink
Support JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
xeger committed Apr 11, 2023
1 parent b40a209 commit dea1148
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/scrub.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/cobra"
"github.com/xeger/sqlstream/nlp"
"github.com/xeger/sqlstream/scrubbing"
"github.com/xeger/sqlstream/scrubbing/json"
"github.com/xeger/sqlstream/scrubbing/mysql"
)

Expand Down Expand Up @@ -67,13 +68,21 @@ func scrub(cmd *cobra.Command, args []string) {
}

switch format {
case "json":
scrubJson(models)
case "mysql":
scrubMysql(models)
default:
panic("unknown format: " + format)
}
}

func scrubJson(models []nlp.Model) {
sc := scrubbing.NewScrubber(salt, models, 0.95)
// TODO: parallelize JSON scrubbing (but not parsing)
json.Scrub(sc, os.Stdin, os.Stdout)
}

func scrubMysql(models []nlp.Model) {
N := parallelism

Expand Down
19 changes: 19 additions & 0 deletions scrubbing/json/scrub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package json

import (
"encoding/json"
"io"

"github.com/xeger/sqlstream/scrubbing"
)

// Scrub sanitizes a single line, which may contain multiple SQL statements.
func Scrub(sc *scrubbing.Scrubber, r io.Reader, w io.Writer) {
dec := json.NewDecoder(r)
enc := json.NewEncoder(w)
var v any
for err := dec.Decode(&v); err == nil; err = dec.Decode(&v) {
sc.ScrubData(v)
enc.Encode(v)
}
}

0 comments on commit dea1148

Please sign in to comment.