Skip to content

Commit

Permalink
Add support for ignore-columns flag as an inverse of columns flag (fixes
Browse files Browse the repository at this point in the history
 #23)
  • Loading branch information
aswinkarthik committed Oct 8, 2019
1 parent 36438e4 commit 32f4e66
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
25 changes: 25 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func NewContext(
return nil, fmt.Errorf("base-file and delta-file columns count do not match")
}

if len(ignoreValueColumnPositions) > 0 && len(valueColumnPositions) > 0 {
return nil, fmt.Errorf("only one of --columns or --ignore-columns")
}
if len(ignoreValueColumnPositions) > 0 {
valueColumnPositions = inferValueColumns(baseRecordCount, ignoreValueColumnPositions)
}

baseFile, err := fs.Open(baseFilename)
if err != nil {
return nil, err
Expand Down Expand Up @@ -140,6 +147,24 @@ func (c *Context) validate() error {
return nil
}

func inferValueColumns(recordCount int, ignoreValueColumns []int) digest.Positions {
lookupMap := make(map[int]struct{})
for _, pos := range ignoreValueColumns {
lookupMap[pos] = struct{}{}
}

valueColumns := make(digest.Positions, 0)
if len(ignoreValueColumns) > 0 {
for i := 0; i < recordCount; i++ {
if _, exists := lookupMap[i]; !exists {
valueColumns = append(valueColumns, i)
}
}
}

return valueColumns
}

func assertAll(elements []int, assertFn func(element int) bool) bool {
for _, el := range elements {
if !assertFn(el) {
Expand Down
62 changes: 56 additions & 6 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ func TestNewContext(t *testing.T) {
assert.EqualError(t, err, "base-file and delta-file columns count do not match")
})
})

t.Run("should pass only one of columns or ignore columns", func(t *testing.T) {
fs := afero.NewMemMapFs()
setupFiles(t, fs)

_, err := cmd.NewContext(
fs,
nil,
[]int{0},
[]int{0},
nil,
"jSOn",
"/base.csv",
"/delta.csv",
)

assert.EqualError(t, err, "only one of --columns or --ignore-columns")
})
}

func TestConfig_DigestConfig(t *testing.T) {
Expand All @@ -326,17 +344,49 @@ func TestConfig_DigestConfig(t *testing.T) {

assert.NoError(t, err)
assert.NotNil(t, baseConfig.Reader)
assert.Equal(t, baseConfig.Value, valueColumns)
assert.Equal(t, baseConfig.Key, primaryColumns)
assert.Equal(t, baseConfig.Include, includeColumns)
assert.Equal(t, valueColumns, baseConfig.Value)
assert.Equal(t, primaryColumns, baseConfig.Key)
assert.Equal(t, includeColumns, baseConfig.Include)

deltaConfig, err := ctx.DeltaDigestConfig()

assert.NoError(t, err)
assert.NotNil(t, deltaConfig.Reader)
assert.Equal(t, valueColumns, deltaConfig.Value)
assert.Equal(t, primaryColumns, deltaConfig.Key)
assert.Equal(t, includeColumns, deltaConfig.Include)
})
t.Run("should infer values columns as inverse of ignore columns digest ctx", func(t *testing.T) {
fs := afero.NewMemMapFs()
setupFiles(t, fs)

ignoreValueColumns := digest.Positions{0, 1, 2}
primaryColumns := digest.Positions{0, 1}
ctx, err := cmd.NewContext(
fs,
primaryColumns,
nil,
ignoreValueColumns,
nil,
"jSOn",
"/base.csv",
"/delta.csv",
)
assert.NoError(t, err)

baseConfig, err := ctx.BaseDigestConfig()

assert.NoError(t, err)
assert.NotNil(t, baseConfig.Reader)
assert.Equal(t, digest.Positions{3}, baseConfig.Value)
assert.Equal(t, primaryColumns, baseConfig.Key)

deltaConfig, err := ctx.DeltaDigestConfig()

assert.NoError(t, err)
assert.NotNil(t, deltaConfig.Reader)
assert.Equal(t, deltaConfig.Value, valueColumns)
assert.Equal(t, deltaConfig.Key, primaryColumns)
assert.Equal(t, deltaConfig.Include, includeColumns)
assert.Equal(t, digest.Positions{3}, deltaConfig.Value)
assert.Equal(t, primaryColumns, deltaConfig.Key)
})
}
func setupFiles(t *testing.T, fs afero.Fs) {
Expand Down

0 comments on commit 32f4e66

Please sign in to comment.