Skip to content

Commit

Permalink
Return sourceMap as a hash of primary key to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinkarthik committed Feb 28, 2019
1 parent 0100b30 commit f185a04
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
12 changes: 4 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,21 @@ func (c *Config) GetIncludeColumnPositions() digest.Positions {
// Validate validates the config object
// and returns error if not valid.
func (c *Config) Validate() error {
allFormats := []string{rowmark, jsonFormat}
allFormats := []string{rowmark, jsonFormat, diffFormat}

formatValid := false
for _, format := range allFormats {
if strings.ToLower(c.Format) == format {
formatValid = true
return nil
}
}

if !formatValid {
return errors.New("Specified format is not valid")
}

return nil
return errors.New("Specified format is not valid")
}

const (
rowmark = "rowmark"
jsonFormat = "json"
diffFormat = "diff"
)

// Formatter instantiates a new formatted
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func init() {
rootCmd.Flags().IntSliceVarP(&config.PrimaryKeyPositions, "primary-key", "p", []int{0}, "Primary key positions of the Input CSV as comma separated values Eg: 1,2")
rootCmd.Flags().IntSliceVarP(&config.ValueColumnPositions, "columns", "", []int{}, "Selectively compare positions in CSV Eg: 1,2. Default is entire row")
rootCmd.Flags().IntSliceVarP(&config.IncludeColumnPositions, "include", "", []int{}, "Include positions in CSV to display Eg: 1,2. Default is entire row")
rootCmd.Flags().StringVarP(&config.Format, "format", "o", "rowmark", "Available (rowmark|json)")
rootCmd.Flags().StringVarP(&config.Format, "format", "o", "rowmark", "Available (rowmark|json|diff)")

rootCmd.Flags().BoolVarP(&timed, "time", "", false, "Measure time")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/digest/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type diffMessage struct {
// Diff will differentiate between two given configs
func Diff(baseConfig, deltaConfig *Config) (Difference, error) {
maxProcs := runtime.NumCPU()
base, err := Create(baseConfig)
base, _, err := Create(baseConfig)

if err != nil {
return Difference{}, fmt.Errorf("error in base file: %v", err)
Expand Down
16 changes: 13 additions & 3 deletions pkg/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ const bufferSize = 512
// Create can create a Digest using the Configurations passed.
// It returns the digest as a map[uint64]uint64.
// It can also keep track of the Source line.
func Create(config *Config) (map[uint64]uint64, error) {
func Create(config *Config) (map[uint64]uint64, map[uint64][]string, error) {
maxProcs := runtime.NumCPU()
reader := csv.NewReader(config.Reader)

output := make(map[uint64]uint64)

var sourceMap map[uint64][]string

if config.KeepSource {
sourceMap = make(map[uint64][]string)
}

digestChannel := make(chan []Digest, bufferSize*maxProcs)
errorChannel := make(chan error)
defer close(errorChannel)
Expand All @@ -94,14 +100,18 @@ func Create(config *Config) (map[uint64]uint64, error) {
for digests := range digestChannel {
for _, digest := range digests {
output[digest.Key] = digest.Value

if config.KeepSource {
sourceMap[digest.Key] = digest.Source
}
}
}

if err := <-errorChannel; err != nil {
return nil, err
return nil, nil, err
}

return output, nil
return output, sourceMap, nil
}

func readAndProcess(config *Config, reader *csv.Reader, digestChannel chan<- []Digest, errorChannel chan<- error) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ func TestDigestForFile(t *testing.T) {
Key: []int{0},
}

actualDigest, err := digest.Create(testConfig)
actualDigest, sourceMap, err := digest.Create(testConfig)

expectedDigest := map[uint64]uint64{firstKey: firstDigest, secondKey: secondDigest}

assert.NoError(t, err)
assert.Len(t, sourceMap, 0)
assert.Equal(t, expectedDigest, actualDigest)
})

Expand All @@ -70,7 +71,7 @@ func TestDigestForFile(t *testing.T) {
Value: []int{3},
}

actualDigest, err := digest.Create(testConfig)
actualDigest, _, err := digest.Create(testConfig)
expectedDigest := map[uint64]uint64{firstKey: fridayDigest, secondKey: saturdayDigest}

assert.NoError(t, err)
Expand All @@ -84,7 +85,7 @@ func TestDigestForFile(t *testing.T) {
Value: []int{3},
}

actualDigest, err := digest.Create(testConfig)
actualDigest, _, err := digest.Create(testConfig)

assert.Error(t, err)

Expand Down

0 comments on commit f185a04

Please sign in to comment.