Skip to content

Commit

Permalink
Actually provide fake values
Browse files Browse the repository at this point in the history
  • Loading branch information
mpchadwick committed Apr 2, 2019
1 parent 65fa490 commit 4a9621b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ func main() {
// We don't want to hear about it
log.SetOutput(ioutil.Discard)

c := dbanon.NewConfig()
p := dbanon.NewLineProcessor(c)
config := dbanon.NewConfig()
provider := dbanon.NewProvider()
processor := dbanon.NewLineProcessor(config, provider)

reader := bufio.NewReader(os.Stdin)
for {
text, err := reader.ReadString('\n')
fmt.Print(p.ProcessLine(text))
fmt.Print(processor.ProcessLine(text))

if err != nil {
break
Expand Down
8 changes: 4 additions & 4 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func (c Config) ProcessTable(t string) bool {
return false
}

func (c Config) ProcessColumn(t string, col string) bool {
func (c Config) ProcessColumn(t string, col string) (bool, string) {
for _, table := range c.Tables {
if (table.Name != t) {
continue
}

for k, _ := range table.Columns {
for k, v := range table.Columns {
if k == col {
return true
return true, v
}
}
}

return false
return false, ""
}
14 changes: 8 additions & 6 deletions src/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

type LineProcessor struct {
Config *Config
Provider *Provider
}

func NewLineProcessor(c *Config) *LineProcessor {
return &LineProcessor{Config: c}

func NewLineProcessor(c *Config, p *Provider) *LineProcessor {
return &LineProcessor{Config: c, Provider: p}
}

func (p LineProcessor) ProcessLine(s string) string {
Expand Down Expand Up @@ -39,21 +39,23 @@ func (p LineProcessor) ProcessLine(s string) string {

column := stmt.Columns[i].String()

if !p.Config.ProcessColumn(table, column) {
result, dataType := p.Config.ProcessColumn(table, column)

if !result {
continue
}

switch v := e.(type) {
case *sqlparser.SQLVal:
switch v.Type {
default:
v.Val = []byte("999")
v.Val = []byte(p.Provider.Get(dataType))
}
}
}
}

return sqlparser.String(stmt)
return sqlparser.String(stmt) + ";\n"

default:
return s
Expand Down
30 changes: 30 additions & 0 deletions src/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dbanon

import (
"syreclabs.com/go/faker"
)

type Provider struct{}

func NewProvider() *Provider {
p := &Provider{}

return p
}

func (p Provider) Get(s string) string {
switch s {
case "firstname":
return faker.Name().FirstName()
case "lastname":
return faker.Name().LastName()
case "email":
return faker.Internet().Email()
case "username":
return faker.Internet().UserName()
case "password":
return faker.Internet().Password(8, 14)
}

return ""
}

0 comments on commit 4a9621b

Please sign in to comment.