conversions

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 3 Imported by: 0

README

conversions-go

Conversions is a module to convert between types in Go, with support for reflect.Type or a custom type indicator, custom conversion functions and indirect conversions.

Features

  • Uses generics in Conversions for type safety
  • Supports reflect.Type or a custom type indicator
  • Built-in conversions for reflect.Type
  • Error handling, conversions can safely error if values cannot be converted
  • Custom conversion functions can be registered
  • Indirect conversions, if a direct conversion is not found between two types a chain of conversions will be searched for
  • Thread-safe

Installation

go get github.com/levelfourab/conversions-go

Using

If you want to use conversions with your own types and information you need to create a Conversions using the WithTypeExtractor option. The type extractor is in charge of taking a value and returning the type indicator for that value.

This type is then used during conversion. If a direct conversion is not found between the two types, a chain of conversions will be searched for.

Example:

type MediaType string

type StringWithMediaType struct {
  MediaType MediaType
  Value     string
}

conversions, err := conversions.New(
  conversions.WithTypeExtractor(func(v StringWithMediaType) (MediaType, error) {
    return v.MediaType, nil
  }),
)

conversions.AddConversion("text/html", "text/plain", func(v string) (string, error) {
  // Using a hypothetical html2text package
  return html2text.Convert(v)
})

// Perform the conversion
asPlainText, err := conversions.Convert(StringWithMediaType{
  MediaType: "text/html",
  Value:     "<h1>Hello World</h1>",
}, "text/plain")
With Go-types

The types sub-package is available for the most common use case, converting between Go types. Part of this includes default conversions for many built-in types such as string, int (and variants), uint (and variants), float32 and float64.

Example:

conversions, err := types.New(
  types.WithDefaultConversions(),
)

// Convert a string to an int, using types.TypeInt for convenience
asInt, err := conversions.Convert("1", types.TypeInt)

// Or using reflect.Type directly
asInt, err := conversions.Convert("1", reflect.TypeOf(int(0)))

Aliases are available to not have to carry generic types around:

  • types.Conversions is an alias for conversions.Conversions[reflect.Type, any]
  • types.Converter is an alias for conversions.Converter[any]
  • types.Option is an alias for conversions.Option[reflect.Type, any]

License

conversions-go is licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conversions

type Conversions[T comparable, D any] struct {
	// contains filtered or unexported fields
}

Conversions contains converters between different types.

func New

func New[T comparable, D any](opts ...Option[T, D]) (*Conversions[T, D], error)

New creates a new Conversions instance.

func (*Conversions[T, D]) AddConversion

func (c *Conversions[T, D]) AddConversion(from T, to T, conv Converter[D])

AddConversion adds a new converter to the conversions.

func (*Conversions[T, D]) Convert

func (c *Conversions[T, D]) Convert(value D, to T) (any, error)

Convert converts a value from one type to another.

type Converter

type Converter[D any] func(value D) (D, error)

Converter represents a function that can convert a value to another value.

type Option

type Option[T comparable, D any] func(*options[T, D]) error

Option represents an option for Conversions.

func With

func With[T comparable, D any](opts ...Option[T, D]) Option[T, D]

With combines multiple options into a single option.

func WithConversion

func WithConversion[T comparable, D any](from T, to T, conv Converter[D]) Option[T, D]

WithConversion makes a conversion available for use.

func WithTypeExtractor

func WithTypeExtractor[T comparable, D any](extractor func(D) (T, error)) Option[T, D]

WithTypeExtractor sets the type extractor to use when converting values. The type extractor is asked to extract the type of a value, the type is then used to find suitable conversions.

func WithTypedConversion

func WithTypedConversion[T comparable, D any](conv TypedConversion[T, D]) Option[T, D]

WithTypedConversion makes a typed conversion available for use.

type TypedConversion

type TypedConversion[T comparable, D any] struct {
	// From is the type that the converter can convert from.
	From T
	// To is the type that the converter can convert to.
	To T
	// Converter is the converter function.
	Converter Converter[D]
}

TypedConversion represents a conversion between two types. It extends Converter with the types that the converter can convert between.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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