Skip to content

Commit

Permalink
feat: In r/demo/users, add ListUsersByPrefix. See the PR.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Mar 6, 2024
1 parent e225d62 commit 19d9215
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/users/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module gno.land/r/demo/users

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/avlhelpers v0.0.0-latest
gno.land/p/demo/users v0.0.0-latest
)
7 changes: 7 additions & 0 deletions examples/gno.land/r/demo/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/avlhelpers"
"gno.land/p/demo/users"
)

Expand Down Expand Up @@ -235,6 +236,12 @@ func GetUserByAddressOrName(input users.AddressOrName) *users.User {
return GetUserByAddress(std.Address(input))
}

// Get a list of user names starting from the given prefix. Limit the
// number of results to maxResults. (This can be used for a name search tool.)
func ListUsersByPrefix(prefix string, maxResults int) []string {
return avlhelpers.ListKeysByPrefix(name2User, prefix, maxResults)
}

func Resolve(input users.AddressOrName) std.Address {
name, isName := input.GetName()
if !isName {
Expand Down
49 changes: 49 additions & 0 deletions examples/gno.land/r/demo/users/z_11_filetest.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

// SEND: 2000000000ugnot

import (
"strconv"

"gno.land/r/demo/users"
)

func main() {
users.Register("", "gnouser", "my profile")

{
// Normal usage
names := users.ListUsersByPrefix("g", 10)
println("# names: " + strconv.Itoa(len(names)))
println("name: " + names[0])
}

{
// Empty prefix: match all
names := users.ListUsersByPrefix("", 10)
println("# names: " + strconv.Itoa(len(names)))
println("name: " + names[0])
}

{
// The prefix is before "gnouser"
names := users.ListUsersByPrefix("gnouseq", 10)
println("# names: " + strconv.Itoa(len(names)))
}

{
// The prefix is after "gnouser"
names := users.ListUsersByPrefix("go", 10)
println("# names: " + strconv.Itoa(len(names)))
}

// More tests are in p/demo/avlhelpers
}

// Output:
// # names: 1
// name: gnouser
// # names: 1
// name: gnouser
// # names: 0
// # names: 0

0 comments on commit 19d9215

Please sign in to comment.