-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
load extensions during compile time using go build tags
- Loading branch information
1 parent
2e5e6fb
commit c060fdb
Showing
23 changed files
with
439 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package extensions | ||
|
||
import "context" | ||
|
||
type extensionBuilder struct { | ||
extension *Extension | ||
} | ||
|
||
// ExtensionBuilder is the interface for creating an extension server | ||
type ExtensionBuilder interface { | ||
// WithMethods specifies the methods that should be provided | ||
// by the extension | ||
WithMethods(map[string]MethodFunc) ExtensionBuilder | ||
// WithInitializer is a function that initializes a new extension instance. | ||
WithInitializer(InitializeFunc) ExtensionBuilder | ||
// Named specifies the name of the extensions. | ||
Named(string) ExtensionBuilder | ||
|
||
// Build creates the extensions | ||
Build() (*Extension, error) | ||
} | ||
|
||
func Builder() ExtensionBuilder { | ||
return &extensionBuilder{ | ||
extension: &Extension{ | ||
methods: make(map[string]MethodFunc), | ||
initializeFunc: func(ctx context.Context, metadata map[string]string) (map[string]string, error) { | ||
return metadata, nil | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (b *extensionBuilder) Named(name string) ExtensionBuilder { | ||
b.extension.name = name | ||
return b | ||
} | ||
|
||
func (b *extensionBuilder) WithMethods(methods map[string]MethodFunc) ExtensionBuilder { | ||
b.extension.methods = methods | ||
return b | ||
} | ||
|
||
func (b *extensionBuilder) WithInitializer(fn InitializeFunc) ExtensionBuilder { | ||
b.extension.initializeFunc = fn | ||
return b | ||
} | ||
|
||
func (b *extensionBuilder) Build() (*Extension, error) { | ||
return b.extension, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package extensions | ||
|
||
import "strings" | ||
|
||
var registeredExtensions = make(map[string]*Extension) | ||
|
||
func RegisterExtension(name string, ext *Extension) error { | ||
name = strings.ToLower(name) | ||
if _, ok := registeredExtensions[name]; ok { | ||
panic("extension of same name already registered: " + name) | ||
} | ||
|
||
registeredExtensions[name] = ext | ||
return nil | ||
} | ||
|
||
func GetRegisteredExtensions() map[string]*Extension { | ||
return registeredExtensions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,61 @@ | ||
//go:build ext_test | ||
|
||
package extensions_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
extensions "github.com/kwilteam/kwil-db/extensions/actions" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TODO: these tests are pretty bad. | ||
// since this is a prototype, and the package is simple, this is good for now. | ||
func Test_Extensions(t *testing.T) { | ||
ctx := context.Background() | ||
ext := extensions.New("local:8080") | ||
math_ext, err := extensions.NewMathExtension() | ||
assert.NoError(t, err) | ||
|
||
err := ext.Connect(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
// Create an instance with incorrect metadata, instance should be created with default metadata: up | ||
incorrectMetadata := map[string]string{ | ||
"roundoff": "up", | ||
} | ||
instance1, err := math_ext.CreateInstance(ctx, incorrectMetadata) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, instance1) | ||
|
||
instance, err := ext.CreateInstance(ctx, map[string]string{ | ||
"token_address": "0x12345", | ||
"wallet_address": "0xabcd", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Verify that the metadata was updated with the default value "round: up" | ||
updatedMetadata := instance1.Metadata() | ||
assert.Equal(t, "up", updatedMetadata["round"]) | ||
|
||
results, err := instance.Execute(ctx, "method1", "0x12345") | ||
if err != nil { | ||
t.Fatal(err) | ||
// Create an instance with correct metadata | ||
correctMetadata := map[string]string{ | ||
"round": "down", | ||
} | ||
|
||
if len(results) != 2 { | ||
t.Fatalf("expected 2 results, got %d", len(results)) | ||
} | ||
instance2, err := math_ext.CreateInstance(ctx, correctMetadata) | ||
assert.NoError(t, err) | ||
|
||
// test that the instance has the correct name | ||
name := instance2.Name() | ||
assert.Equal(t, "math", name) | ||
|
||
// Execute an available method | ||
// Instance1: round: up | ||
result, err := instance1.Execute(ctx, "divide", 1, 2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, int64(1), result[0]) | ||
|
||
// Instance2: round: down | ||
result, err = instance2.Execute(ctx, "divide", 1, 2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, int64(0), result[0]) | ||
|
||
// Check that the methods are case insensitive | ||
result, err = instance2.Execute(ctx, "ADD", 1, 2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, int64(3), result[0]) | ||
|
||
// Execute an unavailable method | ||
_, err = instance2.Execute(ctx, "modulus", 1, 2) | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.