1+ package provider
2+
3+ import (
4+ "std"
5+ "gno.land/p/demo/avl"
6+ "gno.land/p/demo/ufmt"
7+ "gno.land/r/demo/teritori/registry"
8+ )
9+
10+ var profiles avl.Tree
11+
12+ func init() {
13+ registry.Register("profiles", RegisterHandler)
14+ }
15+
16+
17+ func Get(dataName string, addr std.Address) interface{} {
18+ if dataName != "profile"{
19+ panic("invalid dataname")
20+ }
21+ profile:=getProfile(addr)
22+
23+ return profile.ToString()
24+ }
25+
26+ func SupportedTypes() interface{}{
27+ return []interface{}{"profile"}
28+ }
29+
30+ func UpsertProfile(field string, value string){
31+ caller := std.GetOrigCaller()
32+ profile := getProfile(caller)
33+ profile.SetField(field, value)
34+ profiles.Set(caller.String(), profile)
35+ }
36+
37+
38+ func getProfile(addr std.Address ) *Profile {
39+ profile, found:=profiles.Get(addr.String())
40+ if !found{
41+ return &Profile{}
42+ }
43+
44+ return profile.(*Profile)
45+ }
46+
47+ func RegisterHandler(functionName string, args ...interface{}) interface{} {
48+ switch functionName {
49+ case "get":
50+ if len(args) != 2{
51+ panic("invalid number of arguments")
52+ }
53+ dataname := args[0].(string)
54+ address := args[1].(std.Address)
55+ return Get(dataname,address)
56+ case "supportedTypes":
57+ if len(args) != 0{
58+ panic("invalid number of arguments")
59+ }
60+ dataname := args[0].(string)
61+ address := args[1].(std.Address)
62+ return SupportedTypes()
63+ default:
64+ panic("invalid function name")
65+ }
66+ }
0 commit comments