From 6a13619da958c18b5b907f5dc110417568c61ec2 Mon Sep 17 00:00:00 2001 From: Leon Hudak <33522493+leohhhn@users.noreply.github.com> Date: Sun, 17 Nov 2024 20:40:29 -0600 Subject: [PATCH] feat(examples): add hello_world, update `r/demo/event` (#3130) ## 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.
Contributors' checklist... - [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
--- examples/gno.land/r/demo/emit/emit.gno | 12 ++++++++++++ examples/gno.land/r/demo/emit/gno.mod | 1 + .../r/demo/{event => emit}/z1_filetest.gno | 14 +++++++------- examples/gno.land/r/demo/event/event.gno | 9 --------- examples/gno.land/r/demo/event/gno.mod | 1 - examples/gno.land/r/demo/hello_world/gno.mod | 1 + .../gno.land/r/demo/hello_world/hello.gno | 17 +++++++++++++++++ .../r/demo/hello_world/hello_test.gno | 19 +++++++++++++++++++ 8 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 examples/gno.land/r/demo/emit/emit.gno create mode 100644 examples/gno.land/r/demo/emit/gno.mod rename examples/gno.land/r/demo/{event => emit}/z1_filetest.gno (61%) delete mode 100644 examples/gno.land/r/demo/event/event.gno delete mode 100644 examples/gno.land/r/demo/event/gno.mod create mode 100644 examples/gno.land/r/demo/hello_world/gno.mod create mode 100644 examples/gno.land/r/demo/hello_world/hello.gno create mode 100644 examples/gno.land/r/demo/hello_world/hello_test.gno diff --git a/examples/gno.land/r/demo/emit/emit.gno b/examples/gno.land/r/demo/emit/emit.gno new file mode 100644 index 00000000000..a3de8f764a5 --- /dev/null +++ b/examples/gno.land/r/demo/emit/emit.gno @@ -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) +} diff --git a/examples/gno.land/r/demo/emit/gno.mod b/examples/gno.land/r/demo/emit/gno.mod new file mode 100644 index 00000000000..cf9c2b6b98e --- /dev/null +++ b/examples/gno.land/r/demo/emit/gno.mod @@ -0,0 +1 @@ +module gno.land/r/demo/emit diff --git a/examples/gno.land/r/demo/event/z1_filetest.gno b/examples/gno.land/r/demo/emit/z1_filetest.gno similarity index 61% rename from examples/gno.land/r/demo/event/z1_filetest.gno rename to examples/gno.land/r/demo/emit/z1_filetest.gno index b138aa4351c..7dcdbf8e0a3 100644 --- a/examples/gno.land/r/demo/event/z1_filetest.gno +++ b/examples/gno.land/r/demo/emit/z1_filetest.gno @@ -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" // } // ] diff --git a/examples/gno.land/r/demo/event/event.gno b/examples/gno.land/r/demo/event/event.gno deleted file mode 100644 index 9e5de540734..00000000000 --- a/examples/gno.land/r/demo/event/event.gno +++ /dev/null @@ -1,9 +0,0 @@ -package event - -import ( - "std" -) - -func Emit(value string) { - std.Emit("TAG", "key", value) -} diff --git a/examples/gno.land/r/demo/event/gno.mod b/examples/gno.land/r/demo/event/gno.mod deleted file mode 100644 index 64987d43d79..00000000000 --- a/examples/gno.land/r/demo/event/gno.mod +++ /dev/null @@ -1 +0,0 @@ -module gno.land/r/demo/event diff --git a/examples/gno.land/r/demo/hello_world/gno.mod b/examples/gno.land/r/demo/hello_world/gno.mod new file mode 100644 index 00000000000..9561cd4f077 --- /dev/null +++ b/examples/gno.land/r/demo/hello_world/gno.mod @@ -0,0 +1 @@ +module gno.land/r/demo/hello_world diff --git a/examples/gno.land/r/demo/hello_world/hello.gno b/examples/gno.land/r/demo/hello_world/hello.gno new file mode 100644 index 00000000000..312520de44d --- /dev/null +++ b/examples/gno.land/r/demo/hello_world/hello.gno @@ -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 + "!" +} diff --git a/examples/gno.land/r/demo/hello_world/hello_test.gno b/examples/gno.land/r/demo/hello_world/hello_test.gno new file mode 100644 index 00000000000..4c3d86c556a --- /dev/null +++ b/examples/gno.land/r/demo/hello_world/hello_test.gno @@ -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) + } +}