vars

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: MIT Imports: 2 Imported by: 0

README

Package vars provides copy functions for variables.

Quick start

go get github.com/gregoryv/vars

Usage

import github.com/gregoryv/vars

var (
    i int
    s string
)

vars.MustCopy(
    &i, 0, 
    &s, "hello",
)

Documentation

Overview

Package vars provides copy functions for variables.

The purpose of this package is to simplify transfer of data from empty interface{} to a typed destination.

Example (CopyVariables)
package main

import (
	"fmt"

	"github.com/gregoryv/vars"
)

func main() {
	var (
		dest int
		val  interface{} = 1
	)

	err := vars.Copy(&dest, val)
	if err != nil {
		fmt.Println(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(pairs ...interface{}) error

Copy copies pairwise destination, source. Returns error on first error. Panics if pairs is uneven.

Example
package main

import (
	"fmt"

	"github.com/gregoryv/vars"
)

func main() {
	var (
		i int
		s string
	)
	pairs := []interface{}{
		&i, 1, // ok
		&s, "a", // ok
		&i, "b", // bad
		&s, 0, // bad
	}
	err := vars.Copy(pairs...)
	fmt.Println(err)
}
Output:
Copy[5]: not int

func CopyAll

func CopyAll(pairs ...interface{}) error

CopyAll is similar to Copy but does not fail on first error. Returns nil on no errors.

Example
package main

import (
	"fmt"

	"github.com/gregoryv/vars"
)

func main() {
	var (
		i int
		s string
	)
	pairs := []interface{}{
		&i, 1, // ok
		&s, "a", // ok
		&i, "b", // bad
		&s, 0, // bad
	}
	err := vars.CopyAll(pairs...)

	errs := vars.SplitErr(err)
	for _, err := range errs {
		fmt.Println(err)
	}
}
Output:
Copy[5]: not int
Copy[7]: not string

func MapCopy added in v0.3.0

func MapCopy(in map[string]interface{}, pairs ...interface{}) error

MapCopy copies values from the given map. Pairs argument must be an even list of destination <- key pairs. Missing keys are ignored.

Example
package main

import (
	"fmt"

	"github.com/gregoryv/vars"
)

func main() {
	data := map[string]interface{}{
		"name":   "John Doe",
		"age":    54,
		"weight": 94.5, // kilos
		"alive":  true,
	}

	var (
		name   string
		age    int
		weight float64
		alive  bool
		addr   string
	)
	err := vars.MapCopy(data,
		&name, "name",
		&age, "age",
		&weight, "weight",
		&alive, "alive",
		&addr, "addr", // missing values are ignored
	)
	if err != nil {
		fmt.Println(err)
	}
}

func MapCopyAll added in v0.3.0

func MapCopyAll(in map[string]interface{}, pairs ...interface{}) error

MapCopyAll copies values from the given map. Pairs argument must be an even list of destination <- key pairs. Missing keys yield an error.

Example
package main

import (
	"fmt"

	"github.com/gregoryv/vars"
)

func main() {
	data := map[string]interface{}{}

	var addr, name string
	err := vars.MapCopyAll(data,
		&addr, "addr",
		&name, "name",
	)
	if err != nil {
		fmt.Println(err)
		// or each error
		for _, err := range vars.SplitErr(err) {
			fmt.Println(err)
		}
	}
}
Output:
missing "addr", missing "name"
missing "addr"
missing "name"

func SplitErr added in v0.3.0

func SplitErr(in error) []error

SplitErr takes an error produced by CopyAll or MapCopyAll and splits it into multiple errors. Panics if in error is of other origin or nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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