forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6760265
commit f12da74
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno
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,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) | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno
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,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) | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno
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,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") | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
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,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") | ||
} | ||
} |