parser

package
v0.0.0-...-0c6c348 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMapKey    = errors.New("invalid key")
	ErrInvalidFieldType = errors.New("invalid field type")
)
View Source
var ErrInvalidPathParam = errors.New("invalid path param")

ErrInvalidPathParam is returned when the path parameter is invalid.

Functions

func ParseEnvBool

func ParseEnvBool(key string, defaultValue bool) bool

func ParseEnvFloat64

func ParseEnvFloat64(key string, defaultValue float64) (float64, error)

func ParseEnvInt

func ParseEnvInt(key string, defaultValue int) (int, error)

func ParseEnvIntPanic

func ParseEnvIntPanic(key string) int

func ParseEnvString

func ParseEnvString(key string, defaultValue string) string

func ParseEnvStringPanic

func ParseEnvStringPanic(key string) string

func ParseIDPathParam

func ParseIDPathParam(r *http.Request) (int64, error)

Retrieve the "id" URL parameter from the current request context, then convert it to an integer and return it. If the operation isn't successful, return 0 and an error.

func ParseJSONMapInt64

func ParseJSONMapInt64(m map[string]any, key string) (int64, error)

ParseJSONMapInt64 parses a map[string]any and returns the value as an int64. Since json.Unmarshal() converts all numbers to float64, we need to convert them back to int64. Caller should ensure that castint from float64 to int64 is safe.

func ParseJSONMapString

func ParseJSONMapString(m map[string]any, key string) (string, error)

ParseJSONMapString parses a map[string]any and returns the value as a string.

func ParseQSBool

func ParseQSBool(queryValues url.Values, key string, defaultValue *bool) *bool

ParseQSBool returns a boolean value from the query string or the provided default value if no matching key can be found.

func ParseQSInt

func ParseQSInt(queryValues url.Values, key string, defaultValue *int) *int

ParseQSInt returns an integer value from the query string or the provided default value if no matching key can be found.

func ParseQSInt64

func ParseQSInt64(queryValues url.Values, key string, defaultValue *int64) *int64

ParseQSInt64 returns an integer value from the query string or the provided default value if no matching key can be found.

func ParseQSString

func ParseQSString(queryValues url.Values, key string, defaultValue *string) *string

ParseQSString returns a string value from the query string or the provided default value if no matching key can be found.

func ParseQSStringSlice

func ParseQSStringSlice(queryValues url.Values, key string, defaultSlice []string) []string

func StructsToFilteredMaps

func StructsToFilteredMaps(v interface{}, includeFields []string) ([]map[string]interface{}, error)

StructsToFilteredMaps converts a slice of structs to filtered maps using dot notation.

Types

type Filters

type Filters struct {
	Fields   []string
	Page     int
	PageSize int
	Sort     string
}

Filters contains common query string parameters for fieldsets, pagination, and sort.

func (*Filters) ParseQSMetadata

func (f *Filters) ParseQSMetadata(
	queryValues url.Values,
	v *validation.Validator,
	fieldSafeList, sortSafeList []string,
)

ParseQSMetadata parses the query string sort, page, and page size and populates the Filters struct.

type PaginationMetadata

type PaginationMetadata struct {
	CurrentPage  int `json:"currentPage,omitempty"`
	PageSize     int `json:"pageSize,omitempty"`
	FirstPage    int `json:"firstPage,omitempty"`
	LastPage     int `json:"lastPage,omitempty"`
	TotalRecords int `json:"totalRecords,omitempty"`
}

PaginationMetadata contains metadata about the current page of paginated data.

func ParsePaginationMetadata

func ParsePaginationMetadata(totalRecords, page, pageSize int) PaginationMetadata

ParsePaginationMetadata calculates the pagination metadata based on the total number of records, the current page, and the page size.

Jump to

Keyboard shortcuts

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