examples

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct{}

Bool shows how to use set.V(&boolType).

func (Bool) Set

func (b Bool) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a bool.

Example (Floats)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	b1, b2 := false, false
	fmt.Println(b1, b2)
	v1, v2 := set.V(&b1), set.V(&b2)

	v1.To(float32(1))
	v2.To(float64(1))
	fmt.Println(b1, b2)

	v1.To(float32(0))
	v2.To(float64(0))
	fmt.Println(b1, b2)

	v1.To(float32(3.14))
	v2.To(float64(3.14))
	fmt.Println(b1, b2)

}
Output:

false false
true true
false false
true true
Example (Ints)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	b1, b2 := false, false
	fmt.Println(b1, b2)
	v1, v2 := set.V(&b1), set.V(&b2)

	v1.To(int32(1))
	v2.To(int64(-1))
	fmt.Println(b1, b2)

	v1.To(int32(0))
	v2.To(int64(0))
	fmt.Println(b1, b2)

	v1.To(int8(3))
	v2.To(int16(-3))
	fmt.Println(b1, b2)

}
Output:

false false
true true
false false
true true
Example (Strings)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	b1, b2 := false, false
	fmt.Println(b1, b2)
	v1, v2 := set.V(&b1), set.V(&b2)

	v1.To("True")
	v2.To("TRUE")
	fmt.Println(b1, b2)

	v1.To("0")
	v2.To("false")
	fmt.Println(b1, b2)

	v1.To("true")
	v2.To("1")
	fmt.Println(b1, b2)

}
Output:

false false
true true
false false
true true
Example (Uints)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	b1, b2 := false, false
	fmt.Println(b1, b2)
	v1, v2 := set.V(&b1), set.V(&b2)

	v1.To(uint32(1))
	v2.To(uint64(1))
	fmt.Println(b1, b2)

	v1.To(uint32(0))
	v2.To(uint64(0))
	fmt.Println(b1, b2)

	v1.To(uint8(3))
	v2.To(uint16(3))
	fmt.Println(b1, b2)

}
Output:

false false
true true
false false
true true

type BoolSlice

type BoolSlice struct{}

BoolSlice shows how to use set.V(&sliceBoolType)

func (BoolSlice) Set

func (b BoolSlice) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a []bool.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	b := []bool{}
	fmt.Println(b)
	v := set.V(&b)

	// A slice of specific types (float32)
	v.To([]float32{1, 0})
	fmt.Println(b)

	// A slice of int64
	v.To([]int64{0, 1, 0})
	fmt.Println(b)

	// Mixed interface{} slice.
	v.To([]interface{}{"1", "False", true, 0})
	fmt.Println(b)

}
Output:

[]
[true false]
[false true false]
[true false true false]
Example (CreatesCopy)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	slice := []bool{true, false, true}
	var dest []bool
	set.V(&dest).To(slice)

	fmt.Println(slice[1])
	dest[1] = true
	fmt.Println(slice[1])

}
Output:

false
false

type Float

type Float struct{}

Float shows how to use set.V(&floatType)

func (Float) Set

func (f Float) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a float.

Example (Bool)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	f1, f2 := float32(0), float64(0)
	fmt.Println(f1, f2)
	v1, v2 := set.V(&f1), set.V(&f2)

	v1.To(true)
	v2.To(true)
	fmt.Println(f1, f2)

	v1.To(false)
	v2.To(false)
	fmt.Println(f1, f2)

}
Output:

0 0
1 1
0 0
Example (Int)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	f1, f2 := float32(0), float64(0)
	fmt.Println(f1, f2)
	v1, v2 := set.V(&f1), set.V(&f2)

	v1.To(int32(1))
	v2.To(int64(1))
	fmt.Println(f1, f2)

	v1.To(int32(-3))
	v2.To(int64(-3))
	fmt.Println(f1, f2)

}
Output:

0 0
1 1
-3 -3
Example (String)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	f1, f2 := float32(0), float64(0)
	fmt.Println(f1, f2)
	v1, v2 := set.V(&f1), set.V(&f2)

	v1.To("1")
	v2.To("1")
	fmt.Println(f1, f2)

	v1.To("3.14")
	v2.To("3.14")
	fmt.Println(f1, f2)

	v1.To("-3.59")
	v2.To("-3.59")
	fmt.Println(f1, f2)

}
Output:

