Encoding

The Encoding registry offers methods for encoding and decoding data in different formats, allowing for flexible data representation and storage within your templates.

You can easily import all the functions from the encoding registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/encoding"

base64Encode

The function encodes a given string into its Base64 representation, converting the data into a text format suitable for transmission or storage in systems that support Base64 encoding.

Signature

Base64Encode(s string) string
{{ "Hello World" | base64Encode }} // Output: "SGVsbG8gV29ybGQ="

base64Decode

The function decodes a Base64 encoded string back to its original form. If the input string is not valid Base64, it returns an error message.

Signature

 Base64Decode(s string) (string, error)
{{ "SGVsbG8gV29ybGQ=" | base64Decode }} // Output: "Hello World"

base32Encode

The function encodes a given string into its Base32 representation, converting the data into a text format that is compatible with systems supporting Base32 encoding.

Signature

Base32Encode(s string) string
{{ "Hello World" | base32Encode }} // Output: "JBSWY3DPEBLW64TMMQQQ===="

base32Decode

The function decodes a Base32 encoded string back to its original form. If the input is not valid Base32, it returns an error message.

Signature

Base32Decode(s string) (string, error)
{{ "JBSWY3DPEBLW64TMMQQQ====" | base32Decode }} // Output: "Hello World"

fromJson

The function converts a JSON string into a corresponding Go data structure, enabling easy manipulation of the JSON data in a Go environment.

Signature

FromJson(v string) (any, error)
{{ '{"name":"John", "age":30}' | fromJson }} // Output: map[name:John age:30], nil
{{ '{\invalid' | fromJson }} // Error

toJson

The function converts a Go data structure into a JSON string, allowing the data to be easily serialized for storage, transmission, or further processing.

Signature

ToJson(v any) (string, error)
{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ toJson $d }} // Output: {"key1":"value1","key2":"value2","key3":"value3"}, nil

toPrettyJson

The function converts a Go data structure into a pretty-printed JSON string, formatting the output with indentation and line breaks for better readability.

Signature

ToPrettyJson(v any) (string, error)
{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ toPrettyJson $d }} // Output: "{\n  \"key1\": \"value1\",\n  \"key2\": \"value2\",\n  \"key3\": \"value3\"\n}"

toRawJson

The function converts a Go data structure into a JSON string without escaping HTML characters, preserving the raw content as it is.

Signature

ToRawJson(v any) (string, error)
{{ $d := dict "content" "<p>Hello World</p>" }}
{{ toRawJson $d }} // Output: {"content":"<p>Hello World</p>"}

fromYaml

The function deserializes a YAML string into a Go map, allowing the structured data from YAML to be used and manipulated within a Go program.

Signature

FromYAML(v string) (any, error)
{{ "name: John Doe\nage: 30" | fromYAML }} // Output: map[name:John Doe age:30], nil

toYaml

The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation.

Signature

ToYAML(v any) (string, error)
{{ $d := dict "name" "John Doe" "age" 30 }}
{{ $d | toYaml }} // Output: name: John Doe\nage: 30

Last updated

Change request #24: reflect safe functions feature and new signatures