Parse CSV files into HTML tables

This commit is contained in:
Thomas Miceli
2023-03-19 03:18:56 +01:00
parent 11b3eed250
commit 858ee3e70a
5 changed files with 108 additions and 13 deletions

View File

@ -3,8 +3,11 @@ package git
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io"
"regexp"
"strings"
)
type File struct {
@ -16,6 +19,12 @@ type File struct {
IsDeleted bool
}
type CsvFile struct {
File
Header []string
Rows [][]string
}
type Commit struct {
Hash string
Author string
@ -152,3 +161,27 @@ func parseLog(out io.Reader) []*Commit {
return commits
}
func ParseCsv(file *File) (*CsvFile, error) {
reader := csv.NewReader(strings.NewReader(file.Content))
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
header := records[0]
numColumns := len(header)
for i := 1; i < len(records); i++ {
if len(records[i]) != numColumns {
return nil, fmt.Errorf("CSV file has invalid row at index %d", i)
}
}
return &CsvFile{
File: *file,
Header: header,
Rows: records[1:],
}, nil
}