Skip to content

Commit

Permalink
Add support for CSV and TSV
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Apr 24, 2019
1 parent 43eae16 commit 46d4e17
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
45 changes: 45 additions & 0 deletions internal/matchers/text_csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package matchers

import (
"bytes"
"encoding/csv"
"io"
)

// Csv matches a comma-separated values file.
func Csv(in []byte) bool {
return sv(in, ',')
}

// Tsv matches a tab-separated values file.
func Tsv(in []byte) bool {
return sv(in, '\t')
}

func sv(in []byte, comma rune) bool {
r := csv.NewReader(butLastLineReader(in, ReadLimit))
r.Comma = comma
r.TrimLeadingSpace = true
r.LazyQuotes = true

lines, err := r.ReadAll()
return err == nil && r.FieldsPerRecord > 1 && len(lines) > 1
}

// butLastLineReader returns a reader to the provided byte slice.
// the reader is guaranteed to reach EOF before it reads `cutAt` bytes.
// bytes after the last newline are dropped from the input.
func butLastLineReader(in []byte, cutAt int) io.Reader {
if len(in) >= cutAt {
for i := cutAt - 1; i > 0; i-- {
if in[i] == '\n' {
return bytes.NewReader(in[:i])
}
}

// no newline was found between the 0 index and cutAt
return bytes.NewReader(in[:cutAt])
}

return bytes.NewReader(in)
}
2 changes: 2 additions & 0 deletions mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ var files = map[string]*node{
"a.json": json,
"a.geojson": geoJson,
"b.geojson": geoJson,
"a.csv": csv,
"a.tsv": tsv,
"a.rtf": rtf,
"a.js": js,
"a.lua": lua,
Expand Down
4 changes: 3 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 73 Supported MIME types
## 75 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type
Expand Down Expand Up @@ -66,6 +66,8 @@ Extension | MIME type
**geojson** | application/geo+json
**rtf** | text/rtf
**tcl** | text/x-tcl
**csv** | text/csv
**tsv** | text/tab-separated-values
**gz** | application/gzip
**class** | application/x-java-applet; charset=binary
**swf** | application/x-shockwave-flash
Expand Down
5 changes: 3 additions & 2 deletions testdata/a.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
number,spid,bill_id,cnam_caller,cnam_gns,route_id,type,comments
15550400000,,,,,,,
Start Date ,Start Time,End Date,End Time,Event Title ,All Day Event,No End Time,Event Description,Contact ,Contact Email,Contact Phone,Location,Category,Mandatory,Registration,Maximum,Last Date To Register
9/5/2011,3:00:00 PM,9/5/2011,,Social Studies Dept. Meeting,N,Y,Department meeting,Chris Gallagher,[email protected],814-555-5179,High School,2,N,N,25,9/2/2011
9/5/2011,6:00:00 PM,9/5/2011,8:00:00 PM,Curriculum Meeting,N,N,Curriculum Meeting,Chris Gallagher,[email protected],814-555-5179,High School,2,N,N,25,9/2/2011
39 changes: 39 additions & 0 deletions testdata/a.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
help direct great nearly chair realize
probably examine steady serve would reason
sang about pair dirty heavy reader
production increase chance contrast my nine
best dropped arrow victory tribe knowledge
lie should sort applied talk smallest
world rear product seeing substance tank
ahead sale unusual closely importance fed
away close ran slip bridge protection
fall parent flew syllable offer motor
by valuable cause herself plain flame
strip nails carbon island college canal
organization paragraph heat directly throughout mouth
cook purpose speed everywhere leaving tobacco
flower friendly rising clear rhyme partly
least gather sand milk hearing mother
broken design read shinning bottom wish
similar parent spring care east else
rubber slowly board weight rubber damage
sick mission movement pay route more
upper rule smell post neighbor orbit
have planned quite sitting call merely
medicine ring theory discovery calm this
accident great prove garden buried dropped
jar needed sound driving common medicine
east search create journey mostly gun
aloud race plates black giant throat
fair dinner chicken metal help mark
goes also language position hole save
basic tree palace am hard theory
mad suggest chosen due due most
went double rope recent evidence chair
able twelve hospital road stock stretch
sweet name dirt settlers sheet needle
tell pencil on command onlinetools must
rise milk read hour death valley
amount determine massage pack image disappear
suppose tell club national chair somehow
involved author size cotton half apple
4 changes: 3 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ var (
ogg = newNode("application/ogg", "ogg", matchers.Ogg)

txt = newNode("text/plain", "txt", matchers.Txt,
html, svg, xml, php, js, lua, perl, python, json, rtf, tcl)
html, svg, xml, php, js, lua, perl, python, json, rtf, tcl, csv, tsv)
xml = newNode("text/xml; charset=utf-8", "xml", matchers.Xml,
x3d, kml, collada, gml, gpx, tcx)
json = newNode("application/json", "json", matchers.Json, geoJson)
csv = newNode("text/csv", "csv", matchers.Csv)
tsv = newNode("text/tab-separated-values", "tsv", matchers.Tsv)
geoJson = newNode("application/geo+json", "geojson", matchers.GeoJson)
html = newNode("text/html; charset=utf-8", "html", matchers.Html)
php = newNode("text/x-php; charset=utf-8", "php", matchers.Php)
Expand Down

0 comments on commit 46d4e17

Please sign in to comment.