Skip to content

Commit

Permalink
docs: Document transform.Unmarshal
Browse files Browse the repository at this point in the history
Fixes #5556
  • Loading branch information
bep committed Dec 23, 2018
1 parent 25ddbb0 commit 2efc1a6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/content/en/functions/transform.Unmarshal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "transform.Unmarshal"
description: "`transform.Unmarshal` (alias `unmarshal`) parses the input and converts it into a map or an array. Supported formats are JSON, TOML, YAML and CSV."
date: 2018-12-23
categories: [functions]
menu:
docs:
parent: "functions"
keywords: []
signature: ["RESOURCE or STRING | transform.Unmarshal [OPTIONS]" ]
hugoversion: "0.53"
aliases: []
---


The function accept either a `Resource` created in [Hugo Pipes](/hugo-pipes/) or via [Page Bundles](content-management/page-bundles/), or simply a string. The two examples below will produce the same map:

```go-html-template
{{ $greetings := "hello = \"Hello Hugo\"" | transform.Unmarshal }}`
```

```go-html-template
{{ $greetings := "hello = \"Hello Hugo\"" | resources.FromString "data/greetings.toml" | transform.Unmarshal }}
```

In both the above examples, you get a map you can work with:

```go-html-template
{{ $greetings.hello }}
```

The above prints `Hello Hugo`.

## CSV Options

Unmarshal with CSV as input has some options you can set:

comma
: The delmiter used, default is `,`

comment
: The comment character ued in the CSV. If set, lines beginning with the comment character without preceding whitespace are ignored.:


Example:

```go-html-template
{{ $csv := "a;b;c" | transform.Unmarshal (dict "comma" ";") }}
```

3 comments on commit 2efc1a6

@rdwatters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if this is too far after the fact, but does “separator” make more sense than “comma” in terms of syntax? Regardless, really cool new feature. I can’t even keep up with the cool stuff Hugo is doing these days 😄

@bep
Copy link
Member Author

@bep bep commented on 2efc1a6 Dec 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These maps 1:1 with the terms used in Go's stdlib, which makes it "good enough" for me...

@rdwatters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Didn’t know that Go uses “comma” in this way. Please disregard. Thanks for the quick feedback.

Please sign in to comment.