converters

package
v3.0.0-rc67 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB = map[string]Converter{}

DB is the name-indexed registry of the known converters. It is used to convert back the converter names received from a peer daemon, which may run a different version, so a name may be unknown.

Functions

func DurationWithDefaultMinMax

func DurationWithDefaultMinMax(s *string, defaultD, minD, maxD time.Duration) (time.Duration, error)

DurationWithDefaultMinMax parses a duration string and clamps the result within minD and maxD or returns defaultD. If s is nil or empty, defaultD is returned, clamped within the range defined by minD and maxD.

func Format

func Format(c Converter, v any) string

Format writes the value the converter c converted the way a configuration writes it. A converter with no Formatter, and no converter at all, write the value as it is: a list as its words, a pointer as what it points to, nothing as the empty text.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration writes a duration with its zero units dropped: 2m, 1h30m, 1h, 500ms, 0s.

func FormatSize

func FormatSize(n int64) string

FormatSize writes a number of bytes in the largest binary unit it is a whole number of.

func Name

func Name(c Converter) string

Name returns the name c is registered as, and an empty string if c is nil.

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

func ReadFile

func ReadFile(fs embed.FS, s string) string

func Register

func Register(c Converter)

Types

type Converter

type Converter interface {
	Convert(s string) (any, error)
	String() string
}
var (
	String        Converter = TString{}
	Int           Converter = TInt{}
	Int64         Converter = TInt64{}
	Float64       Converter = TFloat64{}
	Bool          Converter = TBool{}
	List          Converter = TList{}
	ListLowercase Converter = TListLowercase{}
	Set           Converter = TSet{}
	Shlex         Converter = TShlex{}
	Duration      Converter = TDuration{}
	Umask         Converter = TUmask{}
	Size          Converter = TSize{}
	Share         Converter = TShare{}
	FileMode      Converter = TFileMode{}
	Tristate      Converter = TTristate{}
	User          Converter = TUser{}
	Group         Converter = TGroup{}
)

The exported converters are the singletons the keyword definitions and the callers must reference. Referencing them instead of their name makes a typo a compilation error instead of a runtime panic.

func Get

func Get(s string) (Converter, bool)

Get returns the converter registered as s. An empty s means "no conversion", and returns a nil Converter and true.

Unlike a name lookup in a keyword definition, which is now a compile-time check, s may come from a untrusted source like a peer daemon keyword definition, so an unknown name must not panic.

type Formatter

type Formatter interface {
	Format(v any) string
}

Formatter is a converter writing a value it converted back the way a configuration writes it: a duration as 2m, a size as 5g.

The converted value is what a program reads, a count of nanoseconds or of bytes. The text is what a person reads, and converts back to the same value.

type TBool

type TBool struct{}

func (TBool) Convert

func (t TBool) Convert(s string) (any, error)

func (TBool) String

func (t TBool) String() string

type TDuration

type TDuration struct{}

func (TDuration) Convert

func (t TDuration) Convert(s string) (any, error)

Convert converts duration string to *time.Duration

nil is returned when duration is unset Default unit is second when not specified

func (TDuration) Format

func (t TDuration) Format(v any) string

Format writes a duration with its zero units dropped, so a duration configured as 2m reads 2m, and not the 2m0s of time.Duration.

func (TDuration) String

func (t TDuration) String() string

type TFileMode

type TFileMode struct{}

func (TFileMode) Convert

func (t TFileMode) Convert(s string) (any, error)

func (TFileMode) Format

func (t TFileMode) Format(v any) string

Format writes a file mode in octal, as it is configured, with the setuid, setgid and sticky bits as the leading digit when one is set.

func (TFileMode) String

func (t TFileMode) String() string

type TFloat64

type TFloat64 struct{}

func (TFloat64) Convert

func (t TFloat64) Convert(s string) (any, error)

func (TFloat64) String

func (t TFloat64) String() string

type TGroup

type TGroup struct{}

func (TGroup) Convert

func (t TGroup) Convert(s string) (interface{}, error)

func (TGroup) Format

func (t TGroup) Format(v any) string

Format writes a group as its name.

func (TGroup) String

func (t TGroup) String() string

type TInt

type TInt struct{}

func (TInt) Convert

func (t TInt) Convert(s string) (any, error)

func (TInt) String

func (t TInt) String() string

type TInt64

type TInt64 struct{}

func (TInt64) Convert

func (t TInt64) Convert(s string) (any, error)

func (TInt64) String

func (t TInt64) String() string

type TList

type TList struct{}

func (TList) Convert

func (t TList) Convert(s string) (any, error)

func (TList) String

func (t TList) String() string

type TListLowercase

type TListLowercase struct{}

func (TListLowercase) Convert

func (t TListLowercase) Convert(s string) (any, error)

func (TListLowercase) String

func (t TListLowercase) String() string

type TSet

type TSet struct{}

func (TSet) Convert

func (t TSet) Convert(s string) (any, error)

func (TSet) String

func (t TSet) String() string

type TShare

type TShare struct{}

func (TShare) Convert

func (t TShare) Convert(s string) (any, error)

Convert returns the share s says, a percentage or a size, and nil when s is empty.

func (TShare) String

func (t TShare) String() string

type TShlex

type TShlex struct{}

func (TShlex) Convert

func (t TShlex) Convert(s string) (any, error)

func (TShlex) Format

func (t TShlex) Format(v any) string

Format writes the words of a command line, quoting the ones a shell would split.

func (TShlex) String

func (t TShlex) String() string

type TSize

type TSize struct{}

func (TSize) Convert

func (t TSize) Convert(s string) (any, error)

func (TSize) Format

func (t TSize) Format(v any) string

Format writes a size in the largest unit it is a whole number of, so it converts back to the same number of bytes: 5g, 1536m, and a count of bytes when no unit divides it.

func (TSize) String

func (t TSize) String() string

type TString

type TString struct{}

func (TString) Convert

func (t TString) Convert(s string) (any, error)

func (TString) String

func (t TString) String() string

type TTristate

type TTristate struct{}

func (TTristate) Convert

func (t TTristate) Convert(s string) (any, error)

func (TTristate) String

func (t TTristate) String() string

type TUmask

type TUmask struct{}

func (TUmask) Convert

func (t TUmask) Convert(s string) (any, error)

func (TUmask) Format

func (t TUmask) Format(v any) string

Format writes a umask in octal, as it is configured.

func (TUmask) String

func (t TUmask) String() string

type TUser

type TUser struct{}

func (TUser) Convert

func (t TUser) Convert(s string) (interface{}, error)

func (TUser) Format

func (t TUser) Format(v any) string

Format writes a user as its name.

func (TUser) String

func (t TUser) String() string

Jump to

Keyboard shortcuts

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