go-try-get: github.com/spatialcurrent/go-try-get/pkg/gtg Index | Examples | Files

package gtg

import "github.com/spatialcurrent/go-try-get/pkg/gtg"

Package gtg wraps around the reflect package to provide a standard abstraction layer for getting values by name from objects (structs, maps, and single-value "property" functions).

Index

Examples

Package Files

TryGet.go TryGetInt.go TryGetInt64.go TryGetInterfaceSlice.go TryGetString.go TryGetStringSlice.go gtg.go

func TryGet Uses

func TryGet(obj interface{}, name string, fallback interface{}) interface{}

TryGet returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value, then return the result,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function), then return its value directly.
- If the value by name does not exist, then returns the value of the fallback parameter.

Examples

- TryGet(map[string]string{"yo":"yo"}, "yo", "what") == "yo"

func TryGetInt Uses

func TryGetInt(obj interface{}, name string, fallback int) int

TryGetInt returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value and it is convertible to the int type, then return the result,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function) and it the value if convertible to the int type,
  then return its value directly.
- If the value by name does not exist, then returns the value of the fallback parameter.

Examples

- TryGetInt(map[string]int{"yo":10}, "yo", 20) == 10
- TryGetInt(map[string]int{"yo":10}, "hey", 20) == 20

func TryGetInt64 Uses

func TryGetInt64(obj interface{}, name string, fallback int64) int64

TryGetInt64 returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value and it is convertible to the int64 type, then return the result,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function) and it the value if convertible to the int64 type,
  then return its value directly.
- If the value by name does not exist, then returns the value of the fallback parameter.

Examples

- TryGetInt64(map[string]int{"yo":10}, "yo", int64(20)) == int64(10)
- TryGetInt64(map[string]int{"yo":10}, "hey", int64(20)) == int64(20)

func TryGetInterfaceSlice Uses

func TryGetInterfaceSlice(obj interface{}, name string, fallback []interface{}) []interface{}

TryGetInterfaceSlice returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value, then return the result as a 1 element string slice,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function),
  then return the value by name as a interface slice.
- If the value by name does not exist, then returns the value of the fallback parameter.

Examples

- TryGetInterfaceSlice(map[string]string{"yo":[]interface{}{"a", "b", "c"}}, "yo", "what") == []interface{}{"a", "b", "c"}

This example shows you can get a string from a map function.

Code:

in := map[string]interface{}{
    "foo": func() []interface{} {
        return []interface{}{"a", "b", "c"}
    },
}
out := TryGetInterfaceSlice(in, "foo", []interface{}{})
fmt.Println(out)

Output:

[a b c]

This example shows you can get a string from a map value.

Code:

in := map[string]interface{}{
    "foo": []interface{}{"a", "b", "c"},
}
out := TryGetInterfaceSlice(in, "foo", []interface{}{})
fmt.Println(out)

Output:

[a b c]

This example shows you can get a string from a struct.

Code:

in := struct{ Foo []interface{} }{
    Foo: []interface{}{"a", "b", "c"},
}
out := TryGetInterfaceSlice(in, "Foo", []interface{}{})
fmt.Println(out)

Output:

[a b c]

func TryGetString Uses

func TryGetString(obj interface{}, name string, fallback string) string

TryGetString returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value, then return the result as a string using `fmt.Sprint`,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function),
  then return the value by name as a string using `fmt.Sprint`.
- If the value by name does not exist, then returns the value of the fallback parameter.

This example shows you can get a string from a map function.

Code:

in := map[string]interface{}{
    "foo": func() string {
        return "bar"
    },
}
out := TryGetString(in, "foo", "")
fmt.Println(out)

Output:

bar

This example shows you can get a string from a map value.

Code:

in := map[string]interface{}{
    "foo": "bar",
}
out := TryGetString(in, "foo", "")
fmt.Println(out)

Output:

bar

This example shows you can get a string from a struct.

Code:

in := struct{ Foo string }{
    Foo: "bar",
}
out := TryGetString(in, "Foo", "")
fmt.Println(out)

Output:

bar

func TryGetStringSlice Uses

func TryGetStringSlice(obj interface{}, name string, fallback []string) []string

TryGetStringSlice returns the value from the object (a map or struct) by name.

- If the name refers to a struct method or func in a map, then call the function.
  If the function only returns 1 value, then return the result as a 1 element string slice using `fmt.Sprint`,
  otherwise return the value of the fallback parameter.
- If the name refers to a struct field or map key (and not a function),
  then return the value by name as a string array using `fmt.Sprint`.
- If the value by name does not exist, then returns the value of the fallback parameter.

Examples

- TryGetStringSlice(map[string]string{"yo":[]string{"a", "b", "c"}}, "yo", "what") == []string{"a", "b", "c"}

This example shows you can get a string from a map function.

Code:

in := map[string]interface{}{
    "foo": func() []string {
        return []string{"a", "b", "c"}
    },
}
out := TryGetStringSlice(in, "foo", []string{})
fmt.Println(out)

Output:

[a b c]

This example shows you can get a string from a map value.

Code:

in := map[string]interface{}{
    "foo": []string{"a", "b", "c"},
}
out := TryGetStringSlice(in, "foo", []string{})
fmt.Println(out)

Output:

[a b c]

This example shows you can get a string from a struct.

Code:

in := struct{ Foo []string }{
    Foo: []string{"a", "b", "c"},
}
out := TryGetStringSlice(in, "Foo", []string{})
fmt.Println(out)

Output:

[a b c]

Package gtg imports 2 packages (graph) and is imported by 2 packages. Updated 2019-11-11. Refresh now. Tools for package owners.