interpolate

package
v1.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2018 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package interpolate provides a generic mechanism to interpolate variables into strings.

Variables are specified in the form "${foo}". Values for the different variables are supplied by a VariableResolver function provided at render-time. Variable names can also be in the form "${foo:bar}" where everything after the ":" is the default value for that variable if the VariableResolver did not have a value for that variable.

Example
s, err := Parse("My name is ${name}")
if err != nil {
	panic(err)
}

out, err := s.Render(resolver)
if err != nil {
	panic(err)
}

fmt.Println(out)
Output:

My name is Inigo Montoya
Example (Default)
s, err := Parse("My name is ${name:what}")
if err != nil {
	panic(err)
}

out, err := s.Render(emptyResolver)
if err != nil {
	panic(err)
}

fmt.Println(out)
Output:

My name is what

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type String

type String []term

String is a string that supports interpolation given some source of variable values.

A String can be obtained by calling Parse on a string.

func Parse

func Parse(data string) (out String, _ error)

Parse parses a string for interpolation.

Variables may be specified anywhere in the string in the format ${foo} or ${foo:default} where 'default' will be used if the variable foo was unset.

func (String) Render

func (s String) Render(resolve VariableResolver) (string, error)

Render renders and returns the string. The provided VariableResolver will be used to determine values for the different variables mentioned in the string.

func (String) RenderTo

func (s String) RenderTo(w io.Writer, resolve VariableResolver) error

RenderTo renders the string into the given writer. The provided VariableResolver will be used to determine values for the different variables mentioned in the string.

type VariableResolver

type VariableResolver func(name string) (value string, ok bool)

VariableResolver resolves the value of a variable specified in the string.

The boolean value indicates whether this variable had a value defined. If a variable does not have a value and no default is specified, rendering will fail.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL