Skip to content

Commit 85cebe0

Browse files
committed
add Profile DataProvider
1 parent dc1474c commit 85cebe0

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package provider
2+
3+
import (
4+
"gno.land/p/demo/avl"
5+
"gno.land/p/demo/ufmt"
6+
)
7+
8+
type Profile struct{
9+
data avl.Tree
10+
}
11+
12+
func(p *Profile) ToString() string{
13+
var data = ""
14+
p.data.Iterate("", "", func(key string, value interface{}) bool {
15+
data += ufmt.Sprintf("%s: %s\n",key, value)
16+
return false
17+
})
18+
return data
19+
}
20+
21+
func(p *Profile) SetField(fieldName string, value string) {
22+
p.data.Set(fieldName, value)
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package provider
2+
3+
import (
4+
"std"
5+
"gno.land/p/demo/avl"
6+
"gno.land/p/demo/ufmt"
7+
)
8+
9+
var profiles avl.Tree
10+
11+
func Get(dataName string, addr std.Address) string {
12+
if dataName != "profile"{
13+
panic("invalid dataname")
14+
}
15+
profile:=getProfile(addr)
16+
17+
return profile.ToString()
18+
}
19+
20+
func SupportedTypes() []string{
21+
return []string{"profile"}
22+
}
23+
24+
func UpsertProfile(field string, value string){
25+
caller := std.GetOrigCaller()
26+
profile := getProfile(caller)
27+
profile.SetField(field, value)
28+
profiles.Set(caller.String(), profile)
29+
}
30+
31+
32+
func getProfile(addr std.Address ) *Profile {
33+
profile, found:=profiles.Get(addr.String())
34+
if !found{
35+
return &Profile{}
36+
}
37+
38+
return profile.(*Profile)
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package provider
2+
3+
import (
4+
"std"
5+
"testing"
6+
7+
"gno.land/p/demo/avl"
8+
"gno.land/p/demo/testutils"
9+
"strings"
10+
)
11+
12+
func TestGetProfile(t *testing.T) {
13+
user1 := testutils.TestAddress("user1")
14+
std.TestSetOrigCaller(user1)
15+
UpsertProfile("firstname", "John")
16+
UpsertProfile("name", "Gopher")
17+
18+
result:=Get("profile",user1)
19+
t.Log(result)
20+
if !strings.Contains(result,"name: Gopher"){
21+
t.Error("Bad")
22+
}
23+
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package worx_aggregator
2+
3+
import(
4+
"gno.land/p/demo/avl"
5+
)
6+
7+
//interface WorxDataProvider {
8+
// Get(dataName string, addr std.Address) any
9+
// SupportedTypes() []string
10+
//}
11+
12+
var admin std.Address
13+
var dataProviders []WorxDataProvider
14+
var dataTypeToDataProvider avl.Tree
15+
16+
17+
func Get(dataType string, addr std.Address) []any {
18+
all := []any{}
19+
dataProviders := dataTypeToDataProvider.Get(dataType)
20+
if len(dataProviders) == 0 {
21+
panic("there is not dataprovider configured for that datatype")
22+
}
23+
for d := range dataProviders {
24+
all = append(all, d.Get(dataType, addr)...)
25+
}
26+
return all
27+
}
28+
29+
30+
func RegisterDataProvider(dp WorxDataProvider) {
31+
assertAdmin()
32+
for supp := range dp.SupportedTypes() {
33+
providers := dataTypeToDataProvider.Get(supp)
34+
providers = append(providers, dp)
35+
dataTypeToDataProvider.set(supp, providers)
36+
}
37+
}
38+
39+
func assertAdmin(){
40+
if (std.PrevRealm().Addr() != admin) {
41+
panic("unathorized")
42+
}
43+
}

0 commit comments

Comments
 (0)