github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/genutil/ensure.go (about)

     1  package genutil
     2  
     3  import "github.com/authzed/spicedb/pkg/spiceerrors"
     4  
     5  // MustEnsureUInt32 is a helper function that calls EnsureUInt32 and panics on error.
     6  func MustEnsureUInt32(value int) uint32 {
     7  	ret, err := EnsureUInt32(value)
     8  	if err != nil {
     9  		panic(err)
    10  	}
    11  	return ret
    12  }
    13  
    14  // EnsureUInt32 ensures that the specified value can be represented as a uint32.
    15  func EnsureUInt32(value int) (uint32, error) {
    16  	if value > int(^uint32(0)) {
    17  		return 0, spiceerrors.MustBugf("specified value is too large to fit in a uint32")
    18  	}
    19  	return uint32(value), nil
    20  }
    21  
    22  // EnsureUInt8 ensures that the specified value can be represented as a uint8.
    23  func EnsureUInt8(value int) (uint8, error) {
    24  	if value > int(^uint8(0)) {
    25  		return 0, spiceerrors.MustBugf("specified value is too large to fit in a uint8")
    26  	}
    27  	return uint8(value), nil
    28  }