summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/genutil/ensure.go
blob: 56467bae0872dcabc9b3d1b949679349e47b2179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package genutil

import (
	"github.com/ccoveille/go-safecast"

	"github.com/authzed/spicedb/pkg/spiceerrors"
)

// MustEnsureUInt32 is a helper function that calls EnsureUInt32 and panics on error.
func MustEnsureUInt32(value int) uint32 {
	ret, err := EnsureUInt32(value)
	if err != nil {
		panic(err)
	}
	return ret
}

// EnsureUInt32 ensures that the specified value can be represented as a uint32.
func EnsureUInt32(value int) (uint32, error) {
	uint32Value, err := safecast.ToUint32(value)
	if err != nil {
		return 0, spiceerrors.MustBugf("specified value could not be cast to a uint32")
	}
	return uint32Value, nil
}

// EnsureUInt8 ensures that the specified value can be represented as a uint8.
func EnsureUInt8(value int) (uint8, error) {
	uint8Value, err := safecast.ToUint8(value)
	if err != nil {
		return 0, spiceerrors.MustBugf("specified value could not be cast to a uint8")
	}
	return uint8Value, nil
}