Skip to content

Commit

Permalink
add Profile DataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Apr 8, 2024
1 parent 6760265 commit f12da74
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
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)
}
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)
}
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 examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
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")
}
}

0 comments on commit f12da74

Please sign in to comment.