Rename CSV option from comma to delimiter
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 23 Dec 2018 20:08:12 +0000 (21:08 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 23 Dec 2018 20:09:09 +0000 (21:09 +0100)
See #5555

docs/content/en/functions/transform.Unmarshal.md
hugolib/resource_chain_test.go
parser/metadecoders/decoder.go
parser/metadecoders/format.go
tpl/transform/unmarshal.go
tpl/transform/unmarshal_test.go

index 0220de5d56db1083645769d265da1704424938a0..6e33289638fe9bb4d3aa937b587949fcc0daad0d 100644 (file)
@@ -35,8 +35,8 @@ The above prints `Hello Hugo`.
 
 Unmarshal with CSV as input has some options you can set:
 
-comma
-: The delmiter used, default is `,`
+delimiter
+: The delimiter used, default is `,`
 
 comment
 : The comment character ued in the CSV. If set, lines beginning with the comment character without preceding whitespace are ignored.:
index 90022d6dca37c5783e214fbd3cc48289203ab089..551ac017704828d5de2dee9bd7bd65be74b638a0 100644 (file)
@@ -343,12 +343,11 @@ Publish 2: {{ $cssPublish2.Permalink }}
                        b.WithTemplates("home.html", `
 {{ $toml := "slogan = \"Hugo Rocks!\"" | resources.FromString "slogan.toml" | transform.Unmarshal }}
 {{ $csv1 := "\"Hugo Rocks\",\"Hugo is Fast!\"" | resources.FromString "slogans.csv" | transform.Unmarshal }}
-{{ $csv2 := "a;b;c" | transform.Unmarshal (dict "comma" ";") }}
+{{ $csv2 := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }}
 
 Slogan: {{ $toml.slogan }}
 CSV1: {{ $csv1 }} {{ len (index $csv1 0)  }}
-CSV2: {{ $csv2 }}
-
+CSV2: {{ $csv2 }}              
 `)
                }, func(b *sitesBuilder) {
                        b.AssertFileContent("public/index.html",
index 0ca8575fea5abcf399f9cb3a586a50df6ca279b4..2f3c27d4571f6d2e66c4e9665c6ae984d08238a1 100644 (file)
@@ -31,8 +31,8 @@ import (
 
 // Decoder provides some configuration options for the decoders.
 type Decoder struct {
-       // Comma is the field delimiter used in the CSV decoder. It defaults to ','.
-       Comma rune
+       // Delimiter is the field delimiter used in the CSV decoder. It defaults to ','.
+       Delimiter rune
 
        // Comment, if not 0, is the comment character ued in the CSV decoder. Lines beginning with the
        // Comment character without preceding whitespace are ignored.
@@ -41,7 +41,7 @@ type Decoder struct {
 
 // Default is a Decoder in its default configuration.
 var Default = Decoder{
-       Comma: ',',
+       Delimiter: ',',
 }
 
 // UnmarshalToMap will unmarshall data in format f into a new map. This is
@@ -156,7 +156,7 @@ func (d Decoder) unmarshal(data []byte, f Format, v interface{}) error {
 
 func (d Decoder) unmarshalCSV(data []byte, v interface{}) error {
        r := csv.NewReader(bytes.NewReader(data))
-       r.Comma = d.Comma
+       r.Comma = d.Delimiter
        r.Comment = d.Comment
 
        records, err := r.ReadAll()
index 719fbf100a8d327daf8f1e03193e08e297b6c144..4f81528c308a284c7ef5acd6ba0043477121d0b4 100644 (file)
@@ -92,7 +92,7 @@ func FormatFromFrontMatterType(typ pageparser.ItemType) Format {
 // in the given string.
 // It return an empty string if no format could be detected.
 func (d Decoder) FormatFromContentString(data string) Format {
-       csvIdx := strings.IndexRune(data, d.Comma)
+       csvIdx := strings.IndexRune(data, d.Delimiter)
        jsonIdx := strings.Index(data, "{")
        yamlIdx := strings.Index(data, ":")
        tomlIdx := strings.Index(data, "=")
index e0574eb0408665e2c2fe277a11f8b689d46fd1e2..bc83869756e502fdfedbe6dfe9a190549b77a3f9 100644 (file)
@@ -116,12 +116,12 @@ func decodeDecoder(m map[string]interface{}) (metadecoders.Decoder, error) {
        // mapstructure does not support string to rune conversion, so do that manually.
        // See https://github.com/mitchellh/mapstructure/issues/151
        for k, v := range m {
-               if strings.EqualFold(k, "Comma") {
+               if strings.EqualFold(k, "Delimiter") {
                        r, err := stringToRune(v)
                        if err != nil {
                                return opts, err
                        }
-                       opts.Comma = r
+                       opts.Delimiter = r
                        delete(m, k)
 
                } else if strings.EqualFold(k, "Comment") {
index b1ce30b1f260091934639d652b7ef3a4266615ad..d9ebd1f89a439a13341d90069bd76e559f93b136 100644 (file)
@@ -118,7 +118,7 @@ func TestUnmarshal(t *testing.T) {
                        assert.Equal(5, len(first))
                        assert.Equal("Ford", first[1])
                }},
-               {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"comma": ";"}, func(r [][]string) {
+               {testContentResource{key: "r1", content: `a;b;c`, mime: media.CSVType}, map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
                        assert.Equal(r, [][]string{[]string{"a", "b", "c"}})
 
                }},
@@ -126,13 +126,13 @@ func TestUnmarshal(t *testing.T) {
                        assert.Equal(r, [][]string{[]string{"a", "b", "c"}})
 
                }},
-               {"a;b;c", map[string]interface{}{"comma": ";"}, func(r [][]string) {
+               {"a;b;c", map[string]interface{}{"delimiter": ";"}, func(r [][]string) {
                        assert.Equal(r, [][]string{[]string{"a", "b", "c"}})
 
                }},
                {testContentResource{key: "r1", content: `
 % This is a comment
-a;b;c`, mime: media.CSVType}, map[string]interface{}{"CommA": ";", "Comment": "%"}, func(r [][]string) {
+a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment": "%"}, func(r [][]string) {
                        assert.Equal(r, [][]string{[]string{"a", "b", "c"}})
 
                }},