is

package module
v0.0.0-...-51f5bbd Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 1 Imported by: 0

README

is – Reflective Assertions for Golang

CircleCI codecov Go Report Card GoDoc License MIT

Package is provides some assertions on reflect.Values and reflect.Types.

Go's reflect package can sometimes leave one asking basic questions, like

Is this a zero value? Is this field exported?

Although there are well-documented ways to answer these kinds of questions, discoverability can be hard.

I hope this package comes in handy for quick and dirty assertions, but would encourage you to use this code as inspiration to writing your own reflect code accurately and hopefully more efficiently.

Is this a zero value?

  • is.Zero checks if a value is equal to the zero value of its underlying type.
  • is.ZeroForType checks if a value is equal to the zero value of the provided type.

These two functions are subtly different, check out the examples on godoc.

TODO

  • is.ExportedField(reflect.StructField)
  • is.ExportedType(reflect.Type)

Documentation

Overview

Package is provides some assertions on reflect.Values and reflect.Types.

Go's reflect package can sometimes leave one asking basic questions, like "how do I know if a value is a zero value", or "is this field exported?". Although there are well-documented ways to answer these kinds of questions, discoverability can be hard.

I hope this package comes in handy for quick and dirty assertions, but would encourage you to use this code as inspiration to writing your own reflect code more accurately and efficiently.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Zero

func Zero(v interface{}) bool

Zero returns true when the underlying value of v is either nil or zero for its underlying type.

Example (False)
a := "a"
b := 1
c := &b
fmt.Println("a:", Zero(a), "b:", Zero(b), "c:", Zero(c))
Output:

a: false b: false c: false
Example (True)
a := ""
b := 0
c := ((*int)(nil))
fmt.Println("a:", Zero(a), "b:", Zero(b), "c:", Zero(c))
Output:

a: true b: true c: true

func ZeroForType

func ZeroForType(v interface{}, typ reflect.Type) bool

ZeroForType returns true when v matches the zero value of typ. If typ is nil, always returns false.

Example (False)
// i is the type interface{}
i := reflect.TypeOf((*interface{})(nil)).Elem()
fmt.Println(
	"a:", ZeroForType("a", i),
	"b:", ZeroForType(1, i),
	"c:", ZeroForType(i, i),
	"d:", ZeroForType(1, nil),
)
Output:

a: false b: false c: false d: false
Example (True)
// i is the type interface{}
i := reflect.TypeOf((*interface{})(nil)).Elem()
fmt.Println(
	"a:", ZeroForType("", reflect.TypeOf("a")),
	"b:", ZeroForType(0, reflect.TypeOf(1)),
	"c:", ZeroForType(nil, reflect.TypeOf(&i)),
	"d:", ZeroForType(nil, reflect.TypeOf(i)),
)
Output:

a: true b: true c: true d: true

Types

This section is empty.

Jump to

Keyboard shortcuts

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