atoa

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package atoa - converters for any to any

Package atoa convert a string to any type following a given meme.

For example:

v, _ = atoa.Parse("8.97", int32(9))
assert.Equal(t, v, int32(8))
v = atoa.MustParse("8.97", int32(9), atoa.WithFeatures(atoa.RoundNumbers))
assert.Equal(t, v, int32(9))
v = atoa.MustParse("apple=1, banana=2, orange=3", map[string]int{})
assert.Equal(t, v, map[string]int{"apple": 1, "banana": 2, "orange": 3})
v = atoa.MustParse("8,9,7", []int{})
assert.Equal(t, v, []int{8, 9, 7})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParse

func MustParse(str string, meme any, opts ...Opt) (v any)

MustParse convert a string to any type following a given meme.

For example:

v = MustParse("8.97", int32(9), WithFeatures(RoundNumbers))
assert.Equal(t, v, int32(9))

func New

func New(opts ...Opt) *toS

func Parse

func Parse(str string, meme any, opts ...Opt) (v any, err error)

Parse convert a string to any type following a given meme.

For example:

v, _ := Parse("8.97", int32(9))
assert.Equal(t, v, int32(8))
v = MustParse("8.97", int32(9), WithFeatures(RoundNumbers))
assert.Equal(t, v, int32(9))
v = MustParse("apple=1, banana=2, orange=3", map[string]int{})
assert.Equal(t, v, map[string]int{"apple": 1, "banana": 2, "orange": 3})
v = MustParse("8,9,7", []int{})
assert.Equal(t, v, []int{8, 9, 7})

Types

type Converter

type Converter func(str string, targetType reflect.Type) (ret any, err error)

type Opt

type Opt func(s *toS)

Jump to

Keyboard shortcuts

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