go-stringify: github.com/spatialcurrent/go-stringify/pkg/stringify Index | Examples | Files

package stringify

import "github.com/spatialcurrent/go-stringify/pkg/stringify"

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

Package Files

Concat.go InterfaceSliceToStringSlice.go StringSliceToInterfaceSlice.go Stringer.go StringifyMapKeys.go StringifySlice.go stringify.go

func Concat Uses

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

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

func InterfaceSliceToStringSlice Uses

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 Uses

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

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

func StringifyMapKeys Uses

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.

Code:

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"}

Code:

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 Uses

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.

Code:

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", "-"}

Code:

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", "-"}

type Stringer Uses

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.

Code:

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 Uses

func NewDecimalStringer() Stringer

NewDecimalStringer returns a new stringer that uses decimal notation.

func NewDefaultStringer Uses

func NewDefaultStringer() Stringer

NewDefaultStringer returns a new default stringer.

func NewStringer Uses

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.

Package stringify imports 4 packages (graph) and is imported by 10 packages. Updated 2019-07-20. Refresh now. Tools for package owners.