From f12da742f9226f98c785ffa8d02226ec536117d0 Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Mon, 8 Apr 2024 23:23:48 +0200 Subject: [PATCH 1/2] add Profile DataProvider --- .../worx_aggregator/providers/profile.gno | 23 ++++++++++ .../worx_aggregator/providers/profiles.gno | 39 +++++++++++++++++ .../providers/profiles_test.gno | 24 +++++++++++ .../r/demo/teritori/worx_aggregator/worx.gno | 43 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno new file mode 100644 index 00000000000..4849407e7d6 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno @@ -0,0 +1,23 @@ +package provider + +import ( + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" +) + +type Profile struct{ + data avl.Tree +} + +func(p *Profile) ToString() string{ + var data = "" + p.data.Iterate("", "", func(key string, value interface{}) bool { + data += ufmt.Sprintf("%s: %s\n",key, value) + return false + }) + return data +} + +func(p *Profile) SetField(fieldName string, value string) { + p.data.Set(fieldName, value) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno new file mode 100644 index 00000000000..aef780696d1 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno @@ -0,0 +1,39 @@ +package provider + +import ( + "std" + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" +) + +var profiles avl.Tree + +func Get(dataName string, addr std.Address) string { + if dataName != "profile"{ + panic("invalid dataname") + } + profile:=getProfile(addr) + + return profile.ToString() +} + +func SupportedTypes() []string{ + return []string{"profile"} +} + +func UpsertProfile(field string, value string){ + caller := std.GetOrigCaller() + profile := getProfile(caller) + profile.SetField(field, value) + profiles.Set(caller.String(), profile) +} + + +func getProfile(addr std.Address ) *Profile { + profile, found:=profiles.Get(addr.String()) + if !found{ + return &Profile{} + } + + return profile.(*Profile) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno new file mode 100644 index 00000000000..b3c02e9f1ec --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno @@ -0,0 +1,24 @@ +package provider + +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) + UpsertProfile("firstname", "John") + UpsertProfile("name", "Gopher") + + result:=Get("profile",user1) + t.Log(result) + if !strings.Contains(result,"name: Gopher"){ + t.Error("Bad") + } + +} \ 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 new file mode 100644 index 00000000000..2a0f39e756a --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -0,0 +1,43 @@ +package worx_aggregator + +import( + "gno.land/p/demo/avl" +) + +//interface WorxDataProvider { +// Get(dataName string, addr std.Address) any +// SupportedTypes() []string +//} + +var admin std.Address +var dataProviders []WorxDataProvider +var dataTypeToDataProvider avl.Tree + + +func Get(dataType string, addr std.Address) []any { + all := []any{} + dataProviders := dataTypeToDataProvider.Get(dataType) + if len(dataProviders) == 0 { + panic("there is not dataprovider configured for that datatype") + } + for d := range dataProviders { + all = append(all, d.Get(dataType, addr)...) + } + return all +} + + +func RegisterDataProvider(dp WorxDataProvider) { + assertAdmin() + for supp := range dp.SupportedTypes() { + providers := dataTypeToDataProvider.Get(supp) + providers = append(providers, dp) + dataTypeToDataProvider.set(supp, providers) + } +} + +func assertAdmin(){ + if (std.PrevRealm().Addr() != admin) { + panic("unathorized") + } +} \ No newline at end of file From 4ddacf2dc1ec2f60b701c29f5bc402953ca7bbcf Mon Sep 17 00:00:00 2001 From: Miguel Victoria Date: Tue, 9 Apr 2024 14:16:59 +0200 Subject: [PATCH 2/2] Add register --- .../providers/profile.gno | 0 .../r/demo/teritori/providers/profiles.gno | 66 +++++++++++++++++++ .../providers/profiles_test.gno | 11 +++- .../r/demo/teritori/registry/registry.gno | 27 ++++++++ .../demo/teritori/registry/registry_test.gno | 22 +++++++ .../worx_aggregator/providers/profiles.gno | 39 ----------- .../r/demo/teritori/worx_aggregator/worx.gno | 27 ++++---- 7 files changed, 141 insertions(+), 51 deletions(-) rename examples/gno.land/r/demo/teritori/{worx_aggregator => }/providers/profile.gno (100%) create mode 100644 examples/gno.land/r/demo/teritori/providers/profiles.gno rename examples/gno.land/r/demo/teritori/{worx_aggregator => }/providers/profiles_test.gno (62%) 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 delete mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.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/providers/profiles.gno b/examples/gno.land/r/demo/teritori/providers/profiles.gno new file mode 100644 index 00000000000..17cf28cd0aa --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/profiles.gno @@ -0,0 +1,66 @@ +package provider + +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", RegisterHandler) +} + + +func Get(dataName string, addr std.Address) interface{} { + if dataName != "profile"{ + panic("invalid dataname") + } + profile:=getProfile(addr) + + return profile.ToString() +} + +func SupportedTypes() interface{}{ + return []interface{}{"profile"} +} + +func UpsertProfile(field string, value string){ + caller := std.GetOrigCaller() + profile := getProfile(caller) + profile.SetField(field, value) + profiles.Set(caller.String(), profile) +} + + +func getProfile(addr std.Address ) *Profile { + profile, found:=profiles.Get(addr.String()) + if !found{ + return &Profile{} + } + + return profile.(*Profile) +} + +func RegisterHandler(functionName string, args ...interface{}) interface{} { + switch functionName { + case "get": + if len(args) != 2{ + panic("invalid number of arguments") + } + dataname := args[0].(string) + address := args[1].(std.Address) + return Get(dataname,address) + case "supportedTypes": + if len(args) != 0{ + panic("invalid number of arguments") + } + dataname := args[0].(string) + address := args[1].(std.Address) + return SupportedTypes() + default: + panic("invalid function name") + } +} \ No newline at end of file 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 62% 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 index b3c02e9f1ec..b4e44e5f5c6 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno +++ b/examples/gno.land/r/demo/teritori/providers/profiles_test.gno @@ -20,5 +20,14 @@ func TestGetProfile(t *testing.T) { if !strings.Contains(result,"name: Gopher"){ t.Error("Bad") } +} + +func TestRegisterHandler(t *testing.T) { + user1 := testutils.TestAddress("user1") + std.TestSetOrigCaller(user1) + supportedTypes := RegisterHandler("supportedTypes") + if len(supportedTypes) != 1{ + t.Error("Supported types is wrong") + } +} -} \ No newline at end of file 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..faa6175fd9a --- /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(functionName string, 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, functionName string, args ...interface{}) interface{} { + cb, ok:= callbacks.Get(id) + if !ok{ + panic("Callback not found") + } + function := cb.(FunctionCB) + return function(functionName, 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/providers/profiles.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno deleted file mode 100644 index aef780696d1..00000000000 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno +++ /dev/null @@ -1,39 +0,0 @@ -package provider - -import ( - "std" - "gno.land/p/demo/avl" - "gno.land/p/demo/ufmt" -) - -var profiles avl.Tree - -func Get(dataName string, addr std.Address) string { - if dataName != "profile"{ - panic("invalid dataname") - } - profile:=getProfile(addr) - - return profile.ToString() -} - -func SupportedTypes() []string{ - return []string{"profile"} -} - -func UpsertProfile(field string, value string){ - caller := std.GetOrigCaller() - profile := getProfile(caller) - profile.SetField(field, value) - profiles.Set(caller.String(), profile) -} - - -func getProfile(addr std.Address ) *Profile { - profile, found:=profiles.Get(addr.String()) - if !found{ - return &Profile{} - } - - return profile.(*Profile) -} \ 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..adf9f70e829 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -2,13 +2,9 @@ package worx_aggregator import( "gno.land/p/demo/avl" + "gno.land/r/demo/teritori/registry" + "std" ) - -//interface WorxDataProvider { -// Get(dataName string, addr std.Address) any -// SupportedTypes() []string -//} - var admin std.Address var dataProviders []WorxDataProvider var dataTypeToDataProvider avl.Tree @@ -20,18 +16,22 @@ 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() { + supportedTypes := registry.Exec(registerRootID, "supportedTypes") + + supportedTypesSlice := supportedTypes.[[]interface{}] + for supp := range supportedTypesSlice { + suppStr:= supp.(string) providers := dataTypeToDataProvider.Get(supp) - providers = append(providers, dp) + providers = append(providers, registerRootID) dataTypeToDataProvider.set(supp, providers) } } @@ -40,4 +40,9 @@ func assertAdmin(){ if (std.PrevRealm().Addr() != admin) { panic("unathorized") } +} + +func getFromRealm(registerRootID string,dataType string, address std.Address) []any { + getRegisterID := registerRootID + return registry.Exec(getRegisterID, "get", dataType, address) } \ No newline at end of file