value

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package value handles mapping values to the settings type. The package can handle input from a number of source and allows values to be mapped to a provided pointer so that the calling program can deal with concrete types.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidType is returned if an attempt is made to Coerce data to an
	// unsupported type.
	ErrInvalidType = errors.New("value.Data: invalid type")

	// ErrAssignmentFailure is returned if the Data type can't be assigned to
	// the given type.
	ErrAssignmentFailure = errors.New("value.Data: assignment failure")

	// ErrInvalidPointerType is returned if the Pointer in the Data type isn't
	// supported.
	ErrInvalidPointerType = errors.New("value.Data: invalid pointer type")
)

Functions

func Chase

func Chase(in interface{}) reflect.Type

Chase down the actual type of an interface. If it's a pointer get the type being pointed to, if that's an interface get the underlying type for that.

Types

type Data

type Data struct {
	Set     bool
	Pointer interface{}
}

Data holds an untyped (interface{}) value which can be assigned to a bool, int, int64, uint, uint64, float64, string, or time.Duration.

func Coerce

func Coerce(data string, typeOf interface{}) (Data, error)

Coerce the given string into a Data type holding a value of typeOf. If the data cannot be coerced the the underlying parse error will be returned. An invalid typeOf will result in ErrInvalidType.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/goconfigure/value"
)

func main() {
	var typeOf string
	if v, err := value.Coerce("text", &typeOf); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(v)
	}

}
Output:

{true text}

func New

func New(data interface{}) Data

New creates a new Data type with the given value.

func (Data) AssignTo

func (d Data) AssignTo(pointer interface{}) (err error)

AssignTo sets the given pointer to point to the value held by this Data type. ErrInvalidPointerType is returned if the given pointer is not a valid type, or is not a reflect.Ptr. ErrAssignmentFailure is returned if there is an error assigning the Data type to the pointer.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/goconfigure/value"
)

func main() {
	var v string
	d := value.New("example")
	if err := d.AssignTo(&v); err != nil {
		fmt.Println(err)
	}

	fmt.Println(v)

}
Output:

example

Jump to

Keyboard shortcuts

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