Util

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func ExpandEnv

func ExpandEnv(data []byte) []byte

ExpandEnv expand env variables, format: ${env}, ${env||default}

func FormatLanguage

func FormatLanguage(lang string) string

FormatLanguage format lang to ll-CC format

func FormatVersion

func FormatVersion(ver string, minDepth int) string

FormatVersion format version to have minimum depth, eg. FormatVersion("v10...2....2.1-alpha", 5) == "v10.2.2.1.0-alpha"

func GenUniqueId

func GenUniqueId() string

GenUniqueId generate a 24 bytes unique id

func HashSha1Crc32

func HashSha1Crc32(s string) uint32

func IsAllDigit

func IsAllDigit(s string) bool

func IsAllLetter

func IsAllLetter(s string) bool

func IsAllLower

func IsAllLower(s string) bool

func IsAllUpper

func IsAllUpper(s string) bool

func MapClear

func MapClear(m map[string]interface{})

MapClear clear map to empty

func MapGet

func MapGet(m map[string]interface{}, key string) interface{}

MapGet get value by dot separated key, empty key for m itself

func MapMerge

func MapMerge(a map[string]interface{}, m ...map[string]interface{})

MapMerge merge map recursively

func MapSet

func MapSet(m map[string]interface{}, key string, val interface{})

MapSet set value by dot separated key, empty key for root, nil val for clear

func Md5Bytes

func Md5Bytes(v interface{}) []byte

Md5Bytes get 16 bytes of md5

func Md5String

func Md5String(v interface{}) string

Md5String get 32 bytes of md5 hex string

func PanicTrace

func PanicTrace(maxDepth int, multiLine bool) string

PanicTrace get panic trace

func STMergeField

func STMergeField(s1, s2 interface{})

STMergeField merge the same or compatible field of s2 into s1, zero and unexported field will be skipped and embedded struct field will be treated as a single field.

func STMergeSame

func STMergeSame(s1, s2 interface{})

STMergeSame merge none zero field of s2 into s1, s1 and s2 must have the same struct type. unexported field will be skipped and embedded struct field will be treated as a single field.

func SliceSearch

func SliceSearch(n int, f func(int) bool) int

SliceSearch search in a slice has the length of n, return the first position where f(i) is true, -1 is returned if nothing found.

func SliceSearchInt

func SliceSearchInt(a []int, x int) int

SliceSearchInt search x in an int slice, return the first position of x, -1 is returned if nothing found.

func SliceSearchString

func SliceSearchString(a []string, x string) int

SliceSearchString search x in a string slice, return the first position of x, -1 is returned if nothing found.

func SliceUniqueInt

func SliceUniqueInt(a []int) []int

SliceUniqueInt retrieve unique items item from int slice.

func SliceUniqueString

func SliceUniqueString(a []string) []string

SliceUniqueString retrieve unique string items from string slice.

func ToBool

func ToBool(v interface{}) bool

func ToFloat

func ToFloat(v interface{}) float64

func ToInt

func ToInt(v interface{}) int

func ToString

func ToString(v interface{}) string

func VersionCompare

func VersionCompare(ver1, ver2 string) int

VersionCompare compare versions like version_compare of php, special version strings these are handled in the following order, (any string not found) < dev < alpha = a < beta = b < rc < #(empty) < ##(digit) < pl = p, result: -1(ver1 < ver2), 0(ver1 == ver2), 1(ver1 > ver2)

func YamlDecode

func YamlDecode(r io.Reader, out interface{}, strict ...bool) error

YamlDecode wrapper for yaml.Decoder, strict is for Decoder.SetStrict(). if type of out is map[string]interface{}, *map[string]interface{}, the inner map[interface{}]interface{} will fix to map[string]interface{} recursively. if type of out is *interface{}, the underlying type of out will change to *map[string]interface{}.

func YamlEncode

func YamlEncode(w io.Writer, in interface{}) error

YamlEncode wrapper for yaml.Encoder.

func YamlMarshal

func YamlMarshal(in interface{}) ([]byte, error)

YamlMarshal wrapper for yaml.Marshal.

func YamlUnmarshal

func YamlUnmarshal(in []byte, out interface{}, strict ...bool) error

YamlUnmarshal wrapper for yaml.Unmarshal or yaml.UnmarshalStrict, if type of out is map[string]interface{}, *map[string]interface{}, the inner map[interface{}]interface{} will fix to map[string]interface{} recursively. if type of out is *interface{}, the underlying type of out will change to *map[string]interface{}.

Types

type HashFunc

type HashFunc func(string) uint32

type HashRing

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

func NewHashRing

func NewHashRing(args ...interface{}) *HashRing

new hash ring, optional args: node string: node item key, multi node accepted, see AddNode(). spots int: num of virtual spots for each node, default 32. hashFunc HashFunc: function to calculate hash value, default HashSha1Crc32. eg. h := NewHashRing("127.0.0.1:6379", "127.0.0.1:6379", "127.0.0.1:6380")

func (*HashRing) AddNode

func (h *HashRing) AddNode(node string, w ...int)

add node to hash ring with default 1 weight, if node add multiple times, it gets a proportional amount of weight.

func (*HashRing) DelNode

func (h *HashRing) DelNode(node string)

remove node from hash ring

func (*HashRing) GetNode

func (h *HashRing) GetNode(key string) string

get node from hash ring by the hash value of key

Jump to

Keyboard shortcuts

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