Initial QSfera import
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
// Package comdef provide some common type or constant definitions
|
||||
package comdef
|
||||
|
||||
// ToTypeFunc convert value to defined type
|
||||
type ToTypeFunc[T any] func(any) (T, error)
|
||||
|
||||
// IntCheckFunc check func
|
||||
type IntCheckFunc func(val int) error
|
||||
|
||||
// StrCheckFunc check func
|
||||
type StrCheckFunc func(val string) error
|
||||
|
||||
// ToStringFunc try to convert value to string, return error on fail
|
||||
type ToStringFunc func(v any) (string, error)
|
||||
|
||||
// SafeStringFunc safe convert value to string
|
||||
type SafeStringFunc func(v any) string
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package comdef
|
||||
|
||||
// constants for compare operation
|
||||
const (
|
||||
OpEq = "="
|
||||
OpNeq = "!="
|
||||
OpLt = "<"
|
||||
OpLte = "<="
|
||||
OpGt = ">"
|
||||
OpGte = ">="
|
||||
)
|
||||
|
||||
// constants quote chars
|
||||
const (
|
||||
SingleQuote = '\''
|
||||
DoubleQuote = '"'
|
||||
SlashQuote = '\\'
|
||||
|
||||
SingleQuoteStr = "'"
|
||||
DoubleQuoteStr = `"`
|
||||
SlashQuoteStr = "\\"
|
||||
)
|
||||
|
||||
// NoIdx invalid index or length
|
||||
const NoIdx = -1
|
||||
|
||||
// const VarPathReg = `(\w[\w-]*(?:\.[\w-]+)*)`
|
||||
|
||||
// Align define align, position: L, C, R, Auto
|
||||
type Align uint8
|
||||
type Position = Align // Position alias of Align
|
||||
|
||||
// constants for align, position: L, C, R, Auto
|
||||
const (
|
||||
Left Align = iota
|
||||
Center
|
||||
Right
|
||||
PosAuto
|
||||
)
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
//go:build !windows
|
||||
|
||||
package comdef
|
||||
|
||||
// Newline string for non-windows
|
||||
const Newline = "\n"
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package comdef
|
||||
|
||||
// Newline string for windows
|
||||
const Newline = "\r\n"
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package comdef
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrConvType error
|
||||
var ErrConvType = errors.New("convert value type error")
|
||||
|
||||
// Errors multi error list
|
||||
type Errors []error
|
||||
|
||||
// Error string
|
||||
func (es Errors) Error() string {
|
||||
var sb strings.Builder
|
||||
for _, err := range es {
|
||||
sb.WriteString(err.Error())
|
||||
sb.WriteByte('\n')
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// ErrOrNil error
|
||||
func (es Errors) ErrOrNil() error {
|
||||
if len(es) == 0 {
|
||||
return nil
|
||||
}
|
||||
return es
|
||||
}
|
||||
|
||||
// First error
|
||||
func (es Errors) First() error {
|
||||
if len(es) > 0 {
|
||||
return es[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package comdef
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/gookit/goutil/x/stdio"
|
||||
)
|
||||
|
||||
// DataFormatter interface
|
||||
type DataFormatter interface {
|
||||
Format() string
|
||||
FormatTo(w io.Writer)
|
||||
}
|
||||
|
||||
// BaseFormatter struct
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// type YourFormatter struct {
|
||||
// comdef.BaseFormatter
|
||||
// }
|
||||
// // implement the DataFormatter interface...
|
||||
type BaseFormatter struct {
|
||||
ow ByteStringWriter
|
||||
// Out formatted to the writer
|
||||
Out io.Writer
|
||||
// Src data(array, map, struct) for format
|
||||
Src any
|
||||
// MaxDepth limit depth for array, map data TODO
|
||||
MaxDepth int
|
||||
// Prefix string for each element
|
||||
Prefix string
|
||||
// Indent string for format each element
|
||||
Indent string
|
||||
// ClosePrefix string for last "]", "}"
|
||||
ClosePrefix string
|
||||
}
|
||||
|
||||
// Reset after format
|
||||
func (f *BaseFormatter) Reset() {
|
||||
f.Out = nil
|
||||
f.Src = nil
|
||||
}
|
||||
|
||||
// SetOutput writer
|
||||
func (f *BaseFormatter) SetOutput(out io.Writer) {
|
||||
f.Out = out
|
||||
}
|
||||
|
||||
// BsWriter warp the Out, build a ByteStringWriter
|
||||
func (f *BaseFormatter) BsWriter() ByteStringWriter {
|
||||
if f.ow == nil {
|
||||
if f.Out == nil {
|
||||
f.ow = new(bytes.Buffer)
|
||||
} else if ow, ok := f.Out.(ByteStringWriter); ok {
|
||||
f.ow = ow
|
||||
} else {
|
||||
f.ow = stdio.NewWriteWrapper(f.Out)
|
||||
}
|
||||
}
|
||||
return f.ow
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package comdef
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// ByteStringWriter interface
|
||||
type ByteStringWriter interface {
|
||||
io.Writer
|
||||
io.ByteWriter
|
||||
io.StringWriter
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
// StringWriteStringer interface
|
||||
type StringWriteStringer interface {
|
||||
io.StringWriter
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
// Int64able interface
|
||||
type Int64able interface {
|
||||
Int64() (int64, error)
|
||||
}
|
||||
|
||||
// Float64able interface
|
||||
type Float64able interface {
|
||||
Float64() (float64, error)
|
||||
}
|
||||
|
||||
// MapFunc definition
|
||||
type MapFunc func(val any) (any, error)
|
||||
|
||||
//
|
||||
//
|
||||
// Matcher type
|
||||
//
|
||||
//
|
||||
|
||||
// Matcher interface
|
||||
type Matcher[T any] interface {
|
||||
Match(s T) bool
|
||||
}
|
||||
|
||||
// MatchFunc definition. implements Matcher interface
|
||||
type MatchFunc[T any] func(v T) bool
|
||||
|
||||
// Match satisfies the Matcher interface
|
||||
func (fn MatchFunc[T]) Match(v T) bool {
|
||||
return fn(v)
|
||||
}
|
||||
|
||||
// StringMatcher interface
|
||||
type StringMatcher interface {
|
||||
Match(s string) bool
|
||||
}
|
||||
|
||||
// StringMatchFunc definition
|
||||
type StringMatchFunc func(s string) bool
|
||||
|
||||
// Match satisfies the StringMatcher interface
|
||||
func (fn StringMatchFunc) Match(s string) bool {
|
||||
return fn(s)
|
||||
}
|
||||
|
||||
// StringHandler interface
|
||||
type StringHandler interface {
|
||||
Handle(s string) string
|
||||
}
|
||||
|
||||
// StringHandleFunc definition
|
||||
type StringHandleFunc func(s string) string
|
||||
|
||||
// Handle satisfies the StringHandler interface
|
||||
func (fn StringHandleFunc) Handle(s string) string {
|
||||
return fn(s)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package comdef
|
||||
|
||||
type (
|
||||
// MarshalFunc define
|
||||
MarshalFunc func(v any) ([]byte, error)
|
||||
|
||||
// UnmarshalFunc define
|
||||
UnmarshalFunc func(bts []byte, ptr any) error
|
||||
)
|
||||
|
||||
// Serializer interface definition
|
||||
type Serializer interface {
|
||||
Serialize(v any) ([]byte, error)
|
||||
Deserialize(data []byte, v any) error
|
||||
}
|
||||
|
||||
// GoSerializer interface definition
|
||||
type GoSerializer interface {
|
||||
Marshal(v any) ([]byte, error)
|
||||
Unmarshal(v []byte, ptr any) error
|
||||
}
|
||||
|
||||
// Codec interface definition
|
||||
type Codec interface {
|
||||
Decode(blob []byte, v any) (err error)
|
||||
Encode(v any) (out []byte, err error)
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package comdef
|
||||
|
||||
const (
|
||||
// CommaStr const define
|
||||
CommaStr = ","
|
||||
// CommaChar define
|
||||
CommaChar = ','
|
||||
|
||||
// EqualStr define
|
||||
EqualStr = "="
|
||||
// EqualChar define
|
||||
EqualChar = '='
|
||||
|
||||
// ColonStr define
|
||||
ColonStr = ":"
|
||||
// ColonChar define
|
||||
ColonChar = ':'
|
||||
|
||||
// SemicolonStr semicolon define
|
||||
SemicolonStr = ";"
|
||||
// SemicolonChar define
|
||||
SemicolonChar = ';'
|
||||
|
||||
// PathStr define const
|
||||
PathStr = "/"
|
||||
// PathChar define
|
||||
PathChar = '/'
|
||||
|
||||
// DefaultSep comma string
|
||||
DefaultSep = ","
|
||||
|
||||
// SpaceChar char
|
||||
SpaceChar = ' '
|
||||
// SpaceStr string
|
||||
SpaceStr = " "
|
||||
|
||||
// NewlineChar char
|
||||
NewlineChar = '\n'
|
||||
// NewlineStr string
|
||||
NewlineStr = "\n"
|
||||
)
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package comdef
|
||||
|
||||
// Int interface type
|
||||
type Int interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
||||
}
|
||||
|
||||
// Uint interface type
|
||||
type Uint interface {
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
||||
}
|
||||
|
||||
// Xint interface type. alias of Integer
|
||||
type Xint interface {
|
||||
Int | Uint
|
||||
}
|
||||
|
||||
// Integer interface type. all int or uint types
|
||||
type Integer interface {
|
||||
Int | Uint
|
||||
}
|
||||
|
||||
// Float interface type
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
// IntOrFloat interface type. all int and float types, but NOT uint types
|
||||
type IntOrFloat interface {
|
||||
Int | Float
|
||||
}
|
||||
|
||||
// Number interface type. contains all int, uint and float types
|
||||
type Number interface {
|
||||
Int | Uint | Float
|
||||
}
|
||||
|
||||
// XintOrFloat interface type. all int, uint and float types. alias of Number
|
||||
//
|
||||
// Deprecated: use Number instead.
|
||||
type XintOrFloat interface {
|
||||
Int | Uint | Float
|
||||
}
|
||||
|
||||
// NumberOrString interface type for (x)int, float, ~string types
|
||||
type NumberOrString interface {
|
||||
Int | Uint | Float | ~string
|
||||
}
|
||||
|
||||
// SortedType interface type. same of constraints.Ordered
|
||||
//
|
||||
// it can be ordered, that supports the operators < <= >= >.
|
||||
//
|
||||
// contains: (x)int, float, ~string types
|
||||
type SortedType interface {
|
||||
Int | Uint | Float | ~string
|
||||
}
|
||||
|
||||
// Compared type. alias of constraints.SortedType
|
||||
//
|
||||
// TODO: use type alias, will error on go1.18 Error: types.go:50: interface contains type constraints
|
||||
// type Compared = SortedType
|
||||
type Compared interface {
|
||||
Int | Uint | Float | ~string
|
||||
}
|
||||
|
||||
// SimpleType interface type. alias of ScalarType
|
||||
//
|
||||
// contains: (x)int, float, ~string, ~bool types
|
||||
type SimpleType interface {
|
||||
Int | Uint | Float | ~string | ~bool
|
||||
}
|
||||
|
||||
// ScalarType basic interface type.
|
||||
//
|
||||
// TIP: has bool type, it cannot be ordered
|
||||
//
|
||||
// contains: (x)int, float, ~string, ~bool types
|
||||
type ScalarType interface {
|
||||
Int | Uint | Float | ~string | ~bool
|
||||
}
|
||||
|
||||
// StrMap is alias of map[string]string
|
||||
type StrMap map[string]string
|
||||
|
||||
// AnyMap is alias of map[string]any
|
||||
type AnyMap map[string]any
|
||||
|
||||
// L2StrMap is alias of map[string]map[string]string
|
||||
type L2StrMap map[string]map[string]string
|
||||
Reference in New Issue
Block a user