Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinkarthik committed Apr 23, 2018
1 parent c00907c commit 57be4b9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
config = Config{}
}

// Config is to store all command line Flags.
type Config struct {
PrimaryKeyPositions []int
ValueColumnPositions []int
Expand All @@ -23,32 +24,38 @@ type Config struct {
Modifications string
}

// GetPrimaryKeys is to return the --primary-key flags as digest.Positions array.
func (c *Config) GetPrimaryKeys() digest.Positions {
if len(c.PrimaryKeyPositions) > 0 {
return c.PrimaryKeyPositions
}
return []int{0}
}

// GetValueColumns is to return the --value-columns flags as digest.Positions array.
func (c *Config) GetValueColumns() digest.Positions {
if len(c.ValueColumnPositions) > 0 {
return c.ValueColumnPositions
}
return []int{}
}

// GetBaseReader returns an io.Reader for the base file.
func (c *Config) GetBaseReader() io.Reader {
return getReader(c.Base)
}

// GetDeltaReader returns an io.Reader for the delta file.
func (c *Config) GetDeltaReader() io.Reader {
return getReader(c.Delta)
}

// AdditionsWriter gives the output stream for the additions in delta csv.
func (c *Config) AdditionsWriter() io.WriteCloser {
return getWriter(c.Additions)
}

// ModificationsWriter gives the output stream for the modifications in delta csv.
func (c *Config) ModificationsWriter() io.WriteCloser {
return getWriter(c.Modifications)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/digest/compare.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package digest

// Compare compares two Digest maps and returns the additions and modification
// keys as arrays.
func Compare(baseDigest, newDigest map[uint64]uint64) (additions []uint64, modifications []uint64) {
maxSize := len(newDigest)
additions = make([]uint64, maxSize)
Expand Down
7 changes: 7 additions & 0 deletions pkg/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cespare/xxhash"
)

// Separator for CSV. Not configurable for now.
const Separator = ","

// Digest represents the binding of the key of each csv line
Expand All @@ -29,6 +30,8 @@ func CreateDigest(csv []string, pKey Positions, pRow Positions) Digest {

}

// Config represents configurations that can be passed
// to create a Digest.
type Config struct {
KeyPositions []int
Key Positions
Expand All @@ -38,6 +41,7 @@ type Config struct {
SourceMap bool
}

// NewConfig creates an instance of Config struct.
func NewConfig(r io.Reader, createSourceMap bool, primaryKey Positions, valueColumns Positions) *Config {
return &Config{
Reader: r,
Expand All @@ -47,6 +51,9 @@ func NewConfig(r io.Reader, createSourceMap bool, primaryKey Positions, valueCol
}
}

// 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, map[uint64]string, error) {
reader := csv.NewReader(config.Reader)

Expand Down
7 changes: 7 additions & 0 deletions pkg/digest/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package digest

import "strings"

// Positions represents positions of columns in a CSV array.
type Positions []int

// MapToValue plucks the values from CSV from
// their respective positions and concatenates
// them using Separator as a string.
func (p Positions) MapToValue(csv []string) string {
if p.Length() == 0 {
return strings.Join(csv, Separator)
Expand All @@ -15,10 +19,13 @@ func (p Positions) MapToValue(csv []string) string {
return strings.Join(output, Separator)
}

// Length returns the size of the Positions array.
func (p Positions) Length() int {
return len([]int(p))
}

// Items returns the elements of the Positions array
// as an array of int
func (p Positions) Items() []int {
return []int(p)
}

0 comments on commit 57be4b9

Please sign in to comment.