interpol

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2016 License: MIT Imports: 6 Imported by: 39

README

interpol

License GoDoc Build Status Coverage codebeat badge goreportcard

interpol is a Go package for doing format-string like string interpolation using named parameters.

Currently, a template only accepts variable placeholders delimited by brace characters (eg. "Hello {foo} {bar}").

Install

First, you need to install the package:

go get -u github.com/imkira/go-interpol

Documentation

For advanced usage, make sure to check the available documentation here.

Example

The following code should use interpol.WithMap function, which simply replaces every key with the corresponding value of the specified map. When run, it should output Hello World!!!.

package main

import (
	"fmt"

	"github.com/imkira/go-interpol"
)

func main() {
	m := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}
	str, err := interpol.WithMap("{foo} {bar}!!!", m)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	fmt.Println(str)
}

Contribute

Found a bug? Want to contribute and add a new feature?

Please fork this project and send me a pull request!

License

go-interpol is licensed under the MIT license:

www.opensource.org/licenses/MIT

Copyright (c) 2016 Mario Freitas. See LICENSE for further details.

Documentation

Overview

Package interpol provides utility functions for doing format-string like string interpolation using named parameters. Currently, a template only accepts variable placeholders delimited by brace characters (eg. "Hello {foo} {bar}").

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnexpectedClose = errors.New("interpol: unexpected close in template")
	ErrExpectingClose  = errors.New("interpol: expecting close in template")
	ErrKeyNotFound     = errors.New("interpol: key not found")
	ErrReadByteFailed  = errors.New("interpol: read byte failed")
)

Errors returned when formatting templates.

Functions

func WithFunc

func WithFunc(template string, format Func) (string, error)

WithFunc interpolates the specified template with replacements using the given function.

func WithMap

func WithMap(template string, m map[string]string) (string, error)

WithMap interpolates the specified template with replacements using the given map. If a placeholder is used for which a value is not found, an error is returned.

Types

type Func

type Func func(key string, w io.Writer) error

Func receives the placeholder key and writes to the io.Writer. If an error happens, the function can return an error, in which case the interpolation will be aborted.

type Interpolator added in v1.1.0

type Interpolator struct {
	// contains filtered or unexported fields
}

Interpolator interpolates Template to Output, according to Format.

func New added in v1.1.0

func New(opts ...Option) *Interpolator

New creates a new interpolator with the given list of options. You can use options such as the ones returned by WithTemplate, WithFormat and WithOutput.

func NewWithOptions added in v1.1.0

func NewWithOptions(opts *Options) *Interpolator

NewWithOptions creates a new interpolator with the given options.

func (*Interpolator) Interpolate added in v1.1.0

func (i *Interpolator) Interpolate() error

Interpolate reads runes from Template and writes them to Output, with the exception of placeholders which are passed to Format.

type Option added in v1.1.0

type Option func(OptionSetter)

Option is an option that can be applied to an Interpolator.

func WithFormat added in v1.1.0

func WithFormat(format Func) Option

WithFormat assigns Format to Options.

func WithOutput added in v1.1.0

func WithOutput(output io.Writer) Option

WithOutput assigns Output to Options.

func WithTemplate added in v1.1.0

func WithTemplate(template io.Reader) Option

WithTemplate assigns Template to Options.

type OptionSetter added in v1.1.0

type OptionSetter interface {
	SetTemplate(template io.Reader)
	SetFormat(format Func)
	SetOutput(output io.Writer)
}

OptionSetter is an interface that contains the setters for all options supported by Interpolator.

type Options added in v1.1.0

type Options struct {
	Template io.Reader
	Format   Func
	Output   io.Writer
}

Options contains all options supported by an Interpolator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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