Skip to content

Commit

Permalink
feat: In r/demo/users, add ListUsersByPrefix
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Mar 1, 2024
1 parent d32b583 commit 516a99d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/gno.land/r/demo/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ 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 {
result := []string{}
name2User.Iterate(prefix, "", func(key string, value interface{}) bool {
if len(result) >= maxResults {
return true
}
result = append(result, key)
return false
})
return result
}

func Resolve(input users.AddressOrName) std.Address {
name, isName := input.GetName()
if !isName {
Expand Down
38 changes: 38 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,38 @@
package main

// SEND: 2000000000ugnot

import (
"strconv"

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

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

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

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

{
// Reach maxResults
names := users.ListUsersByPrefix("a", 0)
println("# names: " + strconv.Itoa(len(names)))
}
}

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

0 comments on commit 516a99d

Please sign in to comment.