util

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 15 Imported by: 13

Documentation

Index

Constants

View Source
const (
	DiffAdd string = "add"
	DiffDel string = "delete"
	DiffMod string = "modify"
	DiffEq  string = "eq"
)

Variables

View Source
var TplFuncMap = TplFuncMapType{
	"today": func(params []string) string {
		if len(params) == 0 {
			return time.Now().Format("2006-01-02")
		}
		return time.Now().Format(FromISO8601(params[0]))
	},
	"yesterday": func(params []string) string {
		yesterday := time.Now().AddDate(0, 0, -1)
		if len(params) == 0 {
			return yesterday.Format("2006-01-02")
		}
		return yesterday.Format(FromISO8601(params[0]))
	},
}
View Source
var TplVarMap = TplVarMapType{}

Functions

func BoolMaybeDeNil

func BoolMaybeDeNil(s *bool) bool

func Exists

func Exists(path string) (bool, error)

func FromISO8601 added in v0.7.3

func FromISO8601(isoFormat string) string

Convert ISO 8601 format string to Go's reference layout

func FuncName

func FuncName(skip int) string

func Int32to64

func Int32to64(i *int32) *int64

func IntMaybeDefault

func IntMaybeDefault(s *int, def int) int

func JsonStr

func JsonStr(r interface{}) string

func Jsonify

func Jsonify(r interface{}) json.RawMessage

func ListSubFolders

func ListSubFolders(path string) ([]string, error)

func Max

func Max(x, y int) int

Max returns the larger of x or y.

func MaybeAssignStr

func MaybeAssignStr(s **string, defStr string)

func MaybeCreate

func MaybeCreate(folder string) error

func MaybeDefault

func MaybeDefault[T any](s *T, v T) T

func MaybeNil

func MaybeNil[T any](s *T) (res T)

func Min

func Min(x, y int) int

Min returns the smaller of x or y.

func NewDiff added in v0.5.0

func NewDiff(path string, from, to DiffMarshaler) (*Diff, *Result)

func NewJsonDiff added in v0.5.0

func NewJsonDiff[T any](path string, from, to *T) (*Diff, *Result)

func NewString

func NewString(v string) *string

func Ptr

func Ptr[T any](v T) *T

func RenderTpl added in v0.7.3

func RenderTpl(input string) string

func SameResult added in v0.2.1

func SameResult(lh, rh *Result) bool

func StrMaybeDefault

func StrMaybeDefault(s *string, defStr string) string

func StrMaybeNil

func StrMaybeNil(s *string) string

func SumSlice

func SumSlice(s []int) int

Types

type Diff added in v0.5.0

type Diff struct {
	From []byte `json:"-" yaml:"-"`
	To   []byte `json:"-" yaml:"-"`
	Type string `json:"type" yaml:"type"`
	Path string `json:"path" yaml:"path"`
	Diff string `json:"diff" yaml:"diff"`
}

func (*Diff) String added in v0.5.3

func (d *Diff) String() string

type DiffMarshaler added in v0.5.0

type DiffMarshaler interface {
	MarshalForDiff() ([]byte, *Result)
}

type Result

type Result struct {
	Code      int         `json:"code"`
	Msg       string      `json:"message"`
	Ctx       string      `json:"context"`
	Timestamp time.Time   `json:"timestamp"`
	Result    interface{} `json:"result"`
	Inner     *Result     `json:"inner,omitempty"`
}

func ErrResult

func ErrResult(err error) *Result

func Error

func Error(ctx string, err error) *Result

func Errorf

func Errorf(format string, a ...interface{}) *Result

func FilterFiles added in v0.7.1

func FilterFiles(folder, pattern string) ([]string, *Result)

func GetAPIUrl

func GetAPIUrl(baseUrl, endpoint string) (string, *Result)

func GetUrl

func GetUrl(baseUrl, endpoint string) (string, *Result)

func ListFiles

func ListFiles(folder string) ([]string, *Result)

will return full path.

func LogError added in v0.5.2

func LogError(l *zerolog.Logger, ctx string, err error) *Result

func LogMsgError added in v0.4.2

func LogMsgError(logger *zerolog.Logger, ctx string, msg string) *Result

func MoveFile added in v0.6.1

func MoveFile(src, dst string) *Result

func MsgError

func MsgError(ctx string, msg string) *Result

func NewErrResult

func NewErrResult(ctx string, r interface{}) *Result

func NewResult

func NewResult(ctx string, r interface{}) *Result

func OK

func OK(ctx string) *Result

func StupidDeepCopy added in v0.6.2

func StupidDeepCopy[T any](v *T) (*T, *Result)

func (*Result) Error

func (r *Result) Error() string

func (Result) GetRootCause

func (r Result) GetRootCause() *Result

func (*Result) LogWith

func (r *Result) LogWith(logger *zerolog.Logger, ctx string) *Result

func (*Result) With

func (r *Result) With(ctx string) *Result

type TplFuncMapFunc added in v0.7.3

type TplFuncMapFunc func([]string) string

TplFuncMapFunc defines a function type that processes a slice of strings as parameter list and returns a single string as function result.

type TplFuncMapType added in v0.7.3

type TplFuncMapType map[string]TplFuncMapFunc

TplFuncMapType defines a map where keys are function names, and values are functions processing string params to return a string result.

type TplVarMapFunc added in v0.7.3

type TplVarMapFunc func(string) string

TplVarMapFunc defines a function type that transform var-name to var-value.

type TplVarMapType added in v0.7.3

type TplVarMapType map[string]TplVarMapFunc

TplVarMapType defines a map where keys are var-name, and values are function that generate var-value.

Jump to

Keyboard shortcuts

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