convkit

package
v0.255.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DuckParse

func DuckParse[Raw encoded](raw Raw, opts ...Option) (any, error)

func Format

func Format[T any](v T, opts ...Option) (string, error)

Format allows you to format a value into a string format

Example
package main

import (
	"go.llib.dev/frameless/pkg/convkit"
)

func main() {
	formatted, err := convkit.Format(42.24)
	_, _ = formatted, err // "42.24", nil
}

func IsRegistered

func IsRegistered[T any](i ...T) bool

func Parse

func Parse[T any, Raw encoded](raw Raw, opts ...Option) (T, error)
Example
package main

import (
	"go.llib.dev/frameless/pkg/convkit"
)

func main() {
	_, _ = convkit.Parse[string]("hello")
	_, _ = convkit.Parse[int]("42")
	_, _ = convkit.Parse[float64]("42.24")
	_, _ = convkit.Parse[[]string]("a,b,c", convkit.Options{Separator: ","})
	_, _ = convkit.Parse[[]string]([]byte(`["a","b","c"]`), convkit.Options{Separator: ","})
}

func ParseReflect

func ParseReflect[Raw encoded](typ reflect.Type, raw Raw, opts ...Option) (reflect.Value, error)

func Register

func Register[T any](
	parser parseFunc[T],
	formatter formatFunc[T],
) func()

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

func ParseWith

func ParseWith[T any](parser parseFunc[T]) Option
Example
package main

import (
	"fmt"
	"strings"

	"go.llib.dev/frameless/pkg/convkit"
)

func main() {
	// export FOO=foo:baz
	type Conf struct {
		Foo string
		Bar string
	}
	parserFunc := func(v string) (Conf, error) {
		parts := strings.SplitN(v, ":", 1)
		if len(parts) != 2 {
			return Conf{}, fmt.Errorf("invalid format")
		}
		return Conf{
			Foo: parts[0],
			Bar: parts[1],
		}, nil
	}
	conf, err := convkit.Parse[Conf]("foo:bar", convkit.ParseWith(parserFunc))
	_, _ = conf, err
}

type Options

type Options struct {
	// Separator is the separator character which will be used to detect list elements.
	Separator string
	// TimeLayout is the time layout format which will be used to parse time values.
	TimeLayout string
	// ParseFunc is used to Parse the input data. It follows the signature of the json.Unmarshal function.
	ParseFunc func(data []byte, ptr any) error
}

func (*Options) Merge

func (o *Options) Merge(oth Options)

Jump to

Keyboard shortcuts

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