reflect

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertValue

func ConvertValue(value string, targetType reflect.Type) interface{}

ConvertValue converts a single string value to a type matching the specified target type.

value: The string value to be converted. targetType: The desired type to which the value should be converted.

Returns the converted value or the original string value if no suitable conversion is possible.

func ConvertValues

func ConvertValues(values []string, field reflect.StructField) interface{}

ConvertValues converts a slice of string values to a type matching the specified target type.

values: A slice of string values to be converted. targetType: The desired type to which the values should be converted.

Returns the converted value, which may be a slice or a single value, depending on the target type.

func FindField

func FindField(t reflect.Type, name string) (reflect.StructField, bool)

FindField identifies and retrieves a struct field based on its name or its JSON tag.

t: The type of the struct to search within. name: The name of the field or its JSON tag.

Returns the struct field and a boolean indicating whether the field was found.

func FormBodyExtract

func FormBodyExtract(target interface{}, source ...ParamsExtractorSource)

func ParamsExtract

func ParamsExtract(target interface{}, source ...ParamsExtractorSource) error

ParamsExtract extracts parameters from one or more ParamsExtractorSource instances, converts them to their target types, and binds them to the specified target struct. The function handles both single-value and multi-value parameters, supports JSON tags for field mapping, and ensures type conversions using ConvertValue and ConvertValues methods.

target: The struct to which the extracted and converted parameters will be bound. source: A variadic list of ParamsExtractorSource instances, each representing a source of parameters.

Returns an error if parameter extraction or JSON unmarshalling into the target struct fails.

func ParseToJson

func ParseToJson(
	targetType reflect.Type,
	source ...ParamsExtractorSource,
) ([]byte, error)

Types

type ParamsExtractorSource

type ParamsExtractorSource struct {
	// Source is a flat map where keys are parameter names and values are their corresponding string values.
	Source map[string]string
	// Tree represents a hierarchical map, primarily for parameters with multiple values, such as query parameters.
	Tree map[string][]string
}

Jump to

Keyboard shortcuts

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