0 0
1 1
3.14 3.14
-3.59 -3.59
Example (Uint)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	f1, f2 := float32(0), float64(0)
	fmt.Println(f1, f2)
	v1, v2 := set.V(&f1), set.V(&f2)

	v1.To(uint32(1))
	v2.To(uint64(1))
	fmt.Println(f1, f2)

	v1.To(uint32(3))
	v2.To(uint64(3))
	fmt.Println(f1, f2)

}
Output:

0 0
1 1
3 3

type FloatSlice

type FloatSlice struct{}

FloatSlice shows how to use set.V(&sliceFloatType)

func (FloatSlice) Set

func (f FloatSlice) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a []float.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	f := []float32{}
	fmt.Println(f) // []
	v := set.V(&f)

	// A slice of specific types (string)
	v.To([]string{"1.1", "0.5"})
	fmt.Println(f)

	// A slice of int64
	v.To([]int64{0, 1, 0})
	fmt.Println(f)

	// Mixed interface{} slice.
	v.To([]interface{}{"3.14", "-5", false, true})
	fmt.Println(f)

}
Output:

[]
[1.1 0.5]
[0 1 0]
[3.14 -5 0 1]

type Int

type Int struct{}

Int shows how to use set.V(&intType)

func (Int) Set

func (i Int) Set()

Set is a stub. The proceeding examples demonstrate coercing types into an int.

Example (Bool)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := int32(0), int64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(true)
	v2.To(true)
	fmt.Println(i1, i2)

	v1.To(false)
	v2.To(false)
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
Example (Float)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := int32(0), int64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(float32(1.15))
	v2.To(float64(1.59))
	fmt.Println(i1, i2)

	v1.To(float32(0))
	v2.To(float64(0))
	fmt.Println(i1, i2)

	v1.To(float32(-1.15))
	v2.To(float64(-1.59))
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
-1 -1
Example (String)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := int32(0), int64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To("1")
	v2.To("1")
	fmt.Println(i1, i2)

	v1.To("-1")
	v2.To("-1")
	fmt.Println(i1, i2)

	v1.To("1.59")
	v2.To("1.59")
	fmt.Println(i1, i2)

	v1.To("-3.14")
	v2.To("-3.14")
	fmt.Println(i1, i2)

	v1.To("0")
	v2.To("0")
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
-1 -1
1 1
-3 -3
0 0
Example (Uint)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := int32(0), int64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(uint32(1))
	v2.To(uint64(1))
	fmt.Println(i1, i2)

	v1.To(uint32(0))
	v2.To(uint64(0))
	fmt.Println(i1, i2)

	v1.To(uint32(999))
	v2.To(uint64(999))
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
999 999

type IntSlice

type IntSlice struct{}

IntSlice shows how to use set.V(&sliceIntType)

func (IntSlice) Set

func (i IntSlice) Set()

Set is a stub. The proceeding examples demonstrate coercing types into an []int.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i := []int32{}
	fmt.Println(i) // []
	v := set.V(&i)

	// A slice of specific types (string)
	v.To([]string{"1.1", "0.5"})
	fmt.Println(i)

	// A slice of int64
	v.To([]float64{0, 1, 0})
	fmt.Println(i)

	// Mixed interface{} slice.
	v.To([]interface{}{"3.14", "-5", false, true})
	fmt.Println(i)

}
Output:

[]
[1 0]
[0 1 0]
[3 -5 0 1]

type Scalar

type Scalar struct{}

Scalar shows how to use set.V(&scalarType)

func (Scalar) Set

func (s Scalar) Set()

Set is a stub. The proceeding examples demonstrate setting non-scalar values on scalar destinations.

