Documentation ¶
Overview ¶
Package utils contains various utils that do various things. It is deprecated and any future utils should be added as an appropriately named sub-package. Over time this package should be emptied.
Index ¶
- Constants
- Variables
- func CopyStringMap(m map[string]string) map[string]string
- func Indent(text, by string) string
- func NewLogger(name string) lager.Logger
- func PrettyPrintOrExit(content any)
- func PropertyToEnv(propertyName string) string
- func PropertyToEnvUnprefixed(propertyName string) string
- func SetParameter(input json.RawMessage, key string, value any) (json.RawMessage, error)
- func SingleLineErrorFormatter(es []error) string
- func SplitNewlineDelimitedList(paksText string) []string
- func UnmarshalObjectRemainder(data []byte, v any) ([]byte, error)
- type StringSet
- func (set StringSet) Add(str ...string)
- func (set StringSet) Contains(other string) bool
- func (set StringSet) Equals(other StringSet) bool
- func (set StringSet) IsEmpty() bool
- func (set StringSet) Minus(other StringSet) StringSet
- func (set StringSet) String() string
- func (set StringSet) ToSlice() []string
Examples ¶
Constants ¶
const EnvironmentVarPrefix = "gsb"
Variables ¶
var ( PropertyToEnvReplacer = strings.NewReplacer(".", "_", "-", "_") // InvalidLabelChars encodes that GCP labels only support alphanumeric, // dash and underscore characters in keys and values. InvalidLabelChars = regexp.MustCompile("[^a-zA-Z0-9_-]+") )
var Version = "0.0.0" // set at build time via "-ldflags"
Version sets the version for the whole cloud service broker software.
Functions ¶
func CopyStringMap ¶
CopyStringMap makes a copy of the given map.
Example ¶
m := map[string]string{"a": "one"} copy := CopyStringMap(m) m["a"] = "two" fmt.Println(m["a"]) fmt.Println(copy["a"])
Output: two one
func Indent ¶
Indent indents every line of the given text with the given string.
Example ¶
weirdText := "First\n\tSecond" out := Indent(weirdText, " ") fmt.Println(out == " First\n \tSecond")
Output: true
func NewLogger ¶
func NewLogger(name string) lager.Logger
NewLogger creates a new lager.Logger with the given name that has correct writing settings.
func PrettyPrintOrExit ¶
func PrettyPrintOrExit(content any)
PrettyPrintOrExit writes a JSON serialized version of the content to stdout. If a failure occurs during marshaling, the error is logged along with a formatted version of the object and the program exits with a failure status.
func PropertyToEnv ¶
PropertyToEnv converts a Viper configuration property name into an environment variable prefixed with EnvironmentVarPrefix
Example ¶
env := PropertyToEnv("my.property.key-value") fmt.Println(env)
Output: GSB_MY_PROPERTY_KEY_VALUE
func PropertyToEnvUnprefixed ¶
PropertyToEnvUnprefixed converts a Viper configuration property name into an environment variable using PropertyToEnvReplacer
Example ¶
env := PropertyToEnvUnprefixed("my.property.key-value") fmt.Println(env)
Output: MY_PROPERTY_KEY_VALUE
func SetParameter ¶
func SetParameter(input json.RawMessage, key string, value any) (json.RawMessage, error)
SetParameter sets a value on a JSON raw message and returns a modified version with the value set
Example ¶
// Creates an object if none is input out, err := SetParameter(nil, "foo", 42) fmt.Printf("%s, %v\n", string(out), err) // Replaces existing values out, err = SetParameter([]byte(`{"replace": "old"}`), "replace", "new") fmt.Printf("%s, %v\n", string(out), err)
Output: {"foo":42}, <nil> {"replace":"new"}, <nil>
func SingleLineErrorFormatter ¶
SingleLineErrorFormatter creates a single line error string from an array of errors.
func SplitNewlineDelimitedList ¶
SplitNewlineDelimitedList splits a list of newline delimited items and trims any leading or trailing whitespace from them.
func UnmarshalObjectRemainder ¶
UnmarshalObjectRemainder unmarshals an object into v and returns the remaining key/value pairs as a JSON string by doing a set difference.
Example ¶
var obj struct { A string `json:"a_str"` B int } remainder, err := UnmarshalObjectRemainder([]byte(`{"a_str":"hello", "B": 33, "C": 123}`), &obj) fmt.Printf("%s, %v\n", string(remainder), err) remainder, err = UnmarshalObjectRemainder([]byte(`{"a_str":"hello", "B": 33}`), &obj) fmt.Printf("%s, %v\n", string(remainder), err)
Output: {"C":123}, <nil> {}, <nil>
Types ¶
type StringSet ¶
StringSet is a set data structure for strings
func NewStringSet ¶
NewStringSet creates a new string set with the given contents.
Example ¶
a := NewStringSet() a.Add("a") a.Add("b") b := NewStringSet("b", "a") fmt.Println(a.Equals(b))
Output: true
func NewStringSetFromStringMapKeys ¶
NewStringSetFromStringMapKeys creates a new string set with the given contents.
Example ¶
m := map[string]string{ "a": "some a value", "b": "some b value", } set := NewStringSetFromStringMapKeys(m) fmt.Println(set)
Output: [a b]
func (StringSet) Add ¶
Add puts one or more elements into the set.
Example ¶
set := NewStringSet() set.Add("a") set.Add("b") fmt.Println(set) set.Add("a") fmt.Println(set)
Output: [a b] [a b]
func (StringSet) Contains ¶
Contains performs a set membership check.
Example ¶
a := NewStringSet("a", "b") fmt.Println(a.Contains("z")) fmt.Println(a.Contains("a"))
Output: false true
func (StringSet) Equals ¶
Equals compares the contents of the two sets and returns true if they are the same.
Example ¶
a := NewStringSet("a", "b") b := NewStringSet("a", "b", "c") fmt.Println(a.Equals(b)) a.Add("c") fmt.Println(a.Equals(b))
Output: false true
func (StringSet) IsEmpty ¶
IsEmpty determines if the set has zero elements.
Example ¶
a := NewStringSet() fmt.Println(a.IsEmpty()) a.Add("a") fmt.Println(a.IsEmpty())
Output: true false
func (StringSet) Minus ¶
Minus returns a copy of this set with every string in the other removed.
Example ¶
a := NewStringSet("a", "b") b := NewStringSet("b", "c") delta := a.Minus(b) fmt.Println(delta)
Output: [a]
Directories ¶
Path | Synopsis |
---|---|
Package correlation reads correlation IDs from the context for logging
|
Package correlation reads correlation IDs from the context for logging |
Package freeport identifies a random unused port
|
Package freeport identifies a random unused port |
Package request decodes the originating identity header
|
Package request decodes the originating identity header |
Package stream implements streaming a bit like 'gulp' on nodejs
|
Package stream implements streaming a bit like 'gulp' on nodejs |