gtg

package
Version: v0.0.0-...-3cec428 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2020 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func TryGet

func TryGet(object 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

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

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

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"}
Example (MapFunc)

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

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

[a b c]
Example (MapValue)

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

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

[a b c]
Example (Struct)

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

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

[a b c]

func TryGetString

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.
Example (MapFunc)

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

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

bar
Example (MapValue)

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

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

bar
Example (Struct)

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

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

bar

func TryGetStringSlice

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"}
Example (MapFunc)

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

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

[a b c]
Example (MapValue)

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

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

[a b c]
Example (Struct)

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

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

[a b c]

Types

This section is empty.

Jump to

Keyboard shortcuts

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