Skip to content

Commit

Permalink
Compare a base csv with the new csv and print changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinkarthik committed Apr 15, 2018
1 parent 9e0c77f commit 701f85e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 26 deletions.
27 changes: 16 additions & 11 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func init() {
type Config struct {
KeyPositions []int
Encoder string
Base string
Input string
Output string
}

func (c Config) GetKeyPositions() []int {
Expand All @@ -40,22 +40,27 @@ func (c Config) GetEncoder() encoder.Encoder {
}
}

func (c Config) GetReader() io.Reader {
if c.Input != "STDIN" && c.Input != "-" && c.Input != "" {
file, err := os.Open(c.Input)
func (c Config) GetBase() io.Reader {
return getReader(c.Base)
}

if err != nil {
log.Fatal(err)
}
func (c Config) GetInput() io.Reader {
return getReader(c.Input)
}

return file
func getReader(filename string) io.Reader {
file, err := os.Open(filename)

if err != nil {
log.Fatal(err)
}
return os.Stdin

return file
}

func (c Config) GetWriter() io.Writer {
if c.Output != "STDOUT" && c.Output != "-" && c.Output != "" {
file, err := os.Create(c.Output)
if c.Input != "STDOUT" && c.Input != "-" && c.Input != "" {
file, err := os.Create(c.Input)

if err != nil {
log.Fatal(err)
Expand Down
18 changes: 9 additions & 9 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ func TestGetKeyPositions(t *testing.T) {
assert.Equal(t, []int{0}, config.GetKeyPositions())
}

func TestReader(t *testing.T) {
config := Config{Input: "STDIN"}
assert.Equal(t, os.Stdin, config.GetReader())

config = Config{Input: "-"}
assert.Equal(t, os.Stdin, config.GetReader())
}
// func TestReader(t *testing.T) {
// config := Config{Base: "STDIN"}
// assert.Equal(t, os.Stdin, config.GetReader())
//
// config = Config{Base: "-"}
// assert.Equal(t, os.Stdin, config.GetReader())
// }

func TestWriter(t *testing.T) {
config := Config{Input: "STDOUT"}
config := Config{Base: "STDOUT"}
assert.Equal(t, os.Stdout, config.GetWriter())

config = Config{Input: "-"}
config = Config{Base: "-"}
assert.Equal(t, os.Stdout, config.GetWriter())
}
38 changes: 32 additions & 6 deletions cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/aswinkarthik93/csv-digest/pkg/digest"
Expand All @@ -43,17 +44,39 @@ func runDigest() {
log.Fatal(err)
}

digestConfig := digest.DigestConfig{
baseConfig := digest.DigestConfig{
KeyPositions: config.GetKeyPositions(),
Encoder: config.GetEncoder(),
Reader: config.GetReader(),
Writer: config.GetWriter(),
Reader: config.GetBase(),
Writer: os.Stdout,
}

_, err := digest.Create(digestConfig)
inputConfig := digest.DigestConfig{
KeyPositions: config.GetKeyPositions(),
Encoder: config.GetEncoder(),
Reader: config.GetInput(),
Writer: os.Stdout,
}

base, err := digest.Create(baseConfig)
if err != nil {
log.Fatal(err)
}
log.Println("Generated Base Digest")

change, err := digest.Create(inputConfig)
if err != nil {
log.Fatal(err)
}
log.Println("Generated Input file Digest")

additions, modifications := digest.Compare(base, change)

fmt.Println(fmt.Sprintf("Additions Count: %d", len(additions)))
fmt.Println(additions)
fmt.Println("")
fmt.Println(fmt.Sprintf("Modifications Count: %d", len(modifications)))
fmt.Println(modifications)
}

var debug bool
Expand All @@ -70,9 +93,12 @@ func init() {
// is called directly, e.g.:
// digestCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

digestCmd.Flags().StringVarP(&config.Input, "input", "i", "STDIN", "Input csv file to read from")
digestCmd.Flags().StringVarP(&config.Output, "output", "o", "STDOUT", "Digest filename to save to")
digestCmd.Flags().StringVarP(&config.Base, "base", "b", "", "Input csv to be used as base")
digestCmd.Flags().StringVarP(&config.Input, "input", "i", "", "The new csv file on which diff should be done")
digestCmd.Flags().StringVarP(&config.Encoder, "encoder", "e", "json", "Encoder to use to output the digest. Available Encoders: "+strings.Join(GetEncoders(), ","))
digestCmd.Flags().IntSliceVarP(&config.KeyPositions, "key-positions", "k", []int{0}, "Primary key positions of the Input CSV as comma separated values Eg: 1,2")
digestCmd.Flags().BoolVarP(&debug, "debug", "", false, "Debug mode")

digestCmd.MarkFlagRequired("base")
digestCmd.MarkFlagRequired("input")
}
24 changes: 24 additions & 0 deletions pkg/digest/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package digest

func Compare(baseDigest, newDigest map[uint64]uint64) (additions []uint64, modifications []uint64) {
maxSize := len(newDigest)
additions = make([]uint64, maxSize)
modifications = make([]uint64, maxSize)

additionCounter := 0
modificationCounter := 0
for k, newVal := range newDigest {
if oldVal, present := baseDigest[k]; present {
if newVal != oldVal {
//Modifications
modifications[modificationCounter] = k
modificationCounter++
}
} else {
//Additions
additions[additionCounter] = k
additionCounter++
}
}
return additions[:additionCounter], modifications[:modificationCounter]
}
30 changes: 30 additions & 0 deletions pkg/digest/compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package digest

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCompare(t *testing.T) {
baseDigest := map[uint64]uint64{
10000106069522789940: 11608188164212916000,
10000305084889337335: 11796412213504516000,
10024909476616779194: 14500526491611670000,
1004896778135186857: 15778011848259830000,
}

newDigest := map[uint64]uint64{
10000106069522789940: 11608188164212916000,
10000305084889337335: 11796412213504516001,
10049141081086325814: 12259600610026582000,
}

additions, modifications := Compare(baseDigest, newDigest)

expectedAdditions := []uint64{10049141081086325814}
expectedModifications := []uint64{10000305084889337335}

assert.Equal(t, expectedAdditions, additions)
assert.Equal(t, expectedModifications, modifications)
}

0 comments on commit 701f85e

Please sign in to comment.