-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da5b623
commit b89cb05
Showing
5 changed files
with
108 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aswinkarthik93/csvdiff/pkg/digest" | ||
) | ||
|
||
// Formatter defines the interface through which differences | ||
// can be formatted and displayed | ||
type Formatter interface { | ||
Format(digest.Difference) | ||
Format(digest.Difference, io.Writer) | ||
} | ||
|
||
// StdoutFormatter formats diff to STDOUT | ||
type StdoutFormatter struct{} | ||
// RowMarkFormatter formats diff by marking each row as | ||
// ADDED/MODIFIED. It mutates the row and adds as a new column. | ||
type RowMarkFormatter struct{} | ||
|
||
// Format prints the diff to os.Stdout | ||
func (f *StdoutFormatter) Format(diff digest.Difference) { | ||
fmt.Printf("Additions %d\n", len(diff.Additions)) | ||
fmt.Printf("Modifications %d\n", len(diff.Modifications)) | ||
fmt.Println("Rows:") | ||
func (f *RowMarkFormatter) Format(diff digest.Difference, w io.Writer) { | ||
fmt.Fprintf(w, "Additions %d\n", len(diff.Additions)) | ||
fmt.Fprintf(w, "Modifications %d\n", len(diff.Modifications)) | ||
fmt.Fprintf(w, "Rows:\n") | ||
|
||
for _, added := range diff.Additions { | ||
fmt.Printf("%s,%s\n", added, "ADDED") | ||
fmt.Fprintf(w, "%s,%s\n", added, "ADDED") | ||
} | ||
|
||
for _, modified := range diff.Modifications { | ||
fmt.Printf("%s,%s\n", modified, "MODIFIED") | ||
fmt.Fprintf(w, "%s,%s\n", modified, "MODIFIED") | ||
} | ||
} | ||
|
||
// JSONFormatter formats diff to as a JSON Object | ||
type JSONFormatter struct{} | ||
|
||
// Format prints the diff as a JSON | ||
func (f *JSONFormatter) Format(diff digest.Difference, w io.Writer) { | ||
data, err := json.MarshalIndent(diff, "", " ") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
w.Write(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/aswinkarthik93/csvdiff/cmd" | ||
"github.com/aswinkarthik93/csvdiff/pkg/digest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJSONFormat(t *testing.T) { | ||
var formatter cmd.Formatter | ||
diff := digest.Difference{ | ||
Additions: []string{"additions"}, | ||
Modifications: []string{"modification"}, | ||
} | ||
expected := `{ | ||
"Additions": [ | ||
"additions" | ||
], | ||
"Modifications": [ | ||
"modification" | ||
] | ||
}` | ||
|
||
var buffer bytes.Buffer | ||
|
||
formatter = &cmd.JSONFormatter{} | ||
|
||
formatter.Format(diff, &buffer) | ||
assert.Equal(t, expected, buffer.String()) | ||
} | ||
|
||
func TestRowMarkFormatter(t *testing.T) { | ||
var formatter cmd.Formatter | ||
diff := digest.Difference{ | ||
Additions: []string{"additions"}, | ||
Modifications: []string{"modification"}, | ||
} | ||
expected := `Additions 1 | ||
Modifications 1 | ||
Rows: | ||
additions,ADDED | ||
modification,MODIFIED | ||
` | ||
|
||
var buffer bytes.Buffer | ||
|
||
formatter = &cmd.RowMarkFormatter{} | ||
|
||
formatter.Format(diff, &buffer) | ||
assert.Equal(t, expected, buffer.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters