utils

package
v0.0.0-...-b824934 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MapMergeModeReplace MapMergeMode = "replace"
	MapMergeModeInsert  MapMergeMode = "insert"
	MapMergeModeRootKey string       = "$root"
)

Variables

View Source
var JsonSerializerDefault = NewJsonSerializer("", "  ")
View Source
var TomlSerializerDefault = NewTomlSerializer(false, true, true, "  ")
View Source
var YamlSerializerDefault = NewYamlSerializer(2)

Functions

func Cast

func Cast(value any, typ CastType) (any, error)

func CastSlice

func CastSlice[T any](slice []T, typ CastType) ([]any, error)

func CastToArray

func CastToArray(value any) ([]any, error)

func CastToBool

func CastToBool(value any) (*bool, error)

func CastToDecimal

func CastToDecimal(value any) (*float64, error)

func CastToInteger

func CastToInteger(value any) (*int64, error)

func CastToObject

func CastToObject(value any) (map[string]any, error)

func CastToString

func CastToString(value any) (*string, error)

func CheckRuntimeVersion

func CheckRuntimeVersion(minVersion Version, maxVersion Version) (err error)

func ClearDir

func ClearDir(dir string) (err error)

func ConvertChildModels

func ConvertChildModels[R any, M Model[R]](helper *ModelHelper, field string, models []M) ([]R, error)

func CopyFile

func CopyFile(sourceFile string, targetFile string) (err error)

func DescN

func DescN(title string, kvs ...DescKeyValue) string

func ErrN

func ErrN(title string, kvs ...DescKeyValue) error

func ErrW

func ErrW(err error, title string, kvs ...DescKeyValue) error

func EvalExpr

func EvalExpr(str string, data map[string]any, cast CastType) (any, error)

func EvalFileTemplate

func EvalFileTemplate(inputPath string, libraryPaths []string, outputPath string, data map[string]any, funcs template.FuncMap) error

func EvalStringTemplate

func EvalStringTemplate(str string, data map[string]any, funcs template.FuncMap) (string, error)

func FuncT

func FuncT[T any](expr bool, trueFunc func() T, falseFunc func() T) T

func GetFileNames

func GetFileNames(globs []string, types []FileType) []string

func GetRuntimeVersionCode

func GetRuntimeVersionCode() int32

func GetSystemArch

func GetSystemArch() string

func GetSystemCurrentDir

func GetSystemCurrentDir() (string, error)

func GetSystemHomeDir

func GetSystemHomeDir() (string, error)

func GetSystemHostname

func GetSystemHostname() (string, error)

func GetSystemOs

func GetSystemOs() string

func GetSystemUsername

func GetSystemUsername() (string, error)

func GetSystemVariables

func GetSystemVariables() map[string]string

func Impossible

func Impossible()

func IsConfigJsonFile

func IsConfigJsonFile(file string) bool

func IsConfigTomlFile

func IsConfigTomlFile(file string) bool

func IsConfigYamlFile

func IsConfigYamlFile(file string) bool

func IsDirExists

func IsDirExists(dir string) bool

func IsFileExists

func IsFileExists(file string) bool

func IsJsonFile

func IsJsonFile(file string) bool

func IsTemplateFile

func IsTemplateFile(file string) bool

func IsTemplateLibFile

func IsTemplateLibFile(file string) bool

func IsTomlFile

func IsTomlFile(file string) bool

func IsYamlFile

func IsYamlFile(file string) bool

func LinkFile

func LinkFile(sourceFile string, targetFile string) (err error)

func LinkOrCopyFile

func LinkOrCopyFile(sourceFile string, targetFile string) (err error)

func ListChildDirs

func ListChildDirs(dir string) (names []string, err error)

func MapAnyByStr

func MapAnyByStr[E any, M map[string]E](m M) map[string]any

func MapCopy

func MapCopy[K comparable, V any](source map[K]V) map[K]V

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) []K

func MapMerge

func MapMerge(target map[string]any, source map[string]any, merge map[string]MapMergeMode, label string, trace map[string]any) (map[string]any, map[string]any, error)

func MapValues

func MapValues[K comparable, V any](m map[K]V) []V

func ParseFloat32

func ParseFloat32(str string) (float32, error)

func ParseFloat64

func ParseFloat64(str string) (float64, error)

func ParseInt32

func ParseInt32(str string) (int, error)

func ParseInt64

func ParseInt64(str string) (int64, error)

func RandomString

func RandomString(len int) (string, error)

func ReadJsonFile

func ReadJsonFile(file string, model any) error

func ReadTomlFile

func ReadTomlFile(file string, model any) error

func ReadYamlFile

func ReadYamlFile(file string, model any) error

func RegexMatch

func RegexMatch(regex *regexp.Regexp, str string) (matched bool, values map[string]string)

func RemakeDir

func RemakeDir(dir string) (err error)

func RemoveFileExt

func RemoveFileExt(file string) string

func Ternary

func Ternary[T any](expr bool, trueValue T, falseValue T) T

func TernaryFunc

func TernaryFunc[T any](expr bool, trueFunc func() T, falseFunc func() T) T

func ValT

func ValT[T any](expr bool, trueValue T, falseValue T) T

Types

type CastType

type CastType string
const (
	CastTypeString  CastType = "string"
	CastTypeBool    CastType = "bool"
	CastTypeInteger CastType = "integer"
	CastTypeDecimal CastType = "decimal"
	CastTypeObject  CastType = "object"
	CastTypeArray   CastType = "array"
)

type Desc

type Desc struct {
	Title string
	Body  DescBody
}

func NewDesc

func NewDesc(title string, kvs DescKeyValues) Desc

func (Desc) String

func (d Desc) String() string

func (Desc) ToString

func (d Desc) ToString(titleIdent string, bodyIdent string) string

type DescBody

type DescBody []string

func NewDescBody

func NewDescBody(kvs DescKeyValues) DescBody

func (DescBody) String

func (b DescBody) String() string

func (DescBody) ToString

func (b DescBody) ToString(ident string) string

type DescExtraKeyValuesFunc

type DescExtraKeyValuesFunc interface {
	DescExtraKeyValues() DescKeyValues
}

type DescKeyValue

type DescKeyValue struct {
	Key   string
	Value any
	// contains filtered or unexported fields
}

func KV

func KV(key string, value any) DescKeyValue

func NewDescKeyValue

func NewDescKeyValue(key string, value any) DescKeyValue

func Reason

func Reason(reason any) DescKeyValue

func (DescKeyValue) DescKeyValues

func (kv DescKeyValue) DescKeyValues() DescKeyValues

func (DescKeyValue) String

func (kv DescKeyValue) String() string

func (DescKeyValue) ToString

func (kv DescKeyValue) ToString(keyPrefix string, valueLabel string) string

type DescKeyValues

type DescKeyValues []DescKeyValue

func (DescKeyValues) DescKeyValues

func (kvs DescKeyValues) DescKeyValues() DescKeyValues

type DescKeyValuesFunc

type DescKeyValuesFunc interface {
	DescKeyValues() DescKeyValues
}

type DescList

type DescList []Desc

func (DescList) String

func (l DescList) String() string

func (DescList) ToString

func (l DescList) ToString(titleIdent string, bodyIdent string) string

type Error

type Error struct {
	Details DescList
	Stacks  errors.StackTrace
	// contains filtered or unexported fields
}

func NewError

func NewError(skipStacks int, title string, kvs ...DescKeyValue) *Error

func WrapError

func WrapError(skipStacks int, err error, title string, kvs ...DescKeyValue) *Error

func (*Error) Cause

func (e *Error) Cause() error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

func (*Error) Unwrap

func (e *Error) Unwrap() error

type EvalData

type EvalData map[string]any

type EvalDataset

type EvalDataset map[string]EvalData

func (EvalDataset) MergeData

func (ds EvalDataset) MergeData(name string, data map[string]any) EvalDataset

func (EvalDataset) MergeDataset

func (ds EvalDataset) MergeDataset(dataset EvalDataset) EvalDataset

func (EvalDataset) SetData

func (ds EvalDataset) SetData(name string, data map[string]any) EvalDataset

func (EvalDataset) ToMap

func (ds EvalDataset) ToMap(root string, funcs EvalFuncs) map[string]any

type EvalFuncs

type EvalFuncs map[string]any

func (EvalFuncs) MergeFuncs

func (fs EvalFuncs) MergeFuncs(funcs EvalFuncs) EvalFuncs

func (EvalFuncs) SetFunc

func (fs EvalFuncs) SetFunc(name string, fn any) EvalFuncs

func (EvalFuncs) ToTemplateFuncMap

func (fs EvalFuncs) ToTemplateFuncMap() template.FuncMap

type Evaluator

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

func NewEvaluator

func NewEvaluator() *Evaluator

func (*Evaluator) ClearRoot

func (e *Evaluator) ClearRoot() *Evaluator

func (*Evaluator) DescExtraKeyValues

func (e *Evaluator) DescExtraKeyValues() KVS

func (*Evaluator) EvalBoolExpr

func (e *Evaluator) EvalBoolExpr(expr string) (bool, error)

func (*Evaluator) EvalExpr

func (e *Evaluator) EvalExpr(expr string, cast CastType) (any, error)

func (*Evaluator) EvalFileTemplate

func (e *Evaluator) EvalFileTemplate(inputPath string, libraryPaths []string, outputPath string) error

func (*Evaluator) EvalStringTemplate

func (e *Evaluator) EvalStringTemplate(str string) (string, error)

func (*Evaluator) GetData

func (e *Evaluator) GetData(name string) map[string]any

func (*Evaluator) GetMap

func (e *Evaluator) GetMap(includeFuncs bool) map[string]any

func (*Evaluator) MergeData

func (e *Evaluator) MergeData(name string, data map[string]any) *Evaluator

func (*Evaluator) MergeDataset

func (e *Evaluator) MergeDataset(dataset EvalDataset) *Evaluator

func (*Evaluator) MergeFuncs

func (e *Evaluator) MergeFuncs(funcs EvalFuncs) *Evaluator

func (*Evaluator) SetData

func (e *Evaluator) SetData(name string, data map[string]any) *Evaluator

func (*Evaluator) SetDataset

func (e *Evaluator) SetDataset(dataset EvalDataset) *Evaluator

func (*Evaluator) SetFunc

func (e *Evaluator) SetFunc(name string, fn any) *Evaluator

func (*Evaluator) SetFuncs

func (e *Evaluator) SetFuncs(funcs EvalFuncs) *Evaluator

func (*Evaluator) SetRoot

func (e *Evaluator) SetRoot(name string) *Evaluator

func (*Evaluator) SetRootData

func (e *Evaluator) SetRootData(name string, data map[string]any) *Evaluator

type File

type File struct {
	Path    string
	RelPath string
	Type    FileType
}

func FindFile

func FindFile(dir string, fileNames []string, fileTypes []FileType) *File

func ScanFiles

func ScanFiles(dir string, includes []string, excludes []string, types []FileType) (files []*File, err error)

type FileType

type FileType string
const (
	FileTypeConfigYaml  FileType = "config-yaml"
	FileTypeConfigToml  FileType = "config-toml"
	FileTypeConfigJson  FileType = "config-json"
	FileTypeTemplate    FileType = "template"
	FileTypeTemplateLib FileType = "template-lib"
	FileTypeYaml        FileType = "yaml"
	FileTypeToml        FileType = "toml"
	FileTypeJson        FileType = "json"
	FileTypePlain       FileType = "plain"
)

func GetFileType

func GetFileType(file string, types []FileType) FileType

type JsonSerializer

type JsonSerializer struct {
	PrefixSymbol string
	IndentSymbol string
}

func NewJsonSerializer

func NewJsonSerializer(prefixSymbol, indentSymbol string) *JsonSerializer

func (*JsonSerializer) GetFileExt

func (s *JsonSerializer) GetFileExt() string

func (*JsonSerializer) GetFormat

func (s *JsonSerializer) GetFormat() SerializationFormat