Example (Slice)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	// This example demonstrates calling Set() with a slice as its argument when the Value is a scalar:
	//	set.V(&Scalar).To(Slice)
	//
	// The Value is set to its proper zero value.  Then if the slice is non-nil and non-zero length the scalar
	// is set to the last element in the slice.
	{
		b1, b2 := true, true
		v1, v2 := set.V(&b1), set.V(&b2)
		v1.To([]interface{}{"Hello", 1, "False"})
		v2.To(([]interface{})(nil))
		fmt.Println(b1, b2)
	}
	{
		i1, i2 := int(42), int(42)
		v1, v2 := set.V(&i1), set.V(&i2)
		v1.To([]interface{}{"Hello", 1, "False"})
		v2.To(([]interface{})(nil))
		fmt.Println(i1, i2)
		v1.To([]interface{}{"Hello", 1, "3.14"})
		v2.To([]interface{}{})
		fmt.Println(i1, i2)
	}
}
Output:

false false
0 0
3 0

type String

type String struct{}

String shows how to use set.V(&stringType)

func (String) Set

func (s String) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a string.

Example (Bool)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	s1, s2 := "", ""
	fmt.Println(s1, s2, "(empty)")
	v1, v2 := set.V(&s1), set.V(&s2)

	v1.To(true)
	v2.To(true)
	fmt.Println(s1, s2)

	v1.To(false)
	v2.To(false)
	fmt.Println(s1, s2)

}
Output:

(empty)
true true
false false
Example (Float)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	s1, s2 := "", ""
	fmt.Println(s1, s2, "(empty)")
	v1, v2 := set.V(&s1), set.V(&s2)

	v1.To(float32(1.15))
	v2.To(float64(1.59))
	fmt.Println(s1, s2)

	v1.To(float32(0))
	v2.To(float64(0))
	fmt.Println(s1, s2)

	v1.To(float32(-1.15))
	v2.To(float64(-1.59))
	fmt.Println(s1, s2)

}
Output:

(empty)
1.15 1.59
0 0
-1.15 -1.59
Example (Int)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	s1, s2 := "", ""
	fmt.Println(s1, s2, "(empty)")
	v1, v2 := set.V(&s1), set.V(&s2)

	v1.To(int32(1))
	v2.To(int64(1))
	fmt.Println(s1, s2)

	v1.To(int32(0))
	v2.To(int64(0))
	fmt.Println(s1, s2)

	v1.To(int32(999))
	v2.To(int64(999))
	fmt.Println(s1, s2)

	v1.To(int32(-999))
	v2.To(int64(-999))
	fmt.Println(s1, s2)

}
Output:

(empty)
1 1
0 0
999 999
-999 -999
Example (Uint)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	s1, s2 := "", ""
	fmt.Println(s1, s2, "(empty)")
	v1, v2 := set.V(&s1), set.V(&s2)

	v1.To(uint32(1))
	v2.To(uint64(1))
	fmt.Println(s1, s2)

	v1.To(uint32(0))
	v2.To(uint64(0))
	fmt.Println(s1, s2)

	v1.To(uint32(999))
	v2.To(uint64(999))
	fmt.Println(s1, s2)

}
Output:

(empty)
1 1
0 0
999 999

type StringSlice

type StringSlice struct{}

StringSlice shows how to use set.V(&sliceStringType)

func (StringSlice) Set

func (s StringSlice) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a []string.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	s := []string{}
	fmt.Println(s) // []
	v := set.V(&s)

	// A slice of specific types (string)
	v.To([]string{"1.1", "0.5"})
	fmt.Println(s)

	// A slice of int64
	v.To([]float64{0, 1, 0})
	fmt.Println(s)

	// Mixed interface{} slice.
	v.To([]interface{}{3.14, 5, false, true})
	fmt.Println(s)

}
Output:

[]
[1.1 0.5]
[0 1 0]
[3.14 5 false true]

type Struct

type Struct struct{}

Struct shows how to use set.V(&structType)

func (Struct) Fill

func (s Struct) Fill()

Fill is a stub. The proceeding examples demonstrate calling Fill on a struct.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
)

func main() {
	type T struct {
		Bool   bool
		Int    int
		Uint   uint
		String string
	}
	values := map[string]interface{}{
		"Bool":   true,
		"Int":    -42,
		"Uint":   42,
		"String": "Hello, World!",
	}
	fn := func(key string) interface{} {
		if value, ok := values[key]; ok {
			return value
		}
		return nil
	}

	var t T
	if err := set.V(&t).Fill(set.GetterFunc(fn)); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(t.Bool, t.Int, t.Uint, t.String)
	}
}
Output:

