-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSV is a classical input format.
- Loading branch information
Guilhem Bonnefille
committed
Nov 14, 2024
1 parent
2fe3ddd
commit 14e409e
Showing
8 changed files
with
221 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package config | ||
|
||
import "testing" | ||
|
||
func TestIsYaml(t *testing.T) { | ||
fs := Fs{} | ||
if !fs.IsYaml("file.yaml") { | ||
t.Error("file.yaml should be yaml") | ||
} | ||
if !fs.IsYaml("file.yml") { | ||
t.Error("file.yml should be yaml") | ||
} | ||
if fs.IsYaml("file.json") { | ||
t.Error("file.json should not be yaml") | ||
} | ||
} | ||
|
||
func TestIsCsv(t *testing.T) { | ||
fs := Fs{} | ||
if !fs.IsCsv("file.csv") { | ||
t.Error("file.csv should be csv") | ||
} | ||
if fs.IsYaml("file.json") { | ||
t.Error("file.json should not be csv") | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package mail | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/rykov/paperboy/config" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
func TestParseRecipientsCsv(t *testing.T) { | ||
aFs := afero.NewMemMapFs() | ||
|
||
// Write and load fake configuration | ||
cPath, _ := filepath.Abs("./recipients.csv") | ||
afero.WriteFile(aFs, cPath, []byte(`email,name,extra | ||
[email protected],J Doe,Extra Data | ||
`), 0644) | ||
|
||
appFs := config.Fs{ | ||
Config: &config.AConfig{ | ||
ConfigFile: config.ConfigFile{ | ||
CSV: config.CSVConfig{ | ||
Comma: ",", | ||
}, | ||
}, | ||
}, | ||
Fs: aFs, | ||
} | ||
|
||
recipients, err := parseRecipients(&appFs, cPath) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(recipients) != 1 { | ||
t.Errorf("Expected 1 recipient, got %d", len(recipients)) | ||
} | ||
} | ||
|
||
func TestParseRecipientsYaml(t *testing.T) { | ||
aFs := afero.NewMemMapFs() | ||
|
||
// Write and load fake configuration | ||
cPath, _ := filepath.Abs("./recipients.yml") | ||
afero.WriteFile(aFs, cPath, []byte(`--- | ||
- email: [email protected] | ||
name: J Doe | ||
extra: Extra Data | ||
`), 0644) | ||
|
||
appFs := config.Fs{ | ||
Config: &config.AConfig{ | ||
ConfigFile: config.ConfigFile{ | ||
CSV: config.CSVConfig{ | ||
Comma: ",", | ||
}, | ||
}, | ||
}, | ||
Fs: aFs, | ||
} | ||
|
||
recipients, err := parseRecipients(&appFs, cPath) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(recipients) != 1 { | ||
t.Errorf("Expected 1 recipient, got %d", len(recipients)) | ||
} | ||
} |
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,51 @@ | ||
package mail | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
"io" | ||
|
||
"github.com/rykov/paperboy/config" | ||
) | ||
|
||
func unmarshalCsvRecipients(appFs *config.Fs, raw []byte) ([]map[string]interface{}, error) { | ||
var data []map[string]interface{} | ||
|
||
// CSV | ||
csvReader := newCSVReader(appFs.Config.ConfigFile.CSV, bytes.NewReader(raw)) | ||
|
||
// Read header | ||
header, err := csvReader.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Read CSV line by line | ||
for { | ||
record, err := csvReader.Read() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Create a map for each record | ||
rec := make(map[string]interface{}) | ||
for i, h := range header { | ||
rec[h] = record[i] | ||
} | ||
|
||
data = append(data, rec) | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func newCSVReader(cfg config.CSVConfig, r io.Reader) *csv.Reader { | ||
csvReader := csv.NewReader(r) | ||
// Deal with separator | ||
if len(cfg.Comma) > 0 { | ||
csvReader.Comma = []rune(cfg.Comma)[0] | ||
} | ||
return csvReader | ||
} |
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,31 @@ | ||
package mail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rykov/paperboy/config" | ||
) | ||
|
||
func TestUnmarshalCsv(t *testing.T) { | ||
content := []byte(`email,name,extra | ||
[email protected],J Doe,Extra Data | ||
`) | ||
|
||
appFs := config.Fs{ | ||
Config: &config.AConfig{ | ||
ConfigFile: config.ConfigFile{ | ||
CSV: config.CSVConfig{ | ||
Comma: ",", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
recipients, err := unmarshalCsvRecipients(&appFs, content) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(recipients) != 1 { | ||
t.Errorf("Expected 1 recipient, got %d", len(recipients)) | ||
} | ||
} |
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,16 @@ | ||
package mail | ||
|
||
import ( | ||
"github.com/ghodss/yaml" | ||
"github.com/rykov/paperboy/config" | ||
) | ||
|
||
func unmarshalYamlRecipients(appFs *config.Fs, raw []byte) ([]map[string]interface{}, error) { | ||
var data []map[string]interface{} | ||
|
||
if err := yaml.Unmarshal(raw, &data); err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |