Added options and tests for exporters.
This commit is contained in:
@ -12,19 +12,24 @@ import (
|
|||||||
// CSVExporter exports response data as CSV streaming file
|
// CSVExporter exports response data as CSV streaming file
|
||||||
type CSVExporter struct {
|
type CSVExporter struct {
|
||||||
FileName string
|
FileName string
|
||||||
|
Comma rune
|
||||||
|
UseCRLF bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export exports response data as CSV streaming file
|
// Export exports response data as CSV streaming file
|
||||||
func (e *CSVExporter) Export(exports chan interface{}) {
|
func (e *CSVExporter) Export(exports chan interface{}) {
|
||||||
|
|
||||||
// Create file
|
// Create or append file
|
||||||
newFile, err := os.OpenFile(internal.PreferFirst(e.FileName, "out.csv"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
file, err := os.OpenFile(internal.PreferFirst(e.FileName, "out.csv"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Output file creation error: %v\n", err)
|
log.Printf("Output file creation error: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
writer := csv.NewWriter(newFile)
|
writer := csv.NewWriter(file)
|
||||||
|
writer.Comma = internal.PreferFirstRune(e.Comma, ',')
|
||||||
|
writer.UseCRLF = e.UseCRLF
|
||||||
|
|
||||||
// Export data as responses came
|
// Export data as responses came
|
||||||
for res := range exports {
|
for res := range exports {
|
||||||
|
16
exporter/csv_test.go
Normal file
16
exporter/csv_test.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package exporter
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCSVExporter_Export(t *testing.T) {
|
||||||
|
ch := make(chan interface{})
|
||||||
|
defer close(ch)
|
||||||
|
|
||||||
|
exporter := &CSVExporter{
|
||||||
|
FileName: "test.out",
|
||||||
|
Comma: ';',
|
||||||
|
}
|
||||||
|
go exporter.Export(ch)
|
||||||
|
|
||||||
|
ch <- []string{"1", "2"}
|
||||||
|
}
|
@ -11,20 +11,24 @@ import (
|
|||||||
type JSONExporter struct {
|
type JSONExporter struct {
|
||||||
FileName string
|
FileName string
|
||||||
EscapeHTML bool
|
EscapeHTML bool
|
||||||
|
Prefix string
|
||||||
|
Indent string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export exports response data as JSON streaming file
|
// Export exports response data as JSON streaming file
|
||||||
func (e *JSONExporter) Export(exports chan interface{}) {
|
func (e *JSONExporter) Export(exports chan interface{}) {
|
||||||
|
|
||||||
// Create file
|
// Create or append file
|
||||||
newFile, err := os.OpenFile(internal.PreferFirst(e.FileName, "out.json"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
file, err := os.OpenFile(internal.PreferFirst(e.FileName, "out.json"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Output file creation error: %v\n", err)
|
log.Printf("Output file creation error: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
encoder := json.NewEncoder(newFile)
|
encoder := json.NewEncoder(file)
|
||||||
encoder.SetEscapeHTML(e.EscapeHTML)
|
encoder.SetEscapeHTML(e.EscapeHTML)
|
||||||
|
encoder.SetIndent(e.Prefix, e.Indent)
|
||||||
|
|
||||||
// Export data as responses came
|
// Export data as responses came
|
||||||
for res := range exports {
|
for res := range exports {
|
||||||
|
16
exporter/json_test.go
Normal file
16
exporter/json_test.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package exporter
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestJSONExporter_Export(t *testing.T) {
|
||||||
|
ch := make(chan interface{})
|
||||||
|
defer close(ch)
|
||||||
|
|
||||||
|
exporter := &JSONExporter{
|
||||||
|
FileName: "test.json",
|
||||||
|
Indent: " ",
|
||||||
|
}
|
||||||
|
go exporter.Export(ch)
|
||||||
|
|
||||||
|
ch <- map[string]string{"key": "value"}
|
||||||
|
}
|
@ -8,6 +8,14 @@ func PreferFirst(first string, second string) string {
|
|||||||
return second
|
return second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PreferFirstRune returns first non-empty rune
|
||||||
|
func PreferFirstRune(first rune, second rune) rune {
|
||||||
|
if first != 0 {
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
return second
|
||||||
|
}
|
||||||
|
|
||||||
// Contains checks whether []string Contains string
|
// Contains checks whether []string Contains string
|
||||||
func Contains(s []string, e string) bool {
|
func Contains(s []string, e string) bool {
|
||||||
for _, a := range s {
|
for _, a := range s {
|
||||||
|
Reference in New Issue
Block a user