Moved Hash function to single implementation

gql_cataclysm v0.2.3
noah metz 2023-07-27 23:26:58 -06:00
parent f314b46415
commit d40e561728
4 changed files with 26 additions and 23 deletions

@ -9,18 +9,25 @@ import (
"encoding/binary"
)
type NodeType string
func (node NodeType) Hash() uint64 {
hash := sha512.Sum512([]byte(fmt.Sprintf("NODE: %s", string(node))))
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
type Type interface {
String() string
Prefix() string
}
type ExtType string
func (ext ExtType) Hash() uint64 {
hash := sha512.Sum512([]byte(fmt.Sprintf("EXTENSION: %s", string(ext))))
func Hash(t Type) uint64 {
hash := sha512.Sum512([]byte(fmt.Sprintf("%s%s", t.Prefix(), t.String())))
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
}
type NodeType string
func (node NodeType) Prefix() string { return "NODE: " }
func (node NodeType) String() string { return string(node) }
type ExtType string
func (ext ExtType) Prefix() string { return "EXTENSION: " }
func (ext ExtType) String() string { return string(ext) }
//Function to load an extension from bytes
type ExtensionLoadFunc func(*Context, []byte) (Extension, error)
@ -64,7 +71,7 @@ type Context struct {
// Register a NodeType to the context, with the list of extensions it requires
func (ctx *Context) RegisterNodeType(node_type NodeType, extensions []ExtType) error {
type_hash := node_type.Hash()
type_hash := Hash(node_type)
_, exists := ctx.Types[type_hash]
if exists == true {
return fmt.Errorf("Cannot register node type %s, type already exists in context", node_type)
@ -72,7 +79,7 @@ func (ctx *Context) RegisterNodeType(node_type NodeType, extensions []ExtType) e
ext_found := map[ExtType]bool{}
for _, extension := range(extensions) {
_, in_ctx := ctx.Extensions[extension.Hash()]
_, in_ctx := ctx.Extensions[Hash(extension)]
if in_ctx == false {
return fmt.Errorf("Cannot register node type %s, required extension %s not in context", node_type, extension)
}
@ -98,7 +105,7 @@ func (ctx *Context) RegisterExtension(ext_type ExtType, load_fn ExtensionLoadFun
return fmt.Errorf("def has no load function")
}
type_hash := ext_type.Hash()
type_hash := Hash(ext_type)
_, exists := ctx.Extensions[type_hash]
if exists == true {
return fmt.Errorf("Cannot register extension of type %s, type already exists in context", ext_type)

@ -367,7 +367,7 @@ func NewResolveContext(ctx *Context, server *Node, gql_ext *GQLExt, r *http.Requ
return &ResolveContext{
Context: ctx,
GQLContext: ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext),
GQLContext: ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext),
Server: server,
User: user,
}, nil
@ -402,7 +402,7 @@ func GQLHandler(ctx *Context, server *Node, gql_ext *GQLExt) func(http.ResponseW
query := GQLPayload{}
json.Unmarshal(str, &query)
gql_context := ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext)
gql_context := ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext)
params := graphql.Params{
Schema: gql_context.Schema,
@ -532,7 +532,7 @@ func GQLWSHandler(ctx * Context, server *Node, gql_ext *GQLExt) func(http.Respon
}
} else if msg.Type == "subscribe" {
ctx.Log.Logf("gqlws", "SUBSCRIBE: %+v", msg.Payload)
gql_context := ctx.Extensions[GQLExtType.Hash()].Data.(*GQLExtContext)
gql_context := ctx.Extensions[Hash(GQLExtType)].Data.(*GQLExtContext)
params := graphql.Params{
Schema: gql_context.Schema,
Context: req_ctx,

@ -193,7 +193,7 @@ func GetCtx[T Extension, C any](ctx *Context) (C, error) {
var zero T
var zero_ctx C
ext_type := zero.Type()
type_hash := ext_type.Hash()
type_hash := Hash(ext_type)
ext_info, ok := ctx.Extensions[type_hash]
if ok == false {
return zero_ctx, fmt.Errorf("%s is not an extension in ctx", ext_type)
@ -228,7 +228,7 @@ func (node *Node) Serialize() ([]byte, error) {
node_db := NodeDB{
Header: NodeDBHeader{
Magic: NODE_DB_MAGIC,
TypeHash: node.Type.Hash(),
TypeHash: Hash(node.Type),
NumExtensions: uint32(len(extensions)),
NumQueuedSignals: uint32(len(node.SignalQueue)),
},
@ -244,7 +244,7 @@ func (node *Node) Serialize() ([]byte, error) {
}
node_db.Extensions[i] = ExtensionDB{
Header: ExtensionDBHeader{
TypeHash: ext_type.Hash(),
TypeHash: Hash(ext_type),
Length: uint64(len(ser)),
},
Data: ser,
@ -262,7 +262,7 @@ func NewNode(ctx *Context, id NodeID, node_type NodeType, queued_signals []Queue
panic("Attempted to create an existing node")
}
def, exists := ctx.Types[node_type.Hash()]
def, exists := ctx.Types[Hash(node_type)]
if exists == false {
panic("Node type %s not registered in Context")
}

@ -3,15 +3,11 @@ package graphvent
import (
"encoding/json"
"fmt"
"crypto/sha512"
"encoding/binary"
)
type PolicyType string
func (policy PolicyType) Hash() uint64 {
hash := sha512.Sum512([]byte(fmt.Sprintf("POLICY: %s", string(policy))))
return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)])
}
func (policy PolicyType) Prefix() string { return "POLICY: " }
func (policy PolicyType) String() string { return string(policy) }
const (
RequirementOfPolicyType = PolicyType("REQUIREMENT_OF")