utils

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidObjectType = errors.New("invalid object type")
	ErrObjectKeyNotFound = errors.New("key not found in object")
	ErrJSONMarshal       = errors.New("can not parse json")
	ErrInvalidImportPath = errors.New("")
)

Functions

func FilterJSONString

func FilterJSONString(data string, keys []string, include bool) (map[string]any, string, error)

FilterJSONString filters keys from a JSON string. It takes a JSON string, a list of keys to filter, and a boolean indicating whether to include or exclude those keys. It returns the filtered JSON object, the filtered JSON string, and any error.

func GetEnvOrDefault

func GetEnvOrDefault(k, defaultvalue string) string

GetEnvOrDefault returns the value of the environment variable k if set, or defaultvalue if not set.

func GetKeys

func GetKeys(hash map[string]any) []string

GetKeys returns a slice containing all the keys in the given hash map.

func GetObjectAtKey

func GetObjectAtKey(data map[string]any, path string) (any, error)

GetObjectAtKey recursively traverses a map[string]any to find the value at the given slash-delimited path. It can handle maps and slices in the path.

Example inputs:

data = {
  "foo": {
    "bar": [1, 2, 3]
  }
}

path = "foo/bar/0"

Returns 1.

func GetRequestData

func GetRequestData(data, overwrite map[string]any) (string, error)

GetRequestData marshals the provided data map into a JSON string. If overwrite is provided, it will override data. Returns the JSON string and any error from json.Marshal.

func GetStringAtKey

func GetStringAtKey(data map[string]any, path string) (string, error)

GetStringAtKey returns the string value at the given slash-delimited path in the provided map[string]any data. It will attempt to convert values of other types like numbers to strings. Returns error if path not found or value is not a string or convertible type.

func IntersectMaps

func IntersectMaps(map1, map2 map[string]any) map[string]any

IntersectMaps takes two maps and returns a new map containing only the keys and values that exist in both input maps. For keys that exist in both maps but have different value types, the value from map2 is used. Nested maps are intersected recursively.

func ParseImportPath

func ParseImportPath(id string) (string, string, error)

ParseImportPath parses a Restobject import path string into its object ID and path components.

func SanitizePath

func SanitizePath(path string) string

SanitizePath removes duplicate slashes and trailing slash from the provided path.

Types

This section is empty.

Jump to

Keyboard shortcuts

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