From f103cb75a50d544c7c33e8271134a11b9594180f Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 9 Apr 2024 14:16:59 +0200 Subject: [PATCH] Add register --- .../providers/profile.gno | 0 .../providers/profiles.gno | 8 ++++++ .../providers/profiles_test.gno | 0 .../r/demo/teritori/registry/registry.gno | 27 +++++++++++++++++++ .../demo/teritori/registry/registry_test.gno | 22 +++++++++++++++ .../r/demo/teritori/worx_aggregator/worx.gno | 20 ++++++++++---- 6 files changed, 72 insertions(+), 5 deletions(-) rename examples/gno.land/r/demo/teritori/{worx_aggregator => }/providers/profile.gno (100%) rename examples/gno.land/r/demo/teritori/{worx_aggregator => }/providers/profiles.gno (77%) rename examples/gno.land/r/demo/teritori/{worx_aggregator => }/providers/profiles_test.gno (100%) create mode 100644 examples/gno.land/r/demo/teritori/registry/registry.gno create mode 100644 examples/gno.land/r/demo/teritori/registry/registry_test.gno diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno b/examples/gno.land/r/demo/teritori/providers/profile.gno similarity index 100% rename from examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno rename to examples/gno.land/r/demo/teritori/providers/profile.gno diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno b/examples/gno.land/r/demo/teritori/providers/profiles.gno similarity index 77% rename from examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno rename to examples/gno.land/r/demo/teritori/providers/profiles.gno index aef780696d1..c2d302a665d 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno +++ b/examples/gno.land/r/demo/teritori/providers/profiles.gno @@ -4,10 +4,18 @@ import ( "std" "gno.land/p/demo/avl" "gno.land/p/demo/ufmt" + "gno.land/r/demo/teritori/registry" ) var profiles avl.Tree +func init() { + registry.Register("profiles_get", Get) + registry.Register("profiles_types", SupportedTypes) +} + +func myCallback(a, b string) { /* ... */ } + func Get(dataName string, addr std.Address) string { if dataName != "profile"{ panic("invalid dataname") diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno b/examples/gno.land/r/demo/teritori/providers/profiles_test.gno similarity index 100% rename from examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno rename to examples/gno.land/r/demo/teritori/providers/profiles_test.gno diff --git a/examples/gno.land/r/demo/teritori/registry/registry.gno b/examples/gno.land/r/demo/teritori/registry/registry.gno new file mode 100644 index 00000000000..114f7551cf1 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/registry/registry.gno @@ -0,0 +1,27 @@ +package registry + +import ( + "gno.land/p/demo/avl" + "std" +) + +var callbacks avl.Tree +type functionCB func(args ...interface{}) []interface{} + +func Register(id string, callback functionCB){ + _,exists:=callbacks.Get(id) + if exists{ + panic("A callback already exists for the id") + } + + callbacks.Set(id, callback) +} + +func Exec(id string, args ...interface{}){ + cb, ok:= callbacks.Get(id) + if !ok{ + panic("Callback not found") + } + function := cb.(functionCB) + function(args) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/registry/registry_test.gno b/examples/gno.land/r/demo/teritori/registry/registry_test.gno new file mode 100644 index 00000000000..bb167cd1752 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/registry/registry_test.gno @@ -0,0 +1,22 @@ +package registry + +import ( + "std" + "testing" + + "gno.land/p/demo/avl" + "gno.land/p/demo/testutils" + "strings" +) + +func TestGetProfile(t *testing.T) { + user1 := testutils.TestAddress("user1") + std.TestSetOrigCaller(user1) + functionID:="SOMEID" + var cb functionCB = func(args ...interface{})[]interface{}{ + //t.Errorf("dataType",dataType) + return nil + } + Register(functionID, cb) + Exec(functionID) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno index 2a0f39e756a..61fd09ded2a 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -2,6 +2,8 @@ package worx_aggregator import( "gno.land/p/demo/avl" + "gno.land/r/demo/teritori/registry" + "std" ) //interface WorxDataProvider { @@ -20,18 +22,21 @@ func Get(dataType string, addr std.Address) []any { if len(dataProviders) == 0 { panic("there is not dataprovider configured for that datatype") } - for d := range dataProviders { - all = append(all, d.Get(dataType, addr)...) + for registerID := range dataProviders { + all = append(all, getFromRealm(registerID,dataType, addr)...) } return all } -func RegisterDataProvider(dp WorxDataProvider) { +func RegisterDataProvider(registerRootID string) { assertAdmin() - for supp := range dp.SupportedTypes() { + listRegisterID := registerRootID + "_types" + supportedTypes := registry.Exec(listRegisterID) + for supp := range supportedTypes { + suppStr:= supp.(string) providers := dataTypeToDataProvider.Get(supp) - providers = append(providers, dp) + providers = append(providers, registerRootID) dataTypeToDataProvider.set(supp, providers) } } @@ -40,4 +45,9 @@ func assertAdmin(){ if (std.PrevRealm().Addr() != admin) { panic("unathorized") } +} + +func getFromRealm(registerRootID string,dataType string, address std.Address) []any { + getRegisterID := registerRootID + "_get" + return registry.Exec(getRegisterID, dataType, address) } \ No newline at end of file