func (*JsonSerializer) SerializeFile

func (s *JsonSerializer) SerializeFile(file string, model any) error

func (*JsonSerializer) SetIndentSymbol

func (s *JsonSerializer) SetIndentSymbol(symbol string) *JsonSerializer

func (*JsonSerializer) SetPrefixSymbol

func (s *JsonSerializer) SetPrefixSymbol(symbol string) *JsonSerializer

type KVS

type KVS = DescKeyValues

type LogLevel

type LogLevel int
const (
	LogLevelAll   LogLevel = 0
	LogLevelDebug LogLevel = 1
	LogLevelInfo  LogLevel = 2
	LogLevelWarn  LogLevel = 3
	LogLevelError LogLevel = 4
	LogLevelNone  LogLevel = 5
)

type Logger

type Logger struct {
	Level LogLevel
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(level LogLevel) *Logger

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...any)

func (*Logger) DebugDesc

func (l *Logger) DebugDesc(title string, kvs ...DescKeyValue)

func (*Logger) Error

func (l *Logger) Error(format string, v ...any)

func (*Logger) ErrorDesc

func (l *Logger) ErrorDesc(title string, kvs ...DescKeyValue)

func (*Logger) Fatal

func (l *Logger) Fatal(format string, v ...any)

func (*Logger) FatalDesc

func (l *Logger) FatalDesc(title string, kvs ...DescKeyValue)

func (*Logger) GetDebugWriter

func (l *Logger) GetDebugWriter() io.Writer

func (*Logger) GetErrorWriter

func (l *Logger) GetErrorWriter() io.Writer

func (*Logger) GetFatalWriter

func (l *Logger) GetFatalWriter() io.Writer

func (*Logger) GetInfoWriter

func (l *Logger) GetInfoWriter() io.Writer

func (*Logger) GetPanicWriter

func (l *Logger) GetPanicWriter() io.Writer

func (*Logger) GetWarnWriter

func (l *Logger) GetWarnWriter() io.Writer

func (*Logger) Info

func (l *Logger) Info(format string, v ...any)

func (*Logger) InfoDesc

func (l *Logger) InfoDesc(title string, kvs ...DescKeyValue)

func (*Logger) IsDebugEnabled

func (l *Logger) IsDebugEnabled() bool

func (*Logger) IsErrorEnabled

func (l *Logger) IsErrorEnabled() bool

func (*Logger) IsInfoEnabled

func (l *Logger) IsInfoEnabled() bool

func (*Logger) IsWarnEnabled

func (l *Logger) IsWarnEnabled() bool

func (*Logger) Panic

func (l *Logger) Panic(format string, v ...any)

func (*Logger) PanicDesc

func (l *Logger) PanicDesc(title string, kvs ...DescKeyValue)

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...any)

func (*Logger) WarnDesc

func (l *Logger) WarnDesc(title string, kvs ...DescKeyValue)

type MapMergeMode

type MapMergeMode string

type Model

type Model[R any] interface {
	Convert(helper *ModelHelper) (R, error)
}

type ModelHelper

type ModelHelper struct {
	Logger    *Logger
	Title     string
	Source    string
	Field     string
	Variables map[string]any
}

func NewModelHelper

func NewModelHelper(logger *Logger, title, source string) *ModelHelper

func (*ModelHelper) AddVariable

func (h *ModelHelper) AddVariable(key string, value any) *ModelHelper

func (*ModelHelper) CheckStringItemEmpty

func (h *ModelHelper) CheckStringItemEmpty(field string, items []string) error

func (*ModelHelper) Child

func (h *ModelHelper) Child(field string) *ModelHelper

func (*ModelHelper) ChildItem

func (h *ModelHelper) ChildItem(field string, index int) *ModelHelper

func (*ModelHelper) GetStringVariable

func (h *ModelHelper) GetStringVariable(key string) string

func (*ModelHelper) Item

func (h *ModelHelper) Item(index int) *ModelHelper

func (*ModelHelper) NewError

func (h *ModelHelper) NewError(reason string, extra ...DescKeyValue) error

