assists

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

Package assists provides some assisted functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertAddr added in v0.11.0

func ConvertAddr(netaddr net.Addr) (addr netip.Addr)

ConvertAddr converts the address from net.Addr to netip.Addr.

If failed, return ZERO.

func IP2Addr added in v0.12.0

func IP2Addr(ip net.IP) (addr netip.Addr)

IP2Addr converts net.IP to netip.Addr, which also converts 4in6 to ipv4.

If ip is invalid, the returned addr is also invalid.

func StructFieldNameFuncWithTags

func StructFieldNameFuncWithTags(tags ...string) func(reflect.StructField) (name string, arg string)

StructFieldNameFuncWithTags returns a function to get the name and arg of struct field from the given tags in turn until a tag is found.

func TrimPort added in v0.13.0

func TrimPort(host string) string

TrimPort trims the port from the host if exists. Or, returns itself.

host may be one of formats as follow:

"ipv4"
"ipv6"
"domain"
"ipv4:port"
"[ipv6]:port"
"domain:port"

Any that is not an ipv4 or ipv6 is as a domain.

NOTICE: it does not validate whether the host is valid.

Example
fmt.Println(TrimPort(""))
fmt.Println(TrimPort("[abc"))
fmt.Println(TrimPort("[abc]"))
fmt.Println(TrimPort("[abc]:80")) // We just trim it by the lexical rule, not check its validity.
fmt.Println(TrimPort("[ff00::]:80"))
fmt.Println(TrimPort("ff00::"))
fmt.Println(TrimPort("1.2.3.4"))
fmt.Println(TrimPort("1.2.3.4:80"))
fmt.Println(TrimPort("localhost"))
fmt.Println(TrimPort("localhost:80"))
Output:


[abc
abc
abc
ff00::
ff00::
1.2.3.4
1.2.3.4
localhost
localhost

Types

type RuleValidateFunc

type RuleValidateFunc func(value any, rule string) error

RuleValidateFunc is a rule validation function.

func (RuleValidateFunc) Validate

func (f RuleValidateFunc) Validate(value any, rule string) error

Validate implements the interface RuleValidator.

type RuleValidator

type RuleValidator interface {
	Validate(value any, rule string) error
}

RuleValidator is used to validate whether a value conforms with the rule.

type StructValidateFunc

type StructValidateFunc func(structvalue any) error

StructValidateFunc is a struct validation function.

func (StructValidateFunc) Validate

func (f StructValidateFunc) Validate(structvalue any) error

Validate implements the interface Validator.

type StructValidator

type StructValidator interface {
	Validate(structvalue any) error
}

StructValidator is used to validate whether a struct value is valid.

Jump to

Keyboard shortcuts

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