must.go 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Package 'must' provides another alternative to the 'fluent' package,
// providing many helpful functions for wrapping methods with multiple returns
// into a single return (converting errors into panics).
//
// It's useful especially for testing code and other situations where panics
// are not problematic.
//
// Unlike the 'fluent' package, panics are not of any particular type.
// There is no equivalent to the `fluent.Recover` feature in the 'must' package.
//
// Because golang supports implied destructuring of multiple-return functions
// into arguments for another funtion of matching arity, most of the 'must'
// functions can used smoothly in a pointfree/chainable form, like this:
//
//		must.Node(SomeNodeBuilder{}.CreateString("a"))
//
package must

import (
tavit ohanian's avatar
tavit ohanian committed
20
	ld "gitlab.dms3.io/ld/go-ld-prime"
tavit ohanian's avatar
tavit ohanian committed
21
	"gitlab.dms3.io/ld/go-ld-prime/schema"
22 23
)

24 25 26 27 28 29 30 31 32
// must.NotError simply panics if given an error.
// It helps turn multi-line code into one-liner code in situations where
// you simply don't care.
func NotError(e error) {
	if e != nil {
		panic(e)
	}
}

33 34 35 36 37 38 39 40
// must.Node helps write pointfree/chainable-style code
// by taking a Node and an error and transforming any error into a panic.
//
// Because golang supports implied destructuring of multiple-return functions
// into arguments for another funtion of matching arity, it can be used like this:
//
//		must.Node(SomeNodeBuilder{}.CreateString("a"))
//
tavit ohanian's avatar
tavit ohanian committed
41
func Node(n ld.Node, e error) ld.Node {
42 43 44 45 46 47 48 49
	if e != nil {
		panic(e)
	}
	return n
}

// must.TypedNode helps write pointfree/chainable-style code
// by taking a Node and an error and transforming any error into a panic.
tavit ohanian's avatar
tavit ohanian committed
50
// It will also cast the `ld.Node` to a `schema.TypedNode`, panicking if impossible.
51 52 53 54 55 56
//
// Because golang supports implied destructuring of multiple-return functions
// into arguments for another funtion of matching arity, it can be used like this:
//
//		must.TypedNode(SomeNodeBuilder{}.CreateString("a"))
//
tavit ohanian's avatar
tavit ohanian committed
57
func TypedNode(n ld.Node, e error) schema.TypedNode {
58 59 60
	if e != nil {
		panic(e)
	}
61
	return n.(schema.TypedNode)
62
}
63 64 65 66 67 68 69

// must.True panics if the given bool is false.
func True(v bool) {
	if !v {
		panic("must.True")
	}
}
70 71 72 73

// must.String unboxes the given Node via AsString,
// panicking in the case that the Node isn't of string kind,
// and otherwise returning the bare native string.
tavit ohanian's avatar
tavit ohanian committed
74
func String(n ld.Node) string {
75 76 77 78 79 80 81 82 83 84
	if v, err := n.AsString(); err != nil {
		panic(err)
	} else {
		return v
	}
}

// must.Int unboxes the given Node via AsInt,
// panicking in the case that the Node isn't of int kind,
// and otherwise returning the bare native int.
tavit ohanian's avatar
tavit ohanian committed
85
func Int(n ld.Node) int64 {
86 87 88 89 90 91
	if v, err := n.AsInt(); err != nil {
		panic(err)
	} else {
		return v
	}
}