convkit

package
v0.318.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 17 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 FormatReflect added in v0.310.0

func FormatReflect(v reflect.Value, opts ...Option) (string, error)

FormatReflect allows you to Format a value into a string, passed as a reflect.Value.

func IsRegistered

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

func Marshal added in v0.310.0

func Marshal[T any](v T, opts ...Option) ([]byte, error)

func MarshalReflect added in v0.310.0

func MarshalReflect(val reflect.Value, opts ...Option) ([]byte, error)

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](c TextCodec[T]) func()

func Unmarshal added in v0.310.0

func Unmarshal[T any](data []byte, p *T, opts ...Option) error
Example (BasicType)
package main

import (
	"fmt"

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

func main() {
	var num int
	convkit.Unmarshal([]byte("42"), &num)
	fmt.Println(num)
}
Output:
42
Example (CustomType)
package main

import (
	"fmt"
	"strconv"
	"strings"

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

func main() {
	type Config struct {
		Host string
		Port int
	}
	var cfg Config
	convkit.Unmarshal([]byte("host=localhost,port=8080"), &cfg,
		convkit.UnmarshalWith(func(data []byte, c *Config) error {
			parts := strings.SplitN(string(data), ",", 2)
			if len(parts) != 2 {
				return fmt.Errorf("invalid format")
			}
			for _, part := range parts {
				kv := strings.SplitN(part, "=", 2)
				if len(kv) != 2 {
					continue
				}
				switch kv[0] {
				case "host":
					c.Host = kv[1]
				case "port":
					var err error
					c.Port, err = strconv.Atoi(kv[1])
					if err != nil {
						return fmt.Errorf("invalid port: %w", err)
					}
				}
			}
			return nil
		}))
	fmt.Printf("%s:%d\n", cfg.Host, cfg.Port)
}
Output:
localhost:8080
Example (Json)
package main

import (
	"fmt"

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

func main() {
	var data map[string]interface{}
	convkit.Unmarshal([]byte(`{"key":"value"}`), &data)
	fmt.Println(data["key"])
}
Output:
value
Example (SliceWithSeparator)
package main

import (
	"fmt"

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

func main() {
	var nums []int
	convkit.Unmarshal([]byte("1;2;3"), &nums, convkit.Options{Separator: ";"})
	fmt.Println(nums)
}
Output:
[1 2 3]
Example (TimeWithLayout)
package main

import (
	"fmt"
	"time"

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

func main() {
	var t time.Time
	layout := "2006-01-02"
	convkit.Unmarshal([]byte("2023-05-15"), &t, convkit.Options{TimeLayout: layout})
	fmt.Println(t.Format(layout))
}
Output:
2023-05-15
Example (Url)
package main

import (
	"fmt"
	"net/url"

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

func main() {
	var url url.URL
	convkit.Unmarshal([]byte("https://example.com/path?query=value"), &url)
	fmt.Println(url.String())
}
Output:
https://example.com/path?query=value

func UnmarshalReflect added in v0.310.0

func UnmarshalReflect(typ reflect.Type, data []byte, ptr reflect.Value, opts ...Option) error
Example
package main

import (
	"fmt"
	"reflect"

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

func main() {
	typ := reflectkit.TypeOf[int]()
	ptr := reflect.New(typ)
	convkit.UnmarshalReflect(typ, []byte("42"), ptr)
	fmt.Println(ptr.Elem().Interface())
}
Output:
42
Example (SliceWithSeparator)
package main

import (
	"fmt"
	"reflect"

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

func main() {
	typ := reflectkit.TypeOf[[]int]()
	ptr := reflect.New(typ)
	convkit.UnmarshalReflect(typ, []byte("1;2;3"), ptr, convkit.Options{Separator: ";"})
	fmt.Println(ptr.Elem().Interface())
}
Output:
[1 2 3]
Example (TimeWithLayout)
package main

import (
	"fmt"
	"reflect"
	"time"

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

func main() {
	typ := reflectkit.TypeOf[time.Time]()
	ptr := reflect.New(typ)
	layout := "2006-01-02"
	convkit.UnmarshalReflect(typ, []byte("2023-05-15"), ptr, convkit.Options{TimeLayout: layout})
	fmt.Println(ptr.Elem().Interface().(time.Time).Format(layout))
}
Output:
2023-05-15

Types

type MarshalFunc added in v0.310.0

type MarshalFunc[T any] func(T) ([]byte, error)

type Option

type Option option.Option[Options]

func UnmarshalWith added in v0.310.0

func UnmarshalWith[T any](parser UnmarshalFunc[T]) Option

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) Configure added in v0.310.0

func (o Options) Configure(options *Options)

func (*Options) Merge

func (o *Options) Merge(oth Options)

type TextCodec added in v0.310.0

type TextCodec[T any] interface {
	Marshal(v T) ([]byte, error)
	Unmarshal(data []byte, p *T) error
}

type UnmarshalFunc added in v0.310.0

type UnmarshalFunc[T any] func(data []byte, p *T) error

Jump to

Keyboard shortcuts

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