Skip to content

Commit

Permalink
Add NewFromCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed May 12, 2021
1 parent 582f69e commit 07eed68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions document/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,15 @@ func (s sliceArray) GetByIndex(i int) (Value, error) {

return NewValue(v.Interface())
}

// NewFromCSV takes a list of headers and columns and returns a document.
// Each header will be assigned as the key and each corresponding column as a text value.
// The length of headers and columns must be the same.
func NewFromCSV(headers, columns []string) Document {
fb := NewFieldBuffer()
for i, h := range headers {
fb.Add(h, NewTextValue(columns[i]))
}

return fb
}
9 changes: 9 additions & 0 deletions document/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/testutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -141,3 +142,11 @@ func BenchmarkJSONToDocument(b *testing.B) {
})
}
}

func TestNewFromCSV(t *testing.T) {
headers := []string{"a", "b", "c"}
columns := []string{"A", "B", "C"}

d := document.NewFromCSV(headers, columns)
testutil.RequireDocJSONEq(t, d, `{"a": "A", "b": "B", "c": "C"}`)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/buger/jsonparser v1.1.1
github.com/google/btree v1.0.0
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/go-cmp v0.5.5
github.com/stretchr/testify v1.7.0
github.com/vmihailenco/msgpack/v5 v5.1.4
go.etcd.io/bbolt v1.3.5
Expand Down

0 comments on commit 07eed68

Please sign in to comment.