From f1aef0e8d26d6027ad4994712b6c43945583331e Mon Sep 17 00:00:00 2001 From: Manish Meganathan Date: Mon, 25 Sep 2023 08:53:22 +0530 Subject: [PATCH] simplify runtime registry - removed the ability to register the crypto driver along with the engine runtime - refactored FetchEngineRuntime to FetchRuntime for consistency --- engine.go | 21 +++++++++++++++++ registry_test.go => engine_test.go | 14 +++++------ manifest.go | 8 +++---- registry.go | 38 ------------------------------ 4 files changed, 32 insertions(+), 49 deletions(-) rename registry_test.go => engine_test.go (91%) delete mode 100644 registry.go diff --git a/engine.go b/engine.go index a2e4e4f..1450641 100644 --- a/engine.go +++ b/engine.go @@ -79,3 +79,24 @@ type EngineRuntime interface { // ErrorResult that is used by the engine runtime DecodeErrorResult([]byte) (ErrorResult, error) } + +// registry is an in-memory registry of supported EngineRuntime instances. +// Support for different engine runtimes is only available if they are first registered with this package. +var registry = map[EngineKind]EngineRuntime{} + +// RegisterRuntime registers an EngineRuntime with the package. +// If a runtime instance already exists for the EngineKind, it is overwritten. +func RegisterRuntime(runtime EngineRuntime) { + registry[runtime.Kind()] = runtime +} + +// FetchRuntime retrieves an EngineRuntime for a given EngineKind. +// If the runtime for the engine kind is not registered, returns false. +func FetchRuntime(kind EngineKind) (EngineRuntime, bool) { + runtime, exists := registry[kind] + if !exists { + return nil, false + } + + return runtime, true +} diff --git a/registry_test.go b/engine_test.go similarity index 91% rename from registry_test.go rename to engine_test.go index 1e45a60..81b0431 100644 --- a/registry_test.go +++ b/engine_test.go @@ -9,19 +9,19 @@ import ( func TestRegisterEngineRuntime(t *testing.T) { // register a mock engine runtime mock := &mockEngineRuntime{kind: PISA} - RegisterRuntime(mock, nil) + RegisterRuntime(mock) // verify that the engine runtime was registered correctly - r, ok := FetchEngineRuntime(PISA) + r, ok := FetchRuntime(PISA) require.True(t, ok, "Expected engine runtime to be registered") require.Equal(t, r.Kind(), PISA, "Expected registered engine runtime to have kind %s, but got %s", PISA, r.Kind()) // overwrite the mock engine runtime with a new one mock2 := &mockEngineRuntime{kind: PISA} - RegisterRuntime(mock2, nil) + RegisterRuntime(mock2) // verify that the new engine runtime was registered correctly - r, ok = FetchEngineRuntime(PISA) + r, ok = FetchRuntime(PISA) require.True(t, ok, "Expected engine runtime to be registered") require.Equal(t, r.Kind(), PISA, "Expected registered engine runtime to have kind %s, but got %s", PISA, r.Kind()) @@ -30,15 +30,15 @@ func TestRegisterEngineRuntime(t *testing.T) { func TestFetchEngineRuntime(t *testing.T) { // fetch an unregistered engine runtime - _, ok := FetchEngineRuntime(MERU) + _, ok := FetchRuntime(MERU) require.False(t, ok, "Expected engine runtime not to be registered") // register a mock engine runtime mock := &mockEngineRuntime{kind: MERU} - RegisterRuntime(mock, nil) + RegisterRuntime(mock) // fetch the registered engine runtime - r, ok := FetchEngineRuntime(MERU) + r, ok := FetchRuntime(MERU) require.True(t, ok, "Expected engine runtime to be registered") require.Equal(t, r.Kind(), MERU, "Expected registered engine runtime to have kind %s, but got %s", MERU, r.Kind()) } diff --git a/manifest.go b/manifest.go index f52ba42..4b0f10d 100644 --- a/manifest.go +++ b/manifest.go @@ -178,7 +178,7 @@ func (header ManifestHeader) validate() error { return errors.New("unsupported manifest syntax") } - if _, ok := FetchEngineRuntime(header.LogicEngine()); !ok { + if _, ok := FetchRuntime(header.LogicEngine()); !ok { return errors.New("unsupported manifest engine: element registry not found") } @@ -209,7 +209,7 @@ func (manifest *Manifest) Depolorize(depolorizer *polo.Depolorizer) (err error) return err } - runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine()) + runtime, _ := FetchRuntime(manifest.Header().LogicEngine()) manifest.Elements = make([]ManifestElement, 0, len(raw.Elements)) @@ -264,7 +264,7 @@ func (manifest *Manifest) UnmarshalJSON(data []byte) (err error) { return err } - runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine()) + runtime, _ := FetchRuntime(manifest.Header().LogicEngine()) manifest.Elements = make([]ManifestElement, 0, len(raw.Elements)) @@ -314,7 +314,7 @@ func (manifest *Manifest) UnmarshalYAML(node *yaml.Node) error { return err } - runtime, _ := FetchEngineRuntime(manifest.Header().LogicEngine()) + runtime, _ := FetchRuntime(manifest.Header().LogicEngine()) manifest.Elements = make([]ManifestElement, 0, len(raw.Elements)) diff --git a/registry.go b/registry.go deleted file mode 100644 index ac5a866..0000000 --- a/registry.go +++ /dev/null @@ -1,38 +0,0 @@ -package engineio - -// registry is an in-memory registry of EngineRuntime instances and their respective cryptography drivers. -// Support for different engine runtimes is only available if they are first registered with this package. -var registry = map[EngineKind]entry{} - -type entry struct { - runtime EngineRuntime - crypto CryptographyDriver -} - -// RegisterRuntime registers an EngineRuntime with the package along with a CryptographyDriver for the runtime. -// If a runtime instance already exists for the EngineKind, it is overwritten. -func RegisterRuntime(runtime EngineRuntime, crypto CryptographyDriver) { - registry[runtime.Kind()] = entry{runtime, crypto} -} - -// FetchEngineRuntime retrieves an EngineRuntime for a given EngineKind. -// If the runtime for the engine kind is not registered, returns false. -func FetchEngineRuntime(kind EngineKind) (EngineRuntime, bool) { - object, exists := registry[kind] - if !exists { - return nil, false - } - - return object.runtime, true -} - -// FetchCryptographyDriver retrieves an CryptoDriver for a given EngineKind. -// If the runtime for the engine kind is not registered, returns false. -func FetchCryptographyDriver(kind EngineKind) (CryptographyDriver, bool) { - object, exists := registry[kind] - if !exists { - return nil, false - } - - return object.crypto, true -}