func (*ModelHelper) NewValueEmptyError

func (h *ModelHelper) NewValueEmptyError() error

func (*ModelHelper) NewValueInvalidError

func (h *ModelHelper) NewValueInvalidError(value any) error

func (*ModelHelper) Warn

func (h *ModelHelper) Warn(reason string, extra ...DescKeyValue)

func (*ModelHelper) WarnValueUnsound

func (h *ModelHelper) WarnValueUnsound(value any)

func (*ModelHelper) WarnValueUseless

func (h *ModelHelper) WarnValueUseless(value any)

func (*ModelHelper) WrapError

func (h *ModelHelper) WrapError(err error, reason string, extra ...DescKeyValue) error

func (*ModelHelper) WrapValueInvalidError

func (h *ModelHelper) WrapValueInvalidError(err error, value any) error

type SerializationFormat

type SerializationFormat string
const (
	SerializationFormatYaml SerializationFormat = "yaml"
	SerializationFormatToml SerializationFormat = "toml"
	SerializationFormatJson SerializationFormat = "json"
)

func GetSerializationFormat

func GetSerializationFormat(fileType FileType) SerializationFormat

type SerializationMetadata

type SerializationMetadata struct {
	File   string
	Format SerializationFormat
}

func DeserializeDir

func DeserializeDir(dir string, globs []string, model any, required bool) (metadata *SerializationMetadata, err error)

func DeserializeFile

func DeserializeFile(file string, format SerializationFormat, model any) (metadata *SerializationMetadata, err error)

type Serializer

type Serializer interface {
	GetFormat() SerializationFormat

	GetFileExt() string

	SerializeFile(file string, model any) error
}

type System

type System struct {
	Os         string
	Arch       string
	Hostname   string
	Username   string
	HomeDir    string
	CurrentDir string
	Variables  map[string]string
}

func GetSystem

func GetSystem() (*System, error)

type TomlSerializer

type TomlSerializer struct {
	TablesInline    bool
	IndentTables    bool
	ArraysMultiline bool
	IndentSymbol    string
}

func NewTomlSerializer

func NewTomlSerializer(tableInline, indentTables, arraysMultiline bool, indentSymbol string) *TomlSerializer

func (*TomlSerializer) GetFileExt

func (s *TomlSerializer) GetFileExt() string

func (*TomlSerializer) GetFormat

func (s *TomlSerializer) GetFormat() SerializationFormat

func (*TomlSerializer) SerializeFile

func (s *TomlSerializer) SerializeFile(file string, model any) error

func (*TomlSerializer) SetArraysMultiline

func (s *TomlSerializer) SetArraysMultiline(multiline bool) *TomlSerializer

func (*TomlSerializer) SetIndentSymbol

func (s *TomlSerializer) SetIndentSymbol(symbol string) *TomlSerializer

func (*TomlSerializer) SetIndentTables

func (s *TomlSerializer) SetIndentTables(indent bool) *TomlSerializer

func (*TomlSerializer) SetTablesInline

func (s *TomlSerializer) SetTablesInline(inline bool) *TomlSerializer

type Value

type Value struct {
	Value any
}

func WrapValue

func WrapValue(value any) *Value

func (*Value) DeepEqual

func (v *Value) DeepEqual(other *Value) bool

type Version

type Version string

func GetRuntimeVersion

func GetRuntimeVersion() Version

func (Version) GetVersionCode

func (v Version) GetVersionCode() (versionCode int32, err error)

type YamlSerializer

type YamlSerializer struct {
	Indent int
}

func NewYamlSerializer

func NewYamlSerializer(indent int) *YamlSerializer

func (*YamlSerializer) GetFileExt

func (s *YamlSerializer) GetFileExt() string

func (*YamlSerializer) GetFormat

func (s *YamlSerializer) GetFormat() SerializationFormat

func (*YamlSerializer) SerializeFile

func (s *YamlSerializer) SerializeFile(file string, model any) error

func (*YamlSerializer) SetIndent

func (s *YamlSerializer) SetIndent(indent int) *YamlSerializer

Jump to

Keyboard shortcuts

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