From 1c9700378c2ea2af6f586b612195ab6ad46dd877 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 14 Mar 2024 02:36:39 -0400 Subject: [PATCH] Moved key/hash stuff into its own file --- entity/key.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ entity/toplevel.go | 65 ------------------------------------------ 2 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 entity/key.go diff --git a/entity/key.go b/entity/key.go new file mode 100644 index 0000000..1e1ec58 --- /dev/null +++ b/entity/key.go @@ -0,0 +1,70 @@ +package entity + +import "fmt" +import "crypto/md5" +import "encoding/base64" +import "github.com/google/uuid" + +// A hash represents a hash sum that fits within a uint64. +type Hash [8]byte + +// NewHash creates a new truncated md5 hash using the specified data. +func NewHash (data []byte) Hash { + sum := md5.Sum(data) + hash := Hash { } + copy(hash[:], sum[:8]) + return hash +} + +// Number converts the hash into a uint64. +func (hash Hash) Number () uint64 { + var number uint64 + for _, part := range hash { + number <<= 8 + number |= uint64(part) + } + return number +} + +// Key globally indexes top level entities in contexts where modules matter. +type Key struct { + Unit uuid.UUID + Name string + Method string +} + +func (key Key) String () string { + out := fmt.Sprintf("%v::%v", key.Unit, key.Name) + if key.Method != "" { + out = fmt.Sprintf("%s.%s", out, key.Method) + } + return out +} + +// Hash returns a representation of the hash of this key that fits within a +// uint64. +func (key Key) Hash () Hash { + return NewHash([]byte("Key:" + key.String())) +} + +// LinkName returns the name that the entity it refers to will be given when +// compiled. +func (key Key) LinkName () string { + data := [16]byte(key.Unit) + out := fmt.Sprintf( + "%s::%s", + base64.StdEncoding.EncodeToString(data[:]), + key.Name) + if key.Method != "" { + out = fmt.Sprintf("%s.%s", out, key.Method) + } + return out +} + +// StripMethod returns a copy of the key that refers to a type instead of a +// method. +func (key Key) StripMethod () Key { + key.Method = "" + return key +} + diff --git a/entity/toplevel.go b/entity/toplevel.go index ba45d6f..52e30d4 100644 --- a/entity/toplevel.go +++ b/entity/toplevel.go @@ -1,74 +1,9 @@ package entity import "fmt" -import "crypto/md5" -import "encoding/base64" import "github.com/google/uuid" import "git.tebibyte.media/fspl/fspl/errors" -// A hash represents a hash sum that fits within a uint64. -type Hash [8]byte - -// NewHash creates a new truncated md5 hash using the specified data. -func NewHash (data []byte) Hash { - sum := md5.Sum(data) - hash := Hash { } - copy(hash[:], sum[:8]) - return hash -} - -// Number converts the hash into a uint64. -func (hash Hash) Number () uint64 { - var number uint64 - for _, part := range hash { - number <<= 8 - number |= uint64(part) - } - return number -} - -// Key globally indexes top level entities in contexts where modules matter. -type Key struct { - Unit uuid.UUID - Name string - Method string -} - -func (key Key) String () string { - out := fmt.Sprintf("%v::%v", key.Unit, key.Name) - if key.Method != "" { - out = fmt.Sprintf("%s.%s", out, key.Method) - } - return out -} - -// Hash returns a representation of the hash of this key that fits within a -// uint64. -func (key Key) Hash () Hash { - return NewHash([]byte("Key:" + key.String())) -} - -// LinkName returns the name that the entity it refers to will be given when -// compiled. -func (key Key) LinkName () string { - data := [16]byte(key.Unit) - out := fmt.Sprintf( - "%s::%s", - base64.StdEncoding.EncodeToString(data[:]), - key.Name) - if key.Method != "" { - out = fmt.Sprintf("%s.%s", out, key.Method) - } - return out -} - -// StripMethod returns a copy of the key that refers to a type instead of a -// method. -func (key Key) StripMethod () Key { - key.Method = "" - return key -} - // TopLevel is any construct that is placed at the root of a file. type TopLevel interface { fmt.Stringer