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
be6f420
commit 5c24dd8
Showing
13 changed files
with
235 additions
and
63 deletions.
There are no files selected for viewing
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 @@ | ||
module gno.land/p/demo/teritori/worx |
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 worx | ||
|
||
import "std" | ||
|
||
|
||
type Worx struct { | ||
Hours int | ||
Metadata string | ||
Address std.Address | ||
Points int | ||
Timestamp int64 | ||
} | ||
|
||
func NewWorx(hours int, metadata string, addr std.Address, points int, timestamp int64) *Worx{ | ||
return &Worx{ | ||
Hours: hours, | ||
Metadata: metadata, | ||
Address: addr, | ||
Points: points, | ||
Timestamp: timestamp, | ||
} | ||
} |
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,13 @@ | ||
package worx | ||
|
||
type WorxKeeper struct { | ||
worxs []*Worx | ||
} | ||
|
||
func (keeper *WorxKeeper) Store(worx *Worx) { | ||
keeper.worxs = append(keeper.worxs, worx) | ||
} | ||
|
||
func (keeper *WorxKeeper) Get() []*Worx { | ||
return keeper.worxs | ||
} |
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,6 @@ | ||
module gno.land/r/demo/teritori/social_follow | ||
|
||
require ( | ||
gno.land/p/demo/avl v0.0.0-latest | ||
gno.land/p/demo/testutils v0.0.0-latest | ||
) |
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() interface{}{ | ||
return []interface{}{"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") | ||
} | ||
} |
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
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,18 @@ | ||
package provider | ||
|
||
import ( | ||
"std" | ||
"gno.land/r/demo/teritori/worx_aggregator" | ||
"gno.land/p/demo/rand" | ||
) | ||
|
||
var admin std.Address | ||
|
||
func init() { | ||
admin = std.GetOrigCaller() | ||
} | ||
|
||
func RandWorx(addr std.Address){ | ||
r := rand.New() | ||
worx_aggregator.Push(r.Intn(25), "", addr, r.Intn(100), std.GetHeight()) | ||
} |
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
16 changes: 16 additions & 0 deletions
16
examples/gno.land/r/demo/teritori/worx_aggregator/worx_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,16 @@ | ||
package worx_aggregator | ||
|
||
import ( | ||
"testing" | ||
"gno.land/p/demo/testutils" | ||
) | ||
|
||
func TestPushCallerNotRegistered(t * testing.T){ | ||
user1 := testutils.TestAddress("user1") | ||
defer func() { | ||
if v := recover(); v == nil { | ||
t.Fatalf("expected panic got no error: ") | ||
} | ||
}() | ||
Push(1, "", user1, 3, 10) | ||
} |