Skip to content

Commit

Permalink
Change transform.Unmarshal remote data examples (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring authored May 6, 2024
1 parent 4b79599 commit b4fc35c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
6 changes: 3 additions & 3 deletions content/en/functions/data/GetCSV.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ my-project/
{{ $p := "data/pets.csv" }}
{{ with resources.Get $p }}
{{ $opts := dict "delimiter" "," }}
{{ $data = .Content | transform.Unmarshal $opts }}
{{ $data = . | transform.Unmarshal $opts }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Expand All @@ -121,7 +121,7 @@ my-project/
{{ $p := "pets.csv" }}
{{ with .Resources.Get $p }}
{{ $opts := dict "delimiter" "," }}
{{ $data = .Content | transform.Unmarshal $opts }}
{{ $data = . | transform.Unmarshal $opts }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Expand All @@ -139,7 +139,7 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`]
{{ errorf "%s" . }}
{{ else }}
{{ $opts := dict "delimiter" "," }}
{{ $data = .Content | transform.Unmarshal $opts }}
{{ $data = . | transform.Unmarshal $opts }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $u }}
Expand Down
6 changes: 3 additions & 3 deletions content/en/functions/data/GetJSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ my-project/
{{ $data := dict }}
{{ $p := "data/books.json" }}
{{ with resources.Get $p }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Expand All @@ -124,7 +124,7 @@ my-project/
{{ $data := dict }}
{{ $p := "books.json" }}
{{ with .Resources.Get $p }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get resource %q" $p }}
{{ end }}
Expand All @@ -141,7 +141,7 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`]
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $u }}
Expand Down
2 changes: 1 addition & 1 deletion content/en/functions/encoding/Base64Decode.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To retrieve and render the content:
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ with .Content | transform.Unmarshal }}
{{ with . | transform.Unmarshal }}
{{ .content | base64Decode | markdownify }}
{{ end }}
{{ end }}
Expand Down
12 changes: 11 additions & 1 deletion content/en/functions/resources/GetRemote.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ When retrieving remote data, use the [`transform.Unmarshal`] function to [unmars
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
```

{{% note %}}
When retrieving remote data, a misconfigured server may send a response header with an incorrect [Content-Type]. For example, the server may set the Content-Type header to `application/octet-stream` instead of `application/json`.

In these cases, pass the resource `Content` through the `transform.Unmarshal` function instead of passing the resource itself. For example, in the above, do this instead:

`{{ $data = .Content | transform.Unmarshal }}`

[Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
{{% /note %}}

## Error handling

The [`Err`] method on a resource returned by the `resources.GetRemote` function returns an error message if the HTTP request fails, else nil. If you do not handle the error yourself, Hugo will fail the build.
Expand Down
21 changes: 15 additions & 6 deletions content/en/functions/transform/Unmarshal.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ assets/
{{ $data := dict }}
{{ $path := "data/books.json" }}
{{ with resources.Get $path }}
{{ with .Content | transform.Unmarshal }}
{{ with . | transform.Unmarshal }}
{{ $data = . }}
{{ end }}
{{ else }}
Expand Down Expand Up @@ -78,7 +78,7 @@ content/
{{ $data := dict }}
{{ $path := "books.json" }}
{{ with .Resources.Get $path }}
{{ with .Content | transform.Unmarshal }}
{{ with . | transform.Unmarshal }}
{{ $data = . }}
{{ end }}
{{ else }}
Expand All @@ -101,7 +101,7 @@ A remote resource is a file on a remote server, accessible via HTTP or HTTPS.
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
Expand All @@ -112,8 +112,15 @@ A remote resource is a file on a remote server, accessible via HTTP or HTTPS.
{{ end }}
```

[resource]: /getting-started/glossary/#resource
[page bundle]: /content-management/page-bundles/
{{% note %}}
When retrieving remote data, a misconfigured server may send a response header with an incorrect [Content-Type]. For example, the server may set the Content-Type header to `application/octet-stream` instead of `application/json`.

In these cases, pass the resource `Content` through the `transform.Unmarshal` function instead of passing the resource itself. For example, in the above, do this instead:

`{{ $data = .Content | transform.Unmarshal }}`

[Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
{{% /note %}}

## Options

Expand Down Expand Up @@ -172,7 +179,7 @@ Get the remote data:
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = .Content | transform.Unmarshal }}
{{ $data = . | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
Expand Down Expand Up @@ -290,3 +297,5 @@ Hugo renders this to:

[`index`]: /functions/collections/indexfunction/
[identifiers]: https://go.dev/ref/spec#Identifiers
[resource]: /getting-started/glossary/#resource
[page bundle]: /content-management/page-bundles/

0 comments on commit b4fc35c

Please sign in to comment.