akornatskyy/scheduler

Arbitrary string as an ID

Closed this issue · 2 comments

Allow an arbitrary string to be used as an id for collection and job. Drop UUID restriction.

While UUID can be supported, the service will generate IDs per the following:

import (
	"crypto/rand"
	"encoding/base64"
	"io"
)

func NewID() string {
	id := make([]byte, 8)
	_, err := io.ReadAtLeast(rand.Reader, id, 8)
	if err != nil {
		return ""
	}
        if id[0] >= 248 { // Exclude '-' and '_'
		id[0] -= 248
	}
	return base64.RawURLEncoding.EncodeToString(id)
}

Required to match URL safe characters only:

const idPattern = "^[A-Za-z0-9][A-Za-z0-9_\\-]*$"