Permit -1 for randlen and maxlen
This allows -1 for randlen and maxlen to use defaults. The default behavior is that randlen uses sha1.Size and maxlen is "no max".
This commit is contained in:
parent
c61dcb5206
commit
bda607abd8
|
@ -4,6 +4,7 @@ package resource
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/pulumi/lumi/pkg/eval/rt"
|
||||
|
@ -56,13 +57,17 @@ const (
|
|||
// NewUniqueHex generates a new "random" hex string for use by resource providers. It has the given optional prefix and
|
||||
// the total length is capped to the maxlen. Note that capping to maxlen necessarily increases the risk of collisions.
|
||||
func NewUniqueHex(prefix string, maxlen, randlen int) string {
|
||||
if randlen == -1 {
|
||||
randlen = sha1.Size // default to SHA1 size.
|
||||
}
|
||||
|
||||
bs := make([]byte, randlen)
|
||||
n, err := rand.Read(bs)
|
||||
contract.Assert(err == nil)
|
||||
contract.Assert(n == len(bs))
|
||||
|
||||
str := prefix + hex.EncodeToString(bs)
|
||||
if len(str) > maxlen {
|
||||
if maxlen != -1 && len(str) > maxlen {
|
||||
str = str[:maxlen]
|
||||
}
|
||||
return str
|
||||
|
@ -71,5 +76,5 @@ func NewUniqueHex(prefix string, maxlen, randlen int) string {
|
|||
// NewUniqueHexID generates a new "random" hex ID for use by resource providers. It has the given optional prefix and
|
||||
// the total length is capped to the maxlen. Note that capping to maxlen necessarily increases the risk of collisions.
|
||||
func NewUniqueHexID(prefix string, maxlen, randlen int) ID {
|
||||
return ID(NewUniqueHex(prefix, randlen, maxlen))
|
||||
return ID(NewUniqueHex(prefix, maxlen, randlen))
|
||||
}
|
||||
|
|
|
@ -3,13 +3,14 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_NewUniqueHex(t *testing.T) {
|
||||
func TestNewUniqueHex(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
randlen := 20
|
||||
maxlen := 100
|
||||
|
@ -18,7 +19,7 @@ func Test_NewUniqueHex(t *testing.T) {
|
|||
assert.Equal(t, true, strings.HasPrefix(id, prefix))
|
||||
}
|
||||
|
||||
func Test_NewUniqueHex_Maxlen(t *testing.T) {
|
||||
func TestNewUniqueHexMaxLen(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
randlen := 20
|
||||
maxlen := 20
|
||||
|
@ -26,3 +27,35 @@ func Test_NewUniqueHex_Maxlen(t *testing.T) {
|
|||
assert.Equal(t, maxlen, len(id))
|
||||
assert.Equal(t, true, strings.HasPrefix(id, prefix))
|
||||
}
|
||||
|
||||
func TestNewUniqueDefaults(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
id := NewUniqueHex(prefix, -1, -1)
|
||||
assert.Equal(t, len(prefix)+(sha1.Size*2), len(id))
|
||||
assert.Equal(t, true, strings.HasPrefix(id, prefix))
|
||||
}
|
||||
|
||||
func TestNewUniqueHexID(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
randlen := 20
|
||||
maxlen := 100
|
||||
id := NewUniqueHexID(prefix, maxlen, randlen)
|
||||
assert.Equal(t, len(prefix)+randlen*2, len(id))
|
||||
assert.Equal(t, true, strings.HasPrefix(string(id), prefix))
|
||||
}
|
||||
|
||||
func TestNewUniqueHexMaxLenID(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
randlen := 20
|
||||
maxlen := 20
|
||||
id := NewUniqueHexID(prefix, maxlen, randlen)
|
||||
assert.Equal(t, maxlen, len(id))
|
||||
assert.Equal(t, true, strings.HasPrefix(string(id), prefix))
|
||||
}
|
||||
|
||||
func TestNewUniqueDefaultsID(t *testing.T) {
|
||||
prefix := "prefix"
|
||||
id := NewUniqueHexID(prefix, -1, -1)
|
||||
assert.Equal(t, len(prefix)+(sha1.Size*2), len(id))
|
||||
assert.Equal(t, true, strings.HasPrefix(string(id), prefix))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue