diff --git a/context.go b/context.go index cf1ab08..26a4e87 100644 --- a/context.go +++ b/context.go @@ -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) diff --git a/gql.go b/gql.go index d9150d5..ae1874d 100644 --- a/gql.go +++ b/gql.go @@ -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, diff --git a/node.go b/node.go index 4d9a0d0..3100981 100644 --- a/node.go +++ b/node.go @@ -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") } diff --git a/policy.go b/policy.go index 3d3d544..91bc685 100644 --- a/policy.go +++ b/policy.go @@ -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")