urlparser

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package urlparser provides utilities for parsing URL query parameters into a given struct. It uses the mapstructure package to decode query parameters into struct fields and supports custom decode hooks for specific types.

Example:

type Options struct {
    MaxRetries      int
    MinRetryBackoff time.Duration
    TLSConfig       *tls.Config
}

const tlsConfigStr = `{"InsecureSkipVerify":true}`

urlStr := "fake://localhost:6379?maxretries=5&minretrybackoff=512ms&tlsconfig=" + url.QueryEscape(tlsConfigStr)
u, _ := url.Parse(urlStr)
options := &Options{}
parser := NewURLParser(mapstructure.StringToTimeDurationHookFunc(), StringToTLSConfigHookFunc())
err := parser.OptionsFromURL(u, options, map[string]bool{"db": true})

After running this code, the options struct will have MaxRetries set to 5, MinRetryBackoff set to 512ms, and TLSConfig set to the corresponding tls.Config object.

Note: This package does not handle URL parsing itself. It expects a *url.URL as input. It also does not set any fields in the struct that are not present in the URL query parameters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New added in v0.15.0

func New(decodeHooks ...mapstructure.DecodeHookFunc) *urlParser

New creates a new [urlParser] with the given mapstructure.DecodeHookFunc hooks. Decode hooks are functions that can convert query parameters into specific types. They are called in the order they are provided.

func StringToCertificateHookFunc

func StringToCertificateHookFunc() mapstructure.DecodeHookFuncType

StringToCertificateHookFunc creates a decode hook for converting a pem encoded x509.Certificate string into a pointer to an x509.Certificate.

func StringToTLSConfigHookFunc

func StringToTLSConfigHookFunc() mapstructure.DecodeHookFuncType

StringToTLSConfigHookFunc creates a decode hook for converting a json encoded tls.Config string into a pointer to a tls.Config.

Types

This section is empty.

Jump to

Keyboard shortcuts

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