true -42 42 Hello, World!
Example (ByTag)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
)

func main() {
	type T struct {
		Bool   bool   `someTag:"myBool"`
		Int    int    `someTag:"myInt"`
		Uint   uint   `someTag:"myUint"`
		String string `someTag:"myString"`
	}
	values := map[string]interface{}{
		"myBool":   true,
		"myInt":    -42,
		"myUint":   42,
		"myString": "Hello, World!",
	}
	fn := func(key string) interface{} {
		if value, ok := values[key]; ok {
			return value
		}
		return nil
	}

	var t T
	if err := set.V(&t).FillByTag("someTag", set.GetterFunc(fn)); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(t.Bool, t.Int, t.Uint, t.String)
	}
}
Output:

true -42 42 Hello, World!
Example (ByTagWithPunctuation)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
)

func main() {
	type T struct {
		Bool   bool   `some.tag:"my-bool"`
		Int    int    `some.tag:"my-int"`
		Uint   uint   `some.tag:"my-uint"`
		String string `some.tag:"my-string"`
	}
	values := map[string]interface{}{
		"my-bool":   true,
		"my-int":    -42,
		"my-uint":   42,
		"my-string": "Hello, World!",
	}
	fn := func(key string) interface{} {
		if value, ok := values[key]; ok {
			return value
		}
		return nil
	}

	var t T
	if err := set.V(&t).FillByTag("some.tag", set.GetterFunc(fn)); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(t.Bool, t.Int, t.Uint, t.String)
	}
}
Output:

true -42 42 Hello, World!

type Uint

type Uint struct{}

Uint shows how to use set.V(&uintType)

func (Uint) Set

func (i Uint) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a uint.

Example (Bool)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := uint32(0), uint64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(true)
	v2.To(true)
	fmt.Println(i1, i2)

	v1.To(false)
	v2.To(false)
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
Example (Float)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := uint32(0), uint64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(float32(1.15))
	v2.To(float64(1.59))
	fmt.Println(i1, i2)

	v1.To(float32(0))
	v2.To(float64(0))
	fmt.Println(i1, i2)

	v1.To(float32(-1.15))
	v2.To(float64(-1.59))
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
0 0
Example (Int)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := uint32(0), uint64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To(int32(1))
	v2.To(int64(1))
	fmt.Println(i1, i2)

	v1.To(int32(0))
	v2.To(int64(0))
	fmt.Println(i1, i2)

	v1.To(int32(999))
	v2.To(int64(999))
	fmt.Println(i1, i2)

	v1.To(int32(-999))
	v2.To(int64(-999))
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
999 999
0 0
Example (String)
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	i1, i2 := uint32(0), uint64(0)
	fmt.Println(i1, i2)
	v1, v2 := set.V(&i1), set.V(&i2)

	v1.To("1")
	v2.To("1")
	fmt.Println(i1, i2)

	v1.To("-1")
	v2.To("-1")
	fmt.Println(i1, i2)

	v1.To("1.59")
	v2.To("1.59")
	fmt.Println(i1, i2)

	v1.To("-3.14")
	v2.To("-3.14")
	fmt.Println(i1, i2)

	v1.To("999")
	v2.To("999")
	fmt.Println(i1, i2)

}
Output:

0 0
1 1
0 0
1 1
0 0
999 999

type UintSlice

type UintSlice struct{}

UintSlice shows how to use set.V(&sliceUintType)

func (UintSlice) Set

func (i UintSlice) Set()

Set is a stub. The proceeding examples demonstrate coercing types into a []uint.

Example
package main

import (
	"fmt"

	"github.com/nofeaturesonlybugs/set"
	_ "github.com/nofeaturesonlybugs/set/examples"
)

func main() {
	u := []uint32{}
	fmt.Println(u) // []
	v := set.V(&u)

	// A slice of specific types (string)
	v.To([]string{"1.1", "0.5"})
	fmt.Println(u)

	// A slice of int64
	v.To([]float64{0, 1, 0})
	fmt.Println(u)

	// Mixed interface{} slice.
	v.To([]interface{}{"3.14", "5", false, true})
	fmt.Println(u)

}
Output:

[]
[1 0]
[0 1 0]
[3 5 0 1]

Jump to

Keyboard shortcuts

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