pandati

package module
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 14 Imported by: 2

README

Pandati

Docker image build. GitHub release (latest by date) Go Reference

The one stop shop for most common Go functions

Table of contents

Purpose of the project

Keeping things DRY for your projects. Set of most popular and frequently used helper functions making it easier to focus on the logic you want to implement rather than re-inventing the wheel ( and spending precious time going through StackOverflow ).

Available helpers
Class Function Description
Errors CheckForError(err error, msg ...string) (string) Checks for error and returns pre-formatted message with stack trace
Errors Trace() string Returns a formatted stack trace
Maps FlattenMap(nested map[string]interface{}, opts *FlattenOptions) (m map[string]interface{}, err error) Flattens provided map using customisation options
Maps UnflattenMap(flat map[string]interface{}, opts *FlattenOptions) (nested map[string]interface{}, err error) Unflattens provided map into nested map using customisation options
Slices ExistsInSlice(slice interface{}, value interface{}) bool Checks if value exists in slice
Slices RemoveFromSlice(slice interface{}, value interface{}) Removes value from slice
Slices RemoveFromSliceByIndex(slice interface{}, index int) Removes value from slice by index
Slices UniqueSlice (slice interface{}) (uniqueSlice interface{}) Returns a slice with unique values
Structs CompareStructsReplacedFields (old interface{}, new interface{}) (changedFields []string) Compares two structs and returns a slice of changed fields
Checks IsZero(v interface{}) bool Checks if value of anything passed is zero / empty / nil
Conversion StringToBytes(s string) []byte Converts string to byte without unnecessary allocations
Conversion BytesToString(b []byte) string Converts byte to string without unnecessary allocations
Conversion ConvertReplyType[T any](desiredType T, reply []byte) T Converts any []byte into desired type
Logging

Additional helper for logger setup is available. I've decided to publish it to DRY my own projects. The logging itself provides nice, clean and fully customizable json formatted logging which then can be ingested without any further modifications straight into your loki, ELK or any other logging mechanism. It can be used as following.

import (
    "github.com/lukaszraczylo/pandati"
)

func main() {
    log = pandati.NewLogger()
    log.Critical("Error binding to queue", map[string]interface{}{"_queue": queueName, "_error": err.Error()})
}
Contributing

If you have any suggestions or want to contribute to the project, please open an issue or create a pull request. Pull requests should contain test cases and documentation describing the added functionality.

Name of the project

It's one of the sweet names I use to call my wife. She's always full of ideas and able to help with absolutely everything.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString added in v0.0.20

func BytesToString(b []byte) string

BytesToString converts a byte slice to a string without copying the underlying data.

func CheckForError

func CheckForError(err error, msg ...string) (toReturn string)

CheckForError takes err and additional message as parameter, Returning pre-formatted error message with stack trace if err is not nil

func ConvertReplyType added in v0.0.22

func ConvertReplyType[T any](desiredType T, reply []byte) T

Convert any []byte reply to the desired reply type Useful for converting json into structs.

func ExistsInSlice

func ExistsInSlice(slice interface{}, value interface{}) bool

ExistsInSlice returns true if the given value exists in the given slice.

func FlattenMap added in v0.0.9

func FlattenMap(nested map[string]interface{}, opts *FlattenOptions) (m map[string]interface{}, err error)

Returns flat map from the provided nested map[string]interface{} For example:

nested := map[string]interface{}{
	"a": map[string]interface{}{
		"b": map[string]interface{}{
			"c": "d",
		},
	},
}

flat := FlattenMap(nested)

flat equals to map[string]interface{}{
	"a.b.c": "d",
}

func IsZero

func IsZero(v interface{}) bool

IsZero reports whether v is zero struct Does not support cycle pointers for performance, so as json

func RemoveFromSlice

func RemoveFromSlice(slice interface{}, value interface{})

RemoveFromSlice removes the given value from the given slice. The value is replaced with Zero value of the same type. For string it is an empty string. For integers it is 0.

func RemoveFromSliceByIndex

func RemoveFromSliceByIndex(slice interface{}, index int)

RemoveFromSlice removes the given value from the given slice using provided index. The value is replaced with Zero value of the same type. For string it is an empty string. For integers it is 0.

func StringToBytes added in v0.0.20

func StringToBytes(s string) []byte

StringToBytes converts a string to a byte slice without copying the underlying data.

func Trace

func Trace() string

Trace returns trace information about executing function together line number and file name It's useful for debugging purposes

func UnflattenMap added in v0.0.9

func UnflattenMap(flat map[string]interface{}, opts *FlattenOptions) (nested map[string]interface{}, err error)

Unflatten the map, it returns a nested map of a map By default, the flatten has Separator equal to "."

Example:

flat := map[string]interface{}{
	"a.b.c": "d",
}

nested := UnflattenMap(flat)

nested equals to map[string]interface{}{
	"a": map[string]interface{}{
		"b": map[string]interface{}{
			"c": "d",
		},
	},
}

func UniqueSlice added in v0.0.12

func UniqueSlice(slice interface{}) interface{}

UniqueSlice returns slice with reordered and unique values. Please note that function does not modify the original slice but returns a new one.

Types

type CompareStructReplacedResult added in v0.0.15

type CompareStructReplacedResult struct {
	Value    interface{}
	OldValue interface{}
	Key      string
}

func CompareStructsReplaced added in v0.0.15

func CompareStructsReplaced(a, b interface{}) ([]CompareStructReplacedResult, bool, error)

CompareStructReplaced compares two structs after converting them into json and returns: - array of differences - bool true if there's no differences - error if such occurs

type FlattenOptions added in v0.0.9

type FlattenOptions struct {
	Separator string
	Safe      bool
	MaxDepth  int
}

type LogConfig added in v0.0.18

type LogConfig struct {
	DefaultOutput *os.File
	Logger        zerolog.Logger
}

func NewLogger added in v0.0.18

func NewLogger() *LogConfig

func (*LogConfig) Critical added in v0.0.18

func (l *LogConfig) Critical(message string, v ...map[string]interface{})

func (*LogConfig) Debug added in v0.0.18

func (l *LogConfig) Debug(message string, v ...map[string]interface{})

func (*LogConfig) Error added in v0.0.18

func (l *LogConfig) Error(message string, v ...map[string]interface{})

func (*LogConfig) Info added in v0.0.18

func (l *LogConfig) Info(message string, v ...map[string]interface{})

func (*LogConfig) Warning added in v0.0.18

func (l *LogConfig) Warning(message string, v ...map[string]interface{})

Jump to

Keyboard shortcuts

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