Skip to content

Commit

Permalink
feat(examples): add hello_world, update r/demo/event (#3130)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

## Description

We don't have a clean & simple hello_world realm. I also updated the doc
on the `r/demo/events` realm, and also renamed it to emit.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
</details>
  • Loading branch information
leohhhn authored Nov 18, 2024
1 parent a1a7cb3 commit 6a13619
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
12 changes: 12 additions & 0 deletions examples/gno.land/r/demo/emit/emit.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package emit demonstrates how to use the std.Emit() function
// to emit Gno events that can be used to track data changes off-chain.
// std.Emit is variadic; apart from the event name, it can take in any number of key-value pairs to emit.
package emit

import (
"std"
)

func Emit(value string) {
std.Emit("EventName", "key", value)
}
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/emit/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/emit
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package main

import "gno.land/r/demo/event"
import "gno.land/r/demo/emit"

func main() {
event.Emit("foo")
event.Emit("bar")
emit.Emit("foo")
emit.Emit("bar")
}

// Events:
// [
// {
// "type": "TAG",
// "type": "EventName",
// "attrs": [
// {
// "key": "key",
// "value": "foo"
// }
// ],
// "pkg_path": "gno.land/r/demo/event",
// "pkg_path": "gno.land/r/demo/emit",
// "func": "Emit"
// },
// {
// "type": "TAG",
// "type": "EventName",
// "attrs": [
// {
// "key": "key",
// "value": "bar"
// }
// ],
// "pkg_path": "gno.land/r/demo/event",
// "pkg_path": "gno.land/r/demo/emit",
// "func": "Emit"
// }
// ]
9 changes: 0 additions & 9 deletions examples/gno.land/r/demo/event/event.gno

This file was deleted.

1 change: 0 additions & 1 deletion examples/gno.land/r/demo/event/gno.mod

This file was deleted.

1 change: 1 addition & 0 deletions examples/gno.land/r/demo/hello_world/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/hello_world
17 changes: 17 additions & 0 deletions examples/gno.land/r/demo/hello_world/hello.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Package hello_world demonstrates the usage of the Render() function.
// Render() can be called via the vm/qrender ABCI query off-chain to
// retrieve realm state or any other custom data defined by the realm
// developer. The vm/qrender query allows for additional data to be
// passed in with the call, which can be utilized as the path argument
// to the Render() function. This allows developers to create different
// "renders" of their realms depending on the data which is passed in,
// such as pagination, admin dashboards, and more.
package hello_world

func Render(path string) string {
if path == "" {
return "# Hello, 世界!"
}

return "# Hello, " + path + "!"
}
19 changes: 19 additions & 0 deletions examples/gno.land/r/demo/hello_world/hello_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hello_world

import (
"testing"
)

func TestHello(t *testing.T) {
expected := "# Hello, 世界!"
got := Render("")
if got != expected {
t.Fatalf("Expected %s, got %s", expected, got)
}

got = Render("world")
expected = "# Hello, world!"
if got != expected {
t.Fatalf("Expected %s, got %s", expected, got)
}
}

0 comments on commit 6a13619

Please sign in to comment.