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.
Index ¶
- func Concat(in interface{}, stringer Stringer) (string, error)
- func InterfaceSliceToStringSlice(values []interface{}) []string
- func StringSliceToInterfaceSlice(values []string) []interface{}
- func StringifyMapKeys(in interface{}, stringer Stringer) (interface{}, error)
- func StringifySlice(in interface{}, stringer Stringer) ([]string, error)
- type Stringer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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.Printf("%#v\n", 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.Printf("%#v\n", out)
Output: map[string]interface {}{"A":"x", "B":"y", "C":"z"}
func StringifySlice ¶
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.Printf("%#v\n", 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.Printf("%#v\n", out)
Output: []string{"A", "B", "C", "1", "2", "3", "1.234567890123E+09", "TRUE", "FALSE", "-"}
Types ¶
type Stringer ¶
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 ¶
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.