utils

package
v0.0.0-...-d841f61 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Regexp2MatchTimeout       = "REGEXP2_MATCH_TIMEOUT"
	DefaultRegex2MatchTimeout = time.Second
)
View Source
const NamingCharacterSet = "abcdefghijklmnopqrstuvwxyz1234567890"

Variables

View Source
var NumberOfCharacter = int64(len(NamingCharacterSet))

Functions

func ApplyTranslation

func ApplyTranslation(element interface{}, key, formatString string) (string, error)

ApplyTranslation translate a templated string using informations in an Event

func AsString

func AsString(value interface{}) (string, bool)

AsString tries to convert an interface{} into a string, and returns its value and an integer indicating whether it succeeded or not.

func ExitOnError

func ExitOnError(err error, msg string)

ExitOnError calls log.Fatalf if err != nil

func FailOnError

func FailOnError(err error, msg string)

FailOnError calls log.Panicf if err != nil

func FindStringSubmatchMap

func FindStringSubmatchMap(re *regexp.Regexp, s string) map[string]string

FindStringSubmatchMap returns a map containing the values of the named subexpressions of the lefmost match of the regular expression re in s. A return value of nil indicates no match.

func FindStringSubmatchMapWithRegexExpression

func FindStringSubmatchMapWithRegexExpression(re RegexExpression, s string) map[string]string

FindStringSubmatchMap returns a map containing the values of the named subexpressions of the lefmost match of the regular expression re in s. A return value of nil indicates no match.

func GetField

func GetField(object interface{}, fieldPath string) (interface{}, error)

GetField returns the value of a field of an object or a map, given the field's name as a string. Multiple field names may be chained, separated by dots:

GetField(alarm_value, "State.Value")

An error is returned if the field does not exist. If the field is a pointer, it will be dereferenced before being returned. Note that GetField cannot return map keys that contain a dot.

func GetStringField

func GetStringField(a interface{}, field string) string

GetStringField read a string field in a struct, with introspection

func NewID

func NewID() string

NewID generate an uuid

func RandString

func RandString(n int) string

RandString generate a random string

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile handles opening and reading file at given path

func StringInSlice

func StringInSlice(a string, list []string) bool

StringInSlice checks if a string is present in a list of strings

func TimeTrack

func TimeTrack(start time.Time, name string)

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase changes case of string to snake case.

func TruncateString

func TruncateString(s string, chars int) string

Types

type OptionalBool

type OptionalBool struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the bool. It should only be taken into
	// account if Set is true.
	Value bool
}

OptionalBool is a wrapper around bool that implements the bson.Setter interface.

Using this type instead of bool in a struct allows to :

  • check whether the value was set or not in the bson document.
  • raise an error when trying to unmarshal a value that is not an integer.

Note that when trying to unmarshal a value that is not a bool, UnmarshalBSONValue will raise an error that will not be handled by bson.Unmarshal. If this error is not handled in the UnmarshalBSONValue method of an ancestor, calls to MongoDB queries may fail.

func (OptionalBool) MarshalBSONValue

func (s OptionalBool) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalBool) UnmarshalBSONValue

func (s *OptionalBool) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalInt64

type OptionalInt64 struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the int64. It should only be taken into
	// account if Set is true.
	Value int64
}

OptionalInt64 is a wrapper around int64 that implements the bson.Setter interface.

Using this type instead of int64 in a struct allows to :

  • check whether the value was set or not in the bson document.
  • raise an error when trying to unmarshal a value that is not an integer.

Note that when trying to unmarshal a value that is not an integer, UnmarshalBSONValue will raise an error that will not be handled by bson.Unmarshal. If this error is not handled in the UnmarshalBSONValue method of an ancestor, calls to MongoDB queries may fail.

func (OptionalInt64) MarshalBSONValue

func (i OptionalInt64) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalInt64) UnmarshalBSONValue

func (i *OptionalInt64) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalInterface

type OptionalInterface struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the interface{}. It should only be taken
	// into account if Set is true.
	Value interface{}
}

OptionalInterface is a wrapper around interface{} that implements the bson.Setter interface.

Using this type instead of interface{} in a struct allows to check whether the value was set or not in the bson document.

func (OptionalInterface) MarshalBSONValue

func (i OptionalInterface) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalInterface) UnmarshalBSONValue

func (i *OptionalInterface) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalRegexp

type OptionalRegexp struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the regular expression. It should only be
	// taken into account if Set is true.
	Value RegexExpression
}

OptionalRegexp is a wrapper around regexp.Regexp that implements the bson.Setter interface.

Using this type in a struct allows to :

  • check whether the value was set or not in the bson document.
  • automatically compile a regular expression.
  • raise an error when trying to unmarshal a value that is not a valid regular expression.

Note that when trying to unmarshal a value that is not a valid regular expression, UnmarshalBSONValue will raise an error that will not be handled by bson.Unmarshal. If this error is not handled in the UnmarshalBSONValue method of an ancestor, calls to MongoDB queries may fail.

func (OptionalRegexp) MarshalBSONValue

func (r OptionalRegexp) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalRegexp) UnmarshalBSONValue

func (r *OptionalRegexp) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalString

type OptionalString struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the string. It should only be taken into
	// account if Set is true.
	Value string
}

OptionalString is a wrapper around string that implements the bson.Setter interface.

Using this type instead of string in a struct allows to :

  • check whether the value was set or not in the bson document.
  • raise an error when trying to unmarshal a value that is not an integer.

Note that when trying to unmarshal a value that is not a string, UnmarshalBSONValue will raise an error that will not be handled by bson.Unmarshal. If this error is not handled in the UnmarshalBSONValue method of an ancestor, calls to MongoDB queries may fail.

func (OptionalString) MarshalBSONValue

func (s OptionalString) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalString) UnmarshalBSONValue

func (s *OptionalString) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalStringArray

type OptionalStringArray struct {
	Set   bool
	Value []string
}

func (OptionalStringArray) MarshalBSONValue

func (a OptionalStringArray) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalStringArray) UnmarshalBSONValue

func (a *OptionalStringArray) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type OptionalTemplate

type OptionalTemplate struct {
	// Set is a boolean indicating whether the value was set or not.
	Set bool

	// Value contains the value of the regular expression. It should only be
	// taken into account if Set is true.
	Value *template.Template
}

OptionalTemplate is a wrapper around template.Template that implements the bson.Setter interface.

Using this type in a struct allows to :

  • check whether the value was set or not in the bson document.
  • automatically compile a template.
  • raise an error when trying to unmarshal a value that is not a valid template.

Note that when trying to unmarshal a value that is not a valid template, UnmarshalBSONValue will raise an error that will not be handled by bson.Unmarshal. If this error is not handled in the UnmarshalBSONValue method of an ancestor, calls to MongoDB queries may fail.

func (OptionalTemplate) MarshalBSONValue

func (t OptionalTemplate) MarshalBSONValue() (bsontype.Type, []byte, error)

func (*OptionalTemplate) UnmarshalBSONValue

func (t *OptionalTemplate) UnmarshalBSONValue(valueType bsontype.Type, b []byte) error

type RegexExpression

type RegexExpression interface {
	Match([]byte) bool
	String() string
}

func NewRegexExpression

func NewRegexExpression(expr string) (RegexExpression, error)

type WrapperBuiltInRegex

type WrapperBuiltInRegex struct {
	*regexp.Regexp
}

type WrapperRegex2

type WrapperRegex2 struct {
	*regexp2.Regexp
}

func (WrapperRegex2) Match

func (r WrapperRegex2) Match(content []byte) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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