goutils

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: Apache-2.0 Imports: 28 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenInvalid denotes a token was not able to be validated.
	ErrTokenInvalid = errors.New("JWT Token was invalid")
	// ErrTokenExpired denotes a token'vs expire header (exp) has since passed.
	ErrTokenExpired = errors.New("JWT Token is expired")
	// ErrTokenMalformed denotes a token was not formatted as a JWT token.
	ErrTokenMalformed = errors.New("JWT Token is malformed")
	// ErrTokenNotActive denotes a token's not before header (nbf) is in the
	// future.
	ErrTokenNotActive = errors.New("Token is not valid yet")
	// ErrTokenUnexpectedSigningMethod denotes a token was signed with an unexpected
	// signing method.
	ErrTokenUnexpectedSigningMethod = errors.New("Token unexpected signing method")
	// ErrTokenUnableGetCAPublic denotes not being able to get CA Public Key
	ErrTokenUnableGetCAPublic = errors.New("Token cannot get CA Public Key")
	// ErrTokenKidNotFound denotes token's key identifier not found
	ErrTokenKidNotFound = errors.New("Token's Key Identifier not found")
)

Functions

func BytesConvertToUTF8 added in v0.2.0

func BytesConvertToUTF8(bytes []byte, charset string) ([]byte, error)

BytesConvertToUTF8 converts bytes from a given encoding to UTF8

func BytesMarshal

func BytesMarshal(i interface{}) ([]byte, error)

BytesMarshal converts a type interface to byte array

func BytesUnMarshal

func BytesUnMarshal(data []byte, i interface{}) error

BytesUnMarshal put data of a byte array to a type interface

func ExecCmdPipe

func ExecCmdPipe(command string, args []string, o io.Writer, e io.Writer) ([]byte, []byte, error)

ExecCmdPipe executes command shell. It redirects output and errors to standard and then stores in a bytes. It returns command's stdout and stderr in bytes and error of command execution

func FileConvertToUTF8 added in v0.2.0

func FileConvertToUTF8(file io.Reader, charset string) (io.Reader, error)

FileConvertToUTF8 converts a file from a given encoding to UTF-8

func FileGuessEncoding added in v0.2.0

func FileGuessEncoding(bytes []byte) string

FileGuessEncoding tries to guess the encoding of bytes.

It recognizes UTF-8, UTF-32LE, UTF-32BE, UTF-16LE, UTF-16BE, windows-1252, iso-8859-1.

func InitZapLog

func InitZapLog(v *viper.Viper) (*zap.Logger, error)

func MapGetKeys

func MapGetKeys(m interface{}) ([]interface{}, error)

MapGetKeys returns a slice of keys.

func MapGetSortedKeys

func MapGetSortedKeys(m interface{}, reverse bool) interface{}

MapGetSortedKeys returns a sorted slice contaning keys of map as interface{}// and need to be casted in the format as you want

This function supports the following type of key: string, int If the given parameter is not a map, it will return nil

func MapRemoveNulls

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

MapRemoveNulls removes all fields with nil value

func MapStringMerge

func MapStringMerge(map1, map2 interface{}) (interface{}, error)

MapStringMerge merges 2 string maps of the same type: map2 in map1.

If an element in map1 does not exist, it will be created. Otherwise, it will be overrided by new value in map2

func SliceFindElemStr

func SliceFindElemStr(slice []string, val string) int

SliceFindElemStr takes a slice and looks for an element in it. If found it will return the element's position. Otherwise it will return -1

func SliceIsElemIn

func SliceIsElemIn(array []interface{}, elem interface{}, fn CompareFunc) int

SliceIsElemIn checks if an element is in an array of elements of the same type

func StringBuild

func StringBuild(strs ...string) string

StringBuild concatenates strings together

func StringBuildWithSep

func StringBuildWithSep(sep rune, strs ...string) string

StringBuildWithSep concatenates strings together with a separator

func StringConvertToMapSimple

func StringConvertToMapSimple(str, elSep, kvSep string) (map[string]interface{}, error)

StringConvertToMapSimple converts a string composed of key/value elements separated by a separator to a map. By default, the separator for key and value is ":" and the separator for each key/value element is ";"

func StringParseFloat

func StringParseFloat(str string) (float64, error)

StringParseFloat is an advance ParseFloat for golang, support scientific notation, comma separated number.

Examples: StringParseFloat("1E2") = 100) StringParseFloat("1E-5") = 0.00001) StringParseFloat("1.6543E2") = 165.43) StringParseFloat("0.89E2") = 89) StringParseFloat("1.6543E-2") = 0.016543) StringParseFloat("156,819,129") = 156819129) StringParseFloat("156819129") = 156819129) StringParseFloat(".1E0") = 0.1) StringParseFloat(".1E1") = 1) StringParseFloat("0E1") = 0)

func StringStripAnsi

func StringStripAnsi(str string) string

StringStripAnsi removes ANSI escape code from string

Types

type CompareFunc

type CompareFunc func(elem1 interface{}, elem2 interface{}) bool

CompareFunc is utility func to compare 2 elements

type JWTToken

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

JWTToken represents struct containing signing key or jwks°uri

func NewJWTToken

func NewJWTToken() *JWTToken

NewJWTToken returns a new JWTToken

func (*JWTToken) ParseToken

func (t *JWTToken) ParseToken(tokenString string) (map[string]interface{}, error)

ParseToken parses and checks to see if JWT is valid. If the token is valid, it returns a map of all claims defined in the JWT.

func (*JWTToken) SetJwksURI

func (t *JWTToken) SetJwksURI(uri string)

SetJwksURI sets jwksURI to validate token

func (*JWTToken) SetSignKey

func (t *JWTToken) SetSignKey(key string)

SetSignKey set signKey to use when jwksURI is not used. It will be used to sign/verify the token.

func (*JWTToken) SetSignMethod

func (t *JWTToken) SetSignMethod(method string)

SetSignMethod set signing method to sign or verify the token

Jump to

Keyboard shortcuts

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