conv

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

The conv package provides a [Coverter], which indexes conversion functions.

Example (Converter)
package main

import (
	"fmt"

	"github.com/AndrewHarrisSPU/lift/conv"
)

func main() {
	// from int to string
	i2s := func(n int) (string, error) {
		return fmt.Sprintf("(int) %d", n), nil
	}
	// from rune to string
	r2s := func(r rune) (string, error) {
		return fmt.Sprintf("(rune) %c", r), nil
	}

	// constructing a new converter
	cv := conv.NewConverter(
		conv.Def(i2s),
	)

	// Storing a conversion
	cv.Store(
		conv.Def(r2s),
	)

	// with destination type as type parameter, and source type inferred:
	a, _ := conv.To[string](cv, 1)
	b, _ := conv.To[string](cv, '1')
	fmt.Println(a)
	fmt.Println(b)

	// converting a type to itself is always defined
	c, _ := conv.To[string](cv, "one")
	fmt.Println(c)
}
Output:

(int) 1
(rune) 1
one

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lookup

func Lookup[SRC any, DST any](cv Converter) (func(SRC) (DST, error), bool)

Lookup returns a conversion function from source to destination type, if found.

Example
package main

import (
	"fmt"

	"github.com/AndrewHarrisSPU/lift/conv"
)

func main() {
	type rgb struct{ r, g, b uint8 }

	rgb2hex := func(color rgb) (string, error) {
		return fmt.Sprintf("#%2x%2x%2x", color.r, color.g, color.b), nil
	}

	cv := conv.NewConverter(
		conv.Def(rgb2hex),
	)

	palette := []rgb{
		rgb{0x29, 0xbe, 0xb0},
		rgb{0xe0, 0xb0, 0xff},
	}

	// pretending we're in a scope far, far away ...
	convFunc, _ := conv.Lookup[rgb, string](cv)
	for _, color := range palette {
		hex, _ := convFunc(color)
		fmt.Println(hex)
	}
}
Output:

#29beb0
#e0b0ff

func To

func To[DST any, SRC any](cv Converter, src SRC) (dst DST, err error)

To converts the src argument to the provided DST type. Conversion may fail if a conversion function isn't found, or it may fail if a particular value fails to convert.

Example (Failure)

Conversion success or failure can depend on value. This is why conv insists on conversion functions with an error return.

package main

import (
	"fmt"

	"github.com/AndrewHarrisSPU/lift/conv"
)

func main() {
	i2r := func(i int) (rune, error) {
		if 0 <= i && i <= 9 {
			return rune(i + '0'), nil
		}
		return ' ', fmt.Errorf("oops")
	}

	cv := conv.NewConverter(
		conv.Def(i2r),
	)

	if _, err := conv.To[rune](cv, 10); err != nil {
		fmt.Print(err.Error())
	}
}
Output:

oops

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

A Converter indexes conversion functions

func NewConverter

func NewConverter(defs ...Entry) Converter

func (Converter) Delete

func (cv Converter) Delete(keys ...Entry)

Delete removes a defined conversion from the Converter.

Example
package main

import (
	"fmt"

	"github.com/AndrewHarrisSPU/lift/conv"
)

func main() {
	bit2n := func(bit bool) (n int, err error) {
		if bit {
			n = 1
		}
		return
	}

	cv := conv.NewConverter(
		conv.Def(bit2n),
	)

	cv.Delete(
		conv.Def[bool, int](nil),
	)

	if _, err := conv.To[int](cv, true); err != nil {
		fmt.Println(err.Error())
	}
}
Output:

Conversion not found: bool->int

func (Converter) Store

func (cv Converter) Store(defs ...Entry)

Store defines a conversion in the Converter.

type Entry

type Entry lift.Sym

An entry is a wrapped conversion function

func Def

func Def[SRC any, DST any](convFunc func(SRC) (DST, error)) Entry

Def wraps a conversion function, yielding an Entry

Jump to

Keyboard shortcuts

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