Skip to content

Commit

Permalink
feat: Skip rows and ignore superfluous columns
Browse files Browse the repository at this point in the history
Additionally, we jump to GO 1.23
  • Loading branch information
dploeger committed Oct 29, 2024
1 parent c9076e3 commit 23fa6ba
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.23

- name: Build
run: go build -o icarus cmd/icarus.go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.23

- name: Test
run: go test -v ./...
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dploeger/icarus/v2

go 1.21
go 1.23

require (
github.com/MakeNowJust/heredoc v1.0.0
Expand Down
13 changes: 10 additions & 3 deletions internal/converteradapters/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package converteradapters

import (
"fmt"
"github.com/akamensky/argparse"
"github.com/dploeger/icarus/v2/pkg/converters"
"github.com/emersion/go-ical"
"os"
"strings"
"time"

"github.com/akamensky/argparse"
"github.com/dploeger/icarus/v2/pkg/converters"
"github.com/emersion/go-ical"
)

type CSVConverterAdapter struct {
Expand All @@ -17,6 +18,7 @@ type CSVConverterAdapter struct {
hasHeaders *bool
headers *[]string
location *string
skipRows *int
}

var _ ConverterAdapter = &CSVConverterAdapter{}
Expand Down Expand Up @@ -46,6 +48,10 @@ func (c *CSVConverterAdapter) Initialize(parser *argparse.Parser) (*argparse.Com
Help: "Assume the location for all timestamps",
Default: "UTC",
})
c.skipRows = command.Int("K", "skip-rows", &argparse.Options{
Help: "Skip the number of rows before starting to parse",
Default: 0,
})
return command, nil
}

Expand All @@ -69,6 +75,7 @@ func (c *CSVConverterAdapter) Convert(input *os.File, output *ical.Calendar) err
HasHeaders: *c.hasHeaders,
Headers: *c.headers,
Location: location,
SkipRows: *c.skipRows,
}

return converter.Convert(input, output)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
stdin example.csv
exec icarus convertCSV -H -K 1
! stderr .
stdout 'SUMMARY:Test'
stdout 'DTSTART:20240826T122400Z'
stdout 'DTSTART:20240826T122400Z'
stdout 'DTEND:20240826T132400Z'

-- example.csv --
BOGUS
DTSTART,DTEND,SUMMARY
2024-08-26T12:24:00Z,2024-08-26T13:24:00Z,Test
16 changes: 13 additions & 3 deletions pkg/converters/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package converters
import (
"encoding/csv"
"fmt"
"github.com/emersion/go-ical"
"github.com/google/uuid"
"golang.org/x/exp/maps"
"io"
"slices"
"time"

"github.com/emersion/go-ical"
"github.com/google/uuid"
"golang.org/x/exp/maps"
)

// CSVConverter converts an incoming CSV formatted file into a Calendar object
Expand All @@ -19,6 +20,7 @@ type CSVConverter struct {
HasHeaders bool
Headers []string
Location *time.Location
SkipRows int
}

var _ BaseConverter = &CSVConverter{}
Expand All @@ -27,6 +29,11 @@ func (c *CSVConverter) Convert(input io.Reader, output *ical.Calendar) error {
reader := csv.NewReader(input)
reader.Comma = ([]rune(c.Separator))[0]

for range c.SkipRows {
reader.Read()
reader.FieldsPerRecord = 0
}

var headers []string

if c.HasHeaders {
Expand All @@ -47,6 +54,9 @@ func (c *CSVConverter) Convert(input io.Reader, output *ical.Calendar) error {
for _, row := range rows {
fieldValues := make(map[string]string)
for i, col := range row {
if i >= len(headers) {
continue
}
var field string
if slices.Contains(maps.Keys(c.FieldMap), headers[i]) {
field = c.FieldMap[headers[i]]
Expand Down
44 changes: 41 additions & 3 deletions pkg/converters/csv_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package converters

import (
"github.com/MakeNowJust/heredoc"
"github.com/emersion/go-ical"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/emersion/go-ical"
"github.com/stretchr/testify/assert"
)

func TestConvert(t *testing.T) {
Expand Down Expand Up @@ -218,3 +219,40 @@ func TestConvertLocation(t *testing.T) {
dtEnd, _ := event.Props.Get(ical.PropDateTimeEnd).DateTime(cetLocation)
assert.Equal(t, "2024-08-26 13:24", dtEnd.Format("2006-01-02 15:04"), "Wrong end time")
}

func TestConvertSkipRow(t *testing.T) {
startTime, _ := time.Parse(time.RFC3339, "2024-08-26T12:24:00Z")
endTime, _ := time.Parse(time.RFC3339, "2024-08-26T13:24:00Z")

subject := heredoc.Docf(`
BOGUS
DTSTART,DTEND,SUMMARY
%s,%s,Test
`,
startTime.Format(time.RFC3339),
endTime.Format(time.RFC3339),
)

converter := CSVConverter{
Separator: ",",
TimestampFormat: time.RFC3339,
FieldMap: nil,
HasHeaders: true,
Headers: nil,
Location: time.UTC,
SkipRows: 1,
}

calendar := ical.NewCalendar()

err := converter.Convert(strings.NewReader(subject), calendar)
assert.NoError(t, err, "Converter errored out")
assert.Len(t, calendar.Children, 1, "Invalid number of children in calendar")
event := calendar.Children[0]
summary, _ := event.Props.Get(ical.PropSummary).Text()
assert.Equal(t, "Test", summary)
dtStart, _ := event.Props.Get(ical.PropDateTimeStart).DateTime(startTime.Location())
assert.Equal(t, startTime, dtStart, "Wrong start time")
dtEnd, _ := event.Props.Get(ical.PropDateTimeEnd).DateTime(endTime.Location())
assert.Equal(t, endTime, dtEnd, "Wrong end time")
}

0 comments on commit 23fa6ba

Please sign in to comment.