Skip to content

Commit

Permalink
Merge pull request #805 from hairyhenderson/let-csv-read-more-input-t…
Browse files Browse the repository at this point in the history
…ypes-804

Allow data.ToCSV to handle more general input types
  • Loading branch information
Dave Henderson authored and GitHub committed Apr 10, 2020
2 parents 6bad577 + 07a0792 commit abce277
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
24 changes: 20 additions & 4 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/Shopify/ejson"
ejsonJson "github.com/Shopify/ejson/json"
"github.com/hairyhenderson/gomplate/v3/conv"
"github.com/hairyhenderson/gomplate/v3/env"

// XXX: replace once https://github.com/BurntSushi/toml/pull/179 is merged
Expand Down Expand Up @@ -272,10 +273,25 @@ func ToCSV(args ...interface{}) (string, error) {
args = args[1:]
}
if len(args) == 1 {
var ok bool
in, ok = args[0].([][]string)
if !ok {
return "", errors.Errorf("Can't parse ToCSV input - must be of type [][]string")
switch a := args[0].(type) {
case [][]string:
in = a
case [][]interface{}:
in = make([][]string, len(a))
for i, v := range a {
in[i] = conv.ToStrings(v...)
}
case []interface{}:
in = make([][]string, len(a))
for i, v := range a {
ar, ok := v.([]interface{})
if !ok {
return "", errors.Errorf("Can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0])
}
in[i] = conv.ToStrings(ar...)
}
default:
return "", errors.Errorf("Can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0])
}
}
b := &bytes.Buffer{}
Expand Down
18 changes: 18 additions & 0 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@ func TestToCSV(t *testing.T) {

_, err = ToCSV([][]int{{1, 2}})
assert.Error(t, err)

expected = "first,second,third\r\n1,2,3\r\n4,5,6\r\n"
out, err = ToCSV([][]interface{}{
{"first", "second", "third"},
{"1", "2", "3"},
{"4", "5", "6"},
})
assert.NoError(t, err)
assert.Equal(t, expected, out)

expected = "first|second|third\r\n1|2|3\r\n4|5|6\r\n"
out, err = ToCSV("|", []interface{}{
[]interface{}{"first", "second", "third"},
[]interface{}{1, "2", 3},
[]interface{}{"4", 5, "6"},
})
assert.NoError(t, err)
assert.Equal(t, expected, out)
}

func TestTOML(t *testing.T) {
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/docs_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//+build integration

package integration

import (
. "gopkg.in/check.v1"

"gotest.tools/v3/fs"
"gotest.tools/v3/icmd"
)

// This suite contains integration tests to make sure that (some of) the examples
// in the gomplate docs work correctly
type DocExamplesSuite struct {
tmpDir *fs.Dir
}

var _ = Suite(&DocExamplesSuite{})

func (s *DocExamplesSuite) SetUpSuite(c *C) {
s.tmpDir = fs.NewDir(c, "gomplate-inttests")
}

func (s *DocExamplesSuite) TearDownSuite(c *C) {
s.tmpDir.Remove()
}

func (s *DocExamplesSuite) TestDataExamples(c *C) {
result := icmd.RunCommand(GomplateBin,
"-i", "{{ $rows := (jsonArray `[[\"first\",\"second\"],[\"1\",\"2\"],[\"3\",\"4\"]]`) }}{{ data.ToCSV \";\" $rows }}",
)
expected := "first;second\r\n1;2\r\n3;4\r\n"
result.Assert(c, icmd.Expected{ExitCode: 0, Out: expected})
}

0 comments on commit abce277

Please sign in to comment.