urlparser

package
v1.8.0 Latest Latest
Warning

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

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

Documentation

Overview

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

Example usage:

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

// Define a TLS config
tlsConfig := &tls.Config{
	InsecureSkipVerify: true,
	MinVersion:         tls.VersionTLS13,
	CipherSuites:       []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
	PreferServerCipherSuites: true,
	ServerName: "localhost",
	ClientAuth: tls.RequireAndVerifyClientCert,
}

// Encode the tls.Config as a JSON string
tlsConfigJSON, _ := json.Marshal(tlsConfig)
tlsConfigStr := string(tlsConfigJSON)

// Create a URL with query parameters
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 DefaultHooks

func DefaultHooks() mapstructure.DecodeHookFunc

DefaultHooks returns the default decode hooks used by the URLParser.

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

type URLParser

type URLParser struct {
	// contains filtered or unexported fields
}

URLParser is a utility for parsing url.URL query parameters into a given struct. It uses the mapstructure package to decode query parameters into the struct fields. It also supports custom decode hooks for specific types.

func NewURLParser

func NewURLParser(decodeHooks ...mapstructure.DecodeHookFunc) *URLParser

NewURLParser 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 (*URLParser) OptionsFromURL

func (p *URLParser) OptionsFromURL(u *url.URL, options interface{}, paramKeyBlacklist map[string]bool) error

OptionsFromURL parses the query parameters from the given url.URL into the provided options struct. It uses the mapstructure.DecodeHookFunc hooks provided when creating the URLParser to convert query parameters into the correct types for the struct fields. It ignores any query parameters whose keys are in the paramKeyBlacklist. It returns an error if it fails to parse the url.URL or convert the query parameters.

Example:

type Options struct {
    MaxRetries      int
    MinRetryBackoff time.Duration
	DB              int
}

u, _ := url.Parse("fake://localhost:6379?maxretries=5&minretrybackoff=512ms&db=4")
options := &Options{}
bl := map[string]bool{"db": true}
err := parser.OptionsFromURL(u, options, bl)

After running this code, the options struct will be:

Options{
	MaxRetries:      5,
	MinRetryBackoff: 512 * time.Millisecond,
	DB:              0, // db is blacklisted and not set
}

Jump to

Keyboard shortcuts

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