buildr/modules/state/key.go
Peter fee941a0e4
feat(state): introduce SQLite based state store
Allow modules to keep a state of their latest execution and skp if not necessary
2023-05-02 18:44:47 +02:00

50 lines
691 B
Go

package state
import (
"crypto/md5"
"fmt"
)
func KeyOfStrings(parts ...any) StringsKey {
nonEmpty := make([]string, 0, len(parts))
for i := range parts {
if parts[i] == nil {
continue
}
switch s := parts[i].(type) {
case string:
if s != "" {
nonEmpty = append(nonEmpty, s)
}
case fmt.Stringer:
if out := s.String(); out != "" {
nonEmpty = append(nonEmpty, out)
}
}
}
return nonEmpty
}
type StringsKey []string
func (k StringsKey) Bytes() []byte {
if len(k) == 0 {
return nil
}
h := md5.New()
for i := range k {
_, _ = h.Write([]byte(k[i]))
}
return h.Sum(nil)
}
type PlainKey []byte
func (k PlainKey) Bytes() []byte {
return k
}