2023-09-05 00:08:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2024-03-03 15:45:45 -07:00
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"slices"
|
2023-09-05 00:08:09 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type SerializedType uint64
|
2023-09-20 19:14:28 -06:00
|
|
|
|
2023-09-11 21:47:53 -06:00
|
|
|
func (t SerializedType) String() string {
|
|
|
|
return fmt.Sprintf("0x%x", uint64(t))
|
|
|
|
}
|
|
|
|
|
2023-09-05 00:08:09 -06:00
|
|
|
type ExtType SerializedType
|
2023-09-20 19:14:28 -06:00
|
|
|
|
2023-09-12 19:00:48 -06:00
|
|
|
func (t ExtType) String() string {
|
|
|
|
return fmt.Sprintf("0x%x", uint64(t))
|
|
|
|
}
|
|
|
|
|
2023-09-05 00:08:09 -06:00
|
|
|
type NodeType SerializedType
|
2023-09-20 19:14:28 -06:00
|
|
|
|
2023-09-12 20:30:18 -06:00
|
|
|
func (t NodeType) String() string {
|
|
|
|
return fmt.Sprintf("0x%x", uint64(t))
|
|
|
|
}
|
|
|
|
|
2023-09-05 00:08:09 -06:00
|
|
|
type SignalType SerializedType
|
2023-09-20 19:14:28 -06:00
|
|
|
|
2023-09-12 20:30:18 -06:00
|
|
|
func (t SignalType) String() string {
|
|
|
|
return fmt.Sprintf("0x%x", uint64(t))
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:18:14 -07:00
|
|
|
type FieldTag SerializedType
|
|
|
|
|
|
|
|
func (t FieldTag) String() string {
|
|
|
|
return fmt.Sprintf("0x%x", uint64(t))
|
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func NodeTypeFor(name string, extensions []ExtType, mappings map[string]FieldIndex) NodeType {
|
|
|
|
digest := []byte("GRAPHVENT_NODE[" + name + "] - ")
|
|
|
|
for _, ext := range(extensions) {
|
|
|
|
digest = binary.BigEndian.AppendUint64(digest, uint64(ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
digest = binary.BigEndian.AppendUint64(digest, 0)
|
|
|
|
|
|
|
|
sorted_keys := make([]string, len(mappings))
|
|
|
|
i := 0
|
|
|
|
for key := range(mappings) {
|
|
|
|
sorted_keys[i] = key
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
slices.Sort(sorted_keys)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, key := range(sorted_keys) {
|
|
|
|
digest = append(digest, []byte(key + ":")...)
|
|
|
|
digest = binary.BigEndian.AppendUint64(digest, uint64(mappings[key].Extension))
|
|
|
|
digest = append(digest, []byte(mappings[key].Field + "|")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := sha512.Sum512(digest)
|
|
|
|
return NodeType(binary.BigEndian.Uint64(hash[0:8]))
|
2023-09-05 00:08:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func SerializedTypeFor[T any]() SerializedType {
|
|
|
|
t := reflect.TypeFor[T]()
|
2024-03-04 17:30:42 -07:00
|
|
|
digest := []byte(t.String())
|
2024-03-03 15:45:45 -07:00
|
|
|
hash := sha512.Sum512(digest)
|
|
|
|
return SerializedType(binary.BigEndian.Uint64(hash[0:8]))
|
2023-09-05 00:08:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func ExtTypeFor[E any, T interface { *E; Extension}]() ExtType {
|
|
|
|
return ExtType(SerializedTypeFor[E]())
|
2023-09-05 00:08:09 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func SignalTypeFor[S Signal]() SignalType {
|
|
|
|
return SignalType(SerializedTypeFor[S]())
|
2023-11-05 21:18:14 -07:00
|
|
|
}
|
|
|
|
|
2024-03-03 15:45:45 -07:00
|
|
|
func Hash(base, data string) SerializedType {
|
|
|
|
digest := []byte(base + ":" + data)
|
|
|
|
hash := sha512.Sum512(digest)
|
|
|
|
return SerializedType(binary.BigEndian.Uint64(hash[0:8]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFieldTag(tag string) FieldTag {
|
|
|
|
return FieldTag(Hash("GRAPHVENT_FIELD_TAG", tag))
|
|
|
|
}
|