pola

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 29 Imported by: 0

README

pola

pola common pattern.

Documentation

Index

Constants

View Source
const (
	ExtJson    = ".json"
	ExtHjson   = ".hjson"
	ExtHuJson  = ".hujson"
	ExtJwcc    = ".jwcc"
	ExtYml     = ".yml"
	ExtYaml    = ".yaml"
	ExtToml    = ".toml"
	ExtJsonnet = ".jsonnet"
	ExtXml     = ".xml"
)
View Source
const (
	IoStdout  = "<stdout>"
	IoStderr  = "<stderr>"
	IoStdin   = "<stdin>"
	IoNull    = "<null>"
	IoDevNull = "/dev/null"
	IoEmpty   = ""
)

Variables

View Source
var (
	ErrDuplicateEntry     = errors.New("duplicate entry")
	ErrEntryDoesNotExists = errors.New("entry does not exists")
)
View Source
var (
	DevNull = devNull{}
)

DevNull mimics /dev/null behaviour It discard on write, and return EOF on read.

View Source
var (
	ErrDecoderUnsupportedType = errors.New("Decoder, unsuported type")
)
View Source
var (
	ErrInvalidSemVer = errors.New("invalid Semantic Version")
)

Functions

func CurrentDirFS

func CurrentDirFS() (fs.FS, error)

CurrentDirFS return fs.FS for current working directory

func GoPath

func GoPath() string

func GoRoot

func GoRoot() string

func GoVersion

func GoVersion() string

func Interruptible

func Interruptible(r Runner, sigs ...os.Signal) error

Interruptible execute Runner and cancel it when SIGINT is captured. Here, context.Background is used as the parent context. By default, the function listen to os.Interrupt and syscall.SIGINT, if additional signals are needed, pass them to optional `sigs` argument.

func InterruptibleContext

func InterruptibleContext(ctx context.Context, r Runner, sigs ...os.Signal) error

InterruptibleContext execute Runner and cancel it when SIGINT is captured. The argument `ctx` is used as the parent context for constructing cancelable context. By default, the function listen to os.Interrupt and syscall.SIGINT, if additional signals are needed, pass them to optional `sigs` argument.

func InterruptibleFunc added in v0.2.0

func InterruptibleFunc(fn func(ctx context.Context) error, sigs ...os.Signal) error

InterruptibleFunc execute runner with given function

func IsGoSrcDir

func IsGoSrcDir(dir string) bool

func NewBytesPool

func NewBytesPool(cap ...int) *sync.Pool

NewBytesPool return pool with pre-allocated []byte Arg `cap` is opitonal, and if not specified, the capacity is 8192 bytes.

func NopReadWriteCloser

func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser

NopReadWriteCloser wrap io.ReadWriter with nop Close method.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopWriteCloser create writer with Close method

func PathExists

func PathExists(path string) bool

check file exists

func ReadCloserFromDescriptor

func ReadCloserFromDescriptor(desc string) (io.ReadCloser, error)

ReadCloserFromDescriptor create reader with closer from given descriptor

func SafeCloser

func SafeCloser(c io.Closer) io.Closer

func SafeSyncCloser

func SafeSyncCloser(c io.Closer) io.Closer

func ToBool

func ToBool(v any) bool

ToBool convert any value to boolean Part of these code is taken from github.com/mattn/anko

func ToDuration

func ToDuration(v any) (time.Duration, bool)

ToDuration convert any valid duration representation to time.Duration

func ToFloat

func ToFloat(v any) (float64, bool)

ToFloat convert any convertible vaue to float64.

func ToInt

func ToInt(v any) (int64, bool)

ToInt convert any convertible value to int64

func ToString

func ToString(v any) string

ToString convert any value to string representation

func ToTime

func ToTime(v any, opt ...*time.Location) (time.Time, bool)

ToTime convert any to time

func UnmarshalFs

func UnmarshalFs(dest any, name string, fa ...fs.FS) error

UnmarshalFs decode content specified as name to dest. Arg `fa` is an array of file system, in which if its not specified, `name` will be searched from current directory (`.`). If multiple fa are specified, UnmarshalFs will finish once its succeeded to decode the content.

func WriteCloserFromDescriptor

func WriteCloserFromDescriptor(desc string) (io.WriteCloser, error)

WriteCloserFromDescriptor return io.WriteCloser from given descriptr. Valid descriptor are: <null>, <stdout>, <stderr> and "desc" as filename

Types

type Closers

type Closers interface {
	io.Closer
	Append(io.Closer) Closers
	Remove(io.Closer) Closers
	TakeFirst() (io.Closer, bool)
	TakeLast() (io.Closer, bool)
	Empty() bool
	Len() int
	Clear()
}

Closers hold list of io.Closer

func NewClosers

func NewClosers(c ...io.Closer) Closers

type Decoder

type Decoder interface {
	Decode(dest any) error
}

Decoder decodes input file/stream/data into golang's data type.

func NewBytesDecoder

func NewBytesDecoder(data []byte, ext string) Decoder

NewBytesDecoder return decoder for given stream

func NewDecoder

func NewDecoder(r io.Reader, ext string) Decoder

NewDecoder return decoder for given rider and ext type

func NewFsDecoder

func NewFsDecoder(name string, fa ...fs.FS) Decoder

NewFsDecoder decode given file into object. Supported format and corresponding decoders are: - json: encoding/json - hjson: github.com/hjson/hjson-go/v4 - hujson, jwcc: github.com/tailscale/hujson - yaml, yml: github.com/goccy/go-yaml - toml: github.com/BurntSushi/toml - jsonnet: github.com/google/go-jsonnet

type FormattedText

type FormattedText interface {
	Decoder
	String() string
	Ext() string
}

FormattedText has Decoder and Stringer interface

type FormattedTextFile

type FormattedTextFile string

FormattedTextFile holds the filename for formatted file content. Filename extension is use to determined the content type.

func (FormattedTextFile) Decode

func (f FormattedTextFile) Decode(dest any) error

func (FormattedTextFile) Ext

func (f FormattedTextFile) Ext() string

func (FormattedTextFile) String

func (f FormattedTextFile) String() string

type GoPackageHint

type GoPackageHint struct {
	Domain       string
	RelPath      string
	ImportPath   string
	SrcDir       string
	IsStdPkg     bool
	ModGoVersion string
	Version      string
	HasGoMod     bool
	Valid        bool
}

func GetGoModHint

func GetGoModHint(base, importDirective, dir string) (*GoPackageHint, error)

func (*GoPackageHint) GoVariableName

func (g *GoPackageHint) GoVariableName() string

func (*GoPackageHint) ID

func (g *GoPackageHint) ID() string

func (*GoPackageHint) OutputFilename

func (g *GoPackageHint) OutputFilename() string

func (*GoPackageHint) SanitizeImportPath

func (g *GoPackageHint) SanitizeImportPath()

type GoPackageHints

type GoPackageHints struct {
	BaseDir   string
	GoVersion string
	Hints     []*GoPackageHint
}

func GetGoPackageHints

func GetGoPackageHints(tgtVer, tgtImport, pkgSrcDir string, rootDir ...string) (*GoPackageHints, error)

func GetStdGoPackageHints

func GetStdGoPackageHints(version string, rootDir ...string) (*GoPackageHints, error)

type HjsonText

type HjsonText []byte

func (HjsonText) Decode

func (h HjsonText) Decode(dest any) error

func (HjsonText) Ext

func (h HjsonText) Ext() string

func (HjsonText) String

func (h HjsonText) String() string

type HuJsonText

type HuJsonText []byte

func (HuJsonText) Decode

func (h HuJsonText) Decode(dest any) error

func (HuJsonText) Ext

func (h HuJsonText) Ext() string

func (HuJsonText) String

func (h HuJsonText) String() string

type JsonText

type JsonText []byte

func (JsonText) Decode

func (j JsonText) Decode(dest any) error

func (JsonText) Ext

func (j JsonText) Ext() string

func (JsonText) String

func (j JsonText) String() string

type JsonnetText

type JsonnetText []byte

func (JsonnetText) Decode

func (j JsonnetText) Decode(dest any) error

func (JsonnetText) Ext

func (j JsonnetText) Ext() string

func (JsonnetText) String

func (j JsonnetText) String() string

type JwccText

type JwccText []byte

func (JwccText) Decode

func (j JwccText) Decode(dest any) error

func (JwccText) Ext

func (j JwccText) Ext() string

func (JwccText) String

func (j JwccText) String() string

type Registry

type Registry[K comparable, V any] interface {
	Register(k K, v V) error
	MustRegister(k K, v V)
	Exists(k K) bool
	Set(k K, v V)
	Get(k K) (V, error)
	MustGet(k K) V
	Map() map[K]V
}

Registry for storing entry.

func NewRegistry

func NewRegistry[K comparable, V any]() Registry[K, V]

NewRegistry create map-based registry. Please note that this registry is not safe for concurrent usage.

func NewSyncRegistry

func NewSyncRegistry[K comparable, V any]() Registry[K, V]

NewRegistry create map-based registry guarded with Mutex. This registry is safe for concurrent usage.

type Runner

type Runner interface {
	RunContext(ctx context.Context) error
}

Runner is the interface that wrap a runnable task/function/action

type RunnerFunc

type RunnerFunc func(context.Context) error

RunnerFunc is adapter to allow function to be used as Runner

func (RunnerFunc) RunContext

func (f RunnerFunc) RunContext(ctx context.Context) error

type TomlText

type TomlText []byte

func (TomlText) Decode

func (t TomlText) Decode(dest any) error

func (TomlText) Ext

func (t TomlText) Ext() string

func (TomlText) String

func (t TomlText) String() string

type XmlText

type XmlText []byte

func (XmlText) Decode

func (x XmlText) Decode(dest any) error

func (XmlText) Ext

func (x XmlText) Ext() string

func (XmlText) String

func (x XmlText) String() string

type YamlText

type YamlText []byte

func (YamlText) Decode

func (y YamlText) Decode(dest any) error

func (YamlText) Ext

func (y YamlText) Ext() string

func (YamlText) String

func (y YamlText) String() string

Jump to

Keyboard shortcuts

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