50 lines
691 B
Go
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
|
|
}
|