Skip to content

Commit f0066f9

Browse files
added BOM support (#26)
Now skipping the BOM if it exists
1 parent 1e4d858 commit f0066f9

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

csv_files/valid_bom.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
header1, header2, header3
2+
line1, 1, 1.2
3+
line2, 2, 2.3
4+
line3, 3, 3.4

load.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csvtag
22

33
import (
4+
"bytes"
45
"encoding/csv"
56
"fmt"
67
"io"
@@ -113,6 +114,8 @@ func LoadFromString(str string, destination interface{}, options ...CsvOptions)
113114
// @param separator: the separator used in the csv file.
114115
// @param header: the optional header if the file does not contain one.
115116
func readFile(file io.Reader, separator rune, header []string) ([]string, [][]string, error) {
117+
file = skipBOM(file)
118+
116119
// Create and configure the csv reader.
117120
reader := csv.NewReader(file)
118121
reader.TrimLeadingSpace = true
@@ -141,6 +144,24 @@ func readFile(file io.Reader, separator rune, header []string) ([]string, [][]st
141144
return header, content, nil
142145
}
143146

147+
// Skip the Byte Order Mark (BOM) if it exists.
148+
// @param file: the io.Reader to read from.
149+
func skipBOM(file io.Reader) io.Reader {
150+
// Read the first 3 bytes.
151+
bom := make([]byte, 3)
152+
_, err := file.Read(bom)
153+
if err != nil {
154+
return file
155+
}
156+
157+
// If the first 3 bytes are not the BOM, reset the reader.
158+
if bom[0] != 0xEF || bom[1] != 0xBB || bom[2] != 0xBF {
159+
return io.MultiReader(bytes.NewReader(bom), file)
160+
}
161+
162+
return file
163+
}
164+
144165
// Map the provided content to the destination using the header and the tags.
145166
// @param header: the csv header to match with the struct's tags.
146167
// @param content: the content to put in destination.

load_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func TestValideFile(t *testing.T) {
5555
}
5656
}
5757

58+
func TestValideFileWithBOM(t *testing.T) {
59+
tabT := []test{}
60+
err := LoadFromPath("csv_files/valid_bom.csv", &tabT)
61+
if err != nil || checkValues(tabT) {
62+
t.Fail()
63+
}
64+
}
65+
5866
func TestBool(t *testing.T) {
5967
tabT := []testBool{}
6068
err := LoadFromPath("csv_files/bool.csv", &tabT)

0 commit comments

Comments
 (0)