struct_utils

package
v1.261.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValue added in v1.72.0

func GetValue(key string, kv []KeyValuePair) string

GetValue returns the value for the given key from a KeyValuePair slice.

func MapParams

func MapParams(data interface{}) map[string]string

MapParams convert fields in a struct to a map of parameters, as if defined in a URL to convert the new params struct into a map[string]string

func NamedValuesFromEnv

func NamedValuesFromEnv(prefix string) map[string][]string

Purpose:

Make a list of named values from the env for parsing into a struct

Parameters:

prefix should be uppercase (by convention) env prefix like "MY_LIB_CONFIG", without trailing "_"

Result:

named values that can be passed into UnmarshalNamedValues()

All env starting with "<prefix>_" will be copied without "<prefix>_" Examples with prefix="MY_LIB_CONFIG": MY_LIB_CONFIG_MAX_SIZE="6" -> {"MAX_SIZE":["6"]} one value of "6" MY_LIB_CONFIG_NAMES ="A,B,C" -> {"NAMES":["A,B,C"]} one value of "A,B,C" MY_LIB_CONFIG_NRS ="1,2,3" -> {"NRS":["1,2,3"]} one value of "1,2,3" (all env values are string, later parsed into int based on struct field type) MY_LIB_CONFIG_CODES ="[1,2,3]"" -> {"CODES":["1","2","3"]} 3 values of "1", "2" and "3" because of outer [...], env values are string

MY_LIB_CONFIG_CODES_1=5 MY_LIB_CONFIG_CODES_5=7 MY_LIB_CONFIG_CODES_2=10 -> {"CODES":["5","10","7"]} 3 values ordered on suffixes "_1", "_5", "_2" moving 10 before 7

MY_LIB_CONFIG_ADDRS=["55 Crescent, Town", "12 Big Street, City"] -> 2 values including commas because of quoted CSV

func NamedValuesFromReader

func NamedValuesFromReader(prefix string, reader string_utils.KeyReader) map[string][]string

func NamedValuesFromURL

func NamedValuesFromURL(params map[string]string, multiValueParams map[string][]string) map[string][]string

converts query string params to named values that can be parsed into a struct it support both single/multi-value params, depending how you get them from your HTTP library

(e.g. AWS API Gateway Context returns both but default golang net/http returns only params)

func UnmarshalJSON

func UnmarshalJSON(data []byte, val interface{}) error

UnmarshalJSON performs a JSON unmarshalling, but on type mismatches it returns a more user friendly error. Used exactly the same as json.Unmarshal

func UnmarshalNamedValues

func UnmarshalNamedValues(namedValues map[string][]string, structPtr interface{}) (unusedValues map[string][]string, err error)

Purpose:

	UnmarshalNamedValues() parses a set of named values into a struct using json tag matching
 Unlike json.Unmarshal(), it takes care of converting quoted "true" -> true, "1" -> int(1) etc...

	Typically used to parse environment or URL params into a struct
	because normal json.Unmarshal() will fail to parse "2" into an integer etc

	By convention, the names should be lowercase to match json tag with "_" delimeters
	And also use "_" for nested sub-struct names
	  named value "a_b_c_d":5 would be stored in
   field with json tag "a_b_c_d" or
	  field with json tag "a_b"        which is a struct with a json tagged field "c_d" etc...

Parameters:

namedValues is name-value pairs, typical from URL params or OS environment
	see construction functions for this:
		NamedValuesFromEnv()
		NamedValuesFromURL()

structPtr must be ptr to a struct variable
	undefined values will not be changed, so you can call this multiple times on the
	same struct to amend a few values, leaving the rest and default values unchanged

Return:

unused values
nil or error if some values could not be used

If all values must be used, check len(unusedValues) when err==nil

func ValidateRequiredFields added in v1.237.0

func ValidateRequiredFields(object any, parentFieldKeys ...string) error

ValidateRequiredFields checks the required tag on struct fields and returns an error if any are set to zero.

Types

type KeyValuePair added in v1.72.0

type KeyValuePair struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

KeyValuePair defines a key/value pair derived from form data

func FormToKeyValuePairs added in v1.72.0

func FormToKeyValuePairs(body string) []KeyValuePair

FormToKeyValuePairs returns a string-based map of strings as derived from posted form keys and values. e.g. oauth_consumer_key=mlhgs&oauth_consumer_secret=x240ar&oauth_verifier=b0qjbx&store_base_url=http%3A%2F%2Flocalhost.com%2Fstore

Jump to

Keyboard shortcuts

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