-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.go
36 lines (30 loc) · 925 Bytes
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package genmap
import (
"fmt"
)
// Key associates a type with a key, ensuring type safety when setting and fetching values of this type.
type Key[T any, K comparable] interface {
// Key returns a serialisation of the key type. This should be unique for the type, and can be a combination of an
// instance.
// e.g. myStruct_objectName
Key() K
}
type mapKey[T any, K comparable] struct {
key K
}
// Key returns the serialisation of this key.
func (sk mapKey[T, K]) Key() K {
return sk.key
}
// NewKey creates a new Key. The id provided should be unique to the object.
func NewKey[T any, K comparable](id K) Key[T, K] {
return mapKey[T, K]{
key: id,
}
}
// NewStringKey creates a new Key with a string id. This can be used with the string variety of top level functions.
func NewStringKey[T any](id string) Key[T, string] {
var t T
keyName := fmt.Sprintf("%s_%T", id, t)
return NewKey[T, string](keyName)
}