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
f12da74
commit a59e9a2
Showing
7 changed files
with
131 additions
and
50 deletions.
There are no files selected for viewing
File renamed without changes.
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,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() []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) | ||
} | ||
|
||
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") | ||
} | ||
} |
File renamed without changes.
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,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) | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/gno.land/r/demo/teritori/registry/registry_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,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) | ||
} |
39 changes: 0 additions & 39 deletions
39
examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno
This file was deleted.
Oops, something went wrong.
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