github.com/ipld/go-ipld-prime@v0.21.0/must/must.go (about)

     1  // Package 'must' provides another alternative to the 'fluent' package,
     2  // providing many helpful functions for wrapping methods with multiple returns
     3  // into a single return (converting errors into panics).
     4  //
     5  // It's useful especially for testing code and other situations where panics
     6  // are not problematic.
     7  //
     8  // Unlike the 'fluent' package, panics are not of any particular type.
     9  // There is no equivalent to the `fluent.Recover` feature in the 'must' package.
    10  //
    11  // Because golang supports implied destructuring of multiple-return functions
    12  // into arguments for another funtion of matching arity, most of the 'must'
    13  // functions can used smoothly in a pointfree/chainable form, like this:
    14  //
    15  //	must.Node(SomeNodeBuilder{}.CreateString("a"))
    16  package must
    17  
    18  import (
    19  	"github.com/ipld/go-ipld-prime/datamodel"
    20  	"github.com/ipld/go-ipld-prime/schema"
    21  )
    22  
    23  // must.NotError simply panics if given an error.
    24  // It helps turn multi-line code into one-liner code in situations where
    25  // you simply don't care.
    26  func NotError(e error) {
    27  	if e != nil {
    28  		panic(e)
    29  	}
    30  }
    31  
    32  // must.Node helps write pointfree/chainable-style code
    33  // by taking a Node and an error and transforming any error into a panic.
    34  //
    35  // Because golang supports implied destructuring of multiple-return functions
    36  // into arguments for another funtion of matching arity, it can be used like this:
    37  //
    38  //	must.Node(SomeNodeBuilder{}.CreateString("a"))
    39  func Node(n datamodel.Node, e error) datamodel.Node {
    40  	if e != nil {
    41  		panic(e)
    42  	}
    43  	return n
    44  }
    45  
    46  // must.TypedNode helps write pointfree/chainable-style code
    47  // by taking a Node and an error and transforming any error into a panic.
    48  // It will also cast the `datamodel.Node` to a `schema.TypedNode`, panicking if impossible.
    49  //
    50  // Because golang supports implied destructuring of multiple-return functions
    51  // into arguments for another funtion of matching arity, it can be used like this:
    52  //
    53  //	must.TypedNode(SomeNodeBuilder{}.CreateString("a"))
    54  func TypedNode(n datamodel.Node, e error) schema.TypedNode {
    55  	if e != nil {
    56  		panic(e)
    57  	}
    58  	return n.(schema.TypedNode)
    59  }
    60  
    61  // must.True panics if the given bool is false.
    62  func True(v bool) {
    63  	if !v {
    64  		panic("must.True")
    65  	}
    66  }
    67  
    68  // must.String unboxes the given Node via AsString,
    69  // panicking in the case that the Node isn't of string kind,
    70  // and otherwise returning the bare native string.
    71  func String(n datamodel.Node) string {
    72  	if v, err := n.AsString(); err != nil {
    73  		panic(err)
    74  	} else {
    75  		return v
    76  	}
    77  }
    78  
    79  // must.Int unboxes the given Node via AsInt,
    80  // panicking in the case that the Node isn't of int kind,
    81  // and otherwise returning the bare native int.
    82  func Int(n datamodel.Node) int64 {
    83  	if v, err := n.AsInt(); err != nil {
    84  		panic(err)
    85  	} else {
    86  		return v
    87  	}
    88  }