stringify

package
Version: v0.0.0-...-232a67e Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 4 Imported by: 11

Documentation

Overview

Package stringify is a simple library for generating a stringer that converts literals to strings, converts map keys to strings, or converts a slice to a slice of strings. This package is used by go-simple-serializer.

- http://github.com/spatialcurrent/go-simple-serializer

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat(in interface{}, stringer Stringer) (string, error)

Concat recursively flattens, stringifies, and concats an array or array of arrays.

func InterfaceSliceToStringSlice

func InterfaceSliceToStringSlice(values []interface{}) []string

InterfaceSliceToStringSlice converts a slice of interface{} to a slice of strings using fmt.Sprint. Use StringifySlice for options for stringifying.

func StringSliceToInterfaceSlice

func StringSliceToInterfaceSlice(values []string) []interface{}

StringSliceToInterfaceSlice converts a slice of strings to a slice of interface{}.

func StringifyMapKeys

func StringifyMapKeys(in interface{}, stringer Stringer) (interface{}, error)

StringifyMapKeys recursively stringifying map keys from interface{} to string. This functionality is inspired by work done in https://github.com/gohugoio/hugo, but support many more types, including:

- []interface{}
- [][]interface{}
- []map[interface{}]interface{}
- map[interface{}]interface{}
- map[string]interface{}
- map[string][]interface{}
- map[string]map[string][]interface{}
- map[interface{}]struct{}

See https://github.com/gohugoio/hugo/pull/4138 for background.

Example (Default)
in := map[interface{}]interface{}{"a": "x", "b": "y", "c": "z"}

nodata := ""
decimal := false
lower := false
upper := false
stringer := NewStringer(nodata, decimal, lower, upper)
out, err := StringifyMapKeys(in, stringer)
if err != nil {
	panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))
Output:

map[string]interface {}{"a":"x", "b":"y", "c":"z"}
Example (Upper)
in := map[interface{}]interface{}{"a": "x", "b": "y", "c": "z"}

nodata := ""
decimal := false
lower := false
upper := true
stringer := NewStringer(nodata, decimal, lower, upper)
out, err := StringifyMapKeys(in, stringer)
if err != nil {
	panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))
Output:

map[string]interface {}{"A":"x", "B":"y", "C":"z"}

func StringifySlice

func StringifySlice(in interface{}, stringer Stringer) ([]string, error)

StringifySlice converts all the objects in a slice or array to strings using the given stringer. Returns a []string, and error if any.

Example (Decimal)
in := []interface{}{
	"a",
	"b",
	"c",
	1,
	2,
	3,
	1234567890.123,
	true,
	false,
	nil,
}
nodata := "-"
decimal := true
lower := false
upper := true
stringer := NewStringer(nodata, decimal, lower, upper)
out, err := StringifySlice(in, stringer)
if err != nil {
	panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))
Output:

[]string{"A", "B", "C", "1", "2", "3", "1234567890.123000", "TRUE", "FALSE", "-"}
Example (Default)
in := []interface{}{
	"a",
	"b",
	"c",
	1,
	2,
	3,
	1234567890.123,
	true,
	false,
	nil,
}
nodata := "-"
decimal := false
lower := false
upper := true
stringer := NewStringer(nodata, decimal, lower, upper)
out, err := StringifySlice(in, stringer)
if err != nil {
	panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))
Output:

[]string{"A", "B", "C", "1", "2", "3", "1.234567890123E+09", "TRUE", "FALSE", "-"}

Types

type Stringer

type Stringer func(object interface{}) (string, error)

Stringer is a type alias for a function that converts an object into a string and returns an error if any.

Example
in := []interface{}{
	"a",
	1,
	1234567890.123,
	true,
	false,
	nil,
}

nodata := "-"
decimal := false
lower := false
upper := false
stringer := NewStringer(nodata, decimal, lower, upper)

for _, x := range in {
	str, err := stringer(x)
	if err != nil {
		panic(err)
	}
	fmt.Println(str)
}
Output:

a
1
1.234567890123e+09
true
false
-

func NewDecimalStringer

func NewDecimalStringer() Stringer

NewDecimalStringer returns a new stringer that uses decimal notation.

func NewDefaultStringer

func NewDefaultStringer() Stringer

NewDefaultStringer returns a new default stringer.

func NewStringer

func NewStringer(noDataValue string, decimal bool, lower bool, upper bool) Stringer

NewStringer creates a new Stringer. If decimal is true, the stringer converts floats to strings in decimal notation rather than scientific notation. If lower, then returned strings are converted to lower case. If upper, then returned strings are converted to upper case.

Jump to

Keyboard shortcuts

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