marshaler

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package marshaler provides marshalers for converting values to and from their binary representations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// String marshals and unmarshals the built-in string type by performing a
	// Go type-conversion.
	String = New(
		func(v string) ([]byte, error) {
			return []byte(v), nil
		},
		func(data []byte) (string, error) {
			return string(data), nil
		},
	)

	// Bool marshals and unmarshals the built-in bool type.
	Bool = New(
		func(v bool) ([]byte, error) {
			if v {
				return []byte{1}, nil
			}
			return nil, nil
		},
		func(data []byte) (bool, error) {
			return len(data) > 0, nil
		},
	)
)

Functions

This section is empty.

Types

type Marshaler

type Marshaler[T any] interface {
	Marshal(T) ([]byte, error)
	Unmarshal([]byte) (T, error)
}

Marshaler is an interface for types that can marshal and unmarshal values of type T.

func New

func New[T any](
	marshal func(T) ([]byte, error),
	unmarshal func([]byte) (T, error),
) Marshaler[T]

New returns a new Marshaler that marshals and unmarshals values of type T using the given functions.

func NewJSON

func NewJSON[T any]() Marshaler[T]

NewJSON returns a marshaler that marshals and unmarshals an arbitrary type using Go's standard NewJSON encoding.

func NewProto

func NewProto[
	T interface {
		proto.Message
		*S
	},
	S any,
]() Marshaler[T]

NewProto returns a marshaler that marshals and unmarshals Protocol Buffers messages.

Jump to

Keyboard shortcuts

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