From ae123e51d6e8ae4b0d402759ce2fb83d26b6658c Mon Sep 17 00:00:00 2001 From: aswinkarthik93 Date: Mon, 23 Apr 2018 21:33:24 +0530 Subject: [PATCH] Add documentation --- cmd/config.go | 7 +++++++ pkg/digest/compare.go | 2 ++ pkg/digest/digest.go | 7 +++++++ pkg/digest/positions.go | 7 +++++++ 4 files changed, 23 insertions(+) diff --git a/cmd/config.go b/cmd/config.go index a30dc64..76c5ac3 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -14,6 +14,7 @@ func init() { config = Config{} } +// Config is to store all command line Flags. type Config struct { PrimaryKeyPositions []int ValueColumnPositions []int @@ -23,6 +24,7 @@ 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 @@ -30,6 +32,7 @@ func (c *Config) GetPrimaryKeys() digest.Positions { 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 @@ -37,18 +40,22 @@ func (c *Config) GetValueColumns() digest.Positions { 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) } diff --git a/pkg/digest/compare.go b/pkg/digest/compare.go index 8c324b9..22f09ff 100644 --- a/pkg/digest/compare.go +++ b/pkg/digest/compare.go @@ -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) diff --git a/pkg/digest/digest.go b/pkg/digest/digest.go index e293826..90a20d1 100644 --- a/pkg/digest/digest.go +++ b/pkg/digest/digest.go @@ -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 @@ -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 @@ -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, @@ -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) diff --git a/pkg/digest/positions.go b/pkg/digest/positions.go index cbbb5ff..e69b2cf 100644 --- a/pkg/digest/positions.go +++ b/pkg/digest/positions.go @@ -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) @@ -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) }