From b3800b7dfb864396ac74dc20390e728bc0b2d88e Mon Sep 17 00:00:00 2001 From: Leon Hudak <33522493+leohhhn@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:07:16 -0600 Subject: [PATCH] feat(examples): mirror realm (#3156) ## Description Adds the mirror realm.
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/mirror/doc.gno | 3 ++ examples/gno.land/r/demo/mirror/gno.mod | 3 ++ examples/gno.land/r/demo/mirror/mirror.gno | 33 ++++++++++++++++++++++ examples/gno.land/r/leon/home/gno.mod | 1 + examples/gno.land/r/leon/home/home.gno | 2 ++ 5 files changed, 42 insertions(+) create mode 100644 examples/gno.land/r/demo/mirror/doc.gno create mode 100644 examples/gno.land/r/demo/mirror/gno.mod create mode 100644 examples/gno.land/r/demo/mirror/mirror.gno diff --git a/examples/gno.land/r/demo/mirror/doc.gno b/examples/gno.land/r/demo/mirror/doc.gno new file mode 100644 index 00000000000..40fdbd5bc26 --- /dev/null +++ b/examples/gno.land/r/demo/mirror/doc.gno @@ -0,0 +1,3 @@ +// Package mirror demonstrates that users can pass realm functions +// as arguments to other realms. +package mirror diff --git a/examples/gno.land/r/demo/mirror/gno.mod b/examples/gno.land/r/demo/mirror/gno.mod new file mode 100644 index 00000000000..2bf27fd6916 --- /dev/null +++ b/examples/gno.land/r/demo/mirror/gno.mod @@ -0,0 +1,3 @@ +module gno.land/r/demo/mirror + +require gno.land/p/demo/avl v0.0.0-latest diff --git a/examples/gno.land/r/demo/mirror/mirror.gno b/examples/gno.land/r/demo/mirror/mirror.gno new file mode 100644 index 00000000000..770fddc4fda --- /dev/null +++ b/examples/gno.land/r/demo/mirror/mirror.gno @@ -0,0 +1,33 @@ +package mirror + +import ( + "gno.land/p/demo/avl" +) + +var store avl.Tree + +func Register(pkgpath string, rndr func(string) string) { + if store.Has(pkgpath) { + return + } + + if rndr == nil { + return + } + + store.Set(pkgpath, rndr) +} + +func Render(path string) string { + if raw, ok := store.Get(path); ok { + return raw.(func(string) string)("") + } + + if store.Size() == 0 { + return "None are fair." + } + + return "Mirror, mirror on the wall, which realm's the fairest of them all?" +} + +// Credits to @jeronimoalbi diff --git a/examples/gno.land/r/leon/home/gno.mod b/examples/gno.land/r/leon/home/gno.mod index 4649cf4abe6..7288c176050 100644 --- a/examples/gno.land/r/leon/home/gno.mod +++ b/examples/gno.land/r/leon/home/gno.mod @@ -5,5 +5,6 @@ require ( gno.land/r/demo/art/gnoface v0.0.0-latest gno.land/r/demo/art/millipede v0.0.0-latest gno.land/r/demo/hof v0.0.0-latest + gno.land/r/demo/mirror v0.0.0-latest gno.land/r/leon/config v0.0.0-latest ) diff --git a/examples/gno.land/r/leon/home/home.gno b/examples/gno.land/r/leon/home/home.gno index aea8b43e9cd..632b3f14a62 100644 --- a/examples/gno.land/r/leon/home/home.gno +++ b/examples/gno.land/r/leon/home/home.gno @@ -9,6 +9,7 @@ import ( "gno.land/r/demo/art/gnoface" "gno.land/r/demo/art/millipede" "gno.land/r/demo/hof" + "gno.land/r/demo/mirror" "gno.land/r/leon/config" ) @@ -34,6 +35,7 @@ TODO import r/gh } hof.Register() + mirror.Register(std.CurrentRealm().PkgPath(), Render) } func UpdatePFP(url, caption string) {