Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support comment in csv #304

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/basic/basic.int.delete.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: DELETE
Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.int.insert.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
id:
Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.int.update.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: UPDATE
Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.string.delete.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: DELETE
Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.string.insert.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
id:
Expand Down
1 change: 1 addition & 0 deletions examples/basic/basic.string.update.v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: UPDATE
Expand Down
3 changes: 3 additions & 0 deletions examples/basic/person.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# I'm comment
# I'm comment
933|Mahinda|Perera|male|1989-12-03|2010-02-14T15:32:10|119.235.7.103|Firefox
# I'm comment
1129|Carmen|Lepland|female|1984-02-18|2010-01-28T06:39:58|195.20.151.175|Internet Explorer
2199023256684|A.|Rao|female|1985-08-02|2010-04-23T22:52:26|49.202.188.25|Firefox
4398046512167|Gustavo|Arbelaez|male|1986-11-02|2010-06-16T20:53:47|190.96.189.165|Chrome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: DELETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: UPDATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: DELETE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sources:
- path: ./person.csv
csv:
delimiter: "|"
comment: "#"
tags:
- name: Person
mode: UPDATE
Expand Down
3 changes: 3 additions & 0 deletions integration-testing/testdata/basic/person.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# I'm comment
# I'm comment
933|Mahinda|Perera|male|1989-12-03|2010-02-14T15:32:10|119.235.7.103|Firefox
# I'm comment
1129|Carmen|Lepland|female|1984-02-18|2010-01-28T06:39:58|195.20.151.175|Internet Explorer
2199023256684|A.|Rao|female|1985-08-02|2010-04-23T22:52:26|49.202.188.25|Firefox
4398046512167|Gustavo|Arbelaez|male|1986-11-02|2010-06-16T20:53:47|190.96.189.165|Chrome
Expand Down
3 changes: 3 additions & 0 deletions pkg/reader/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func NewCSVReader(s source.Source) RecordReader {
if chars := []rune(c.CSV.Delimiter); len(chars) > 0 {
cr.Comma = chars[0]
}
if chars := []rune(c.CSV.Comment); len(chars) > 0 {
cr.Comment = chars[0]
}
cr.LazyQuotes = c.CSV.LazyQuotes

h.withHeader = c.CSV.WithHeader
Expand Down
13 changes: 7 additions & 6 deletions pkg/reader/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var _ = Describe("csvReader", func() {
},
CSV: &source.CSVConfig{
Delimiter: "|",
Comment: "#",
},
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -103,32 +104,32 @@ var _ = Describe("csvReader", func() {
r := NewCSVReader(s)
nBytes, err = r.Size()
Expect(err).NotTo(HaveOccurred())
Expect(nBytes).To(Equal(int64(33)))
Expect(nBytes).To(Equal(int64(117)))

n, record, err = r.Read()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(6))
Expect(n).To(Equal(20))
Expect(record).To(Equal(spec.Record{"1", "2", "3"}))

n, record, err = r.Read()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(7))
Expect(n).To(Equal(35))
Expect(record).To(Equal(spec.Record{"4", " 5", "6"}))

n, record, err = r.Read()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(8))
Expect(n).To(Equal(22))
Expect(record).To(Equal(spec.Record{" 7", "8", " 9"}))

n, record, err = r.Read()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(12))
Expect(n).To(Equal(26))
Expect(record).To(Equal(spec.Record{"10", " 11 ", " 12"}))

n, record, err = r.Read()
Expect(err).To(HaveOccurred())
Expect(stderrors.Is(err, io.EOF)).To(BeTrue())
Expect(n).To(Equal(0))
Expect(n).To(Equal(14))
Expect(record).To(BeEmpty())
})
})
Expand Down
6 changes: 6 additions & 0 deletions pkg/reader/testdata/local_delimiter.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# I'm comment
1|2|3
# I'm comment
# I'm comment
4| 5|6
# I'm comment
7|8| 9
# I'm comment
10| 11 | 12
# I'm comment
1 change: 1 addition & 0 deletions pkg/source/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type (

CSVConfig struct {
Delimiter string `yaml:"delimiter,omitempty"`
Comment string `yaml:"comment,omitempty"`
WithHeader bool `yaml:"withHeader,omitempty"`
LazyQuotes bool `yaml:"lazyQuotes,omitempty"`
}
Expand Down
Loading