Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): add more r/docs items #3987

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

leohhhn
Copy link
Contributor

@leohhhn leohhhn commented Mar 20, 2025

Description

Compiling small examples on how to use some packages in r/docs.

Adds:

  • Emiting events
  • Routing with p/demo/mux
  • Safe object examples
  • [] more

@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Mar 20, 2025
@Gno2D2 Gno2D2 requested a review from a team March 20, 2025 19:09
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Mar 20, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)
🟢 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: leohhhn/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🟢 Requirement satisfied
└── 🟢 If
    ├── 🟢 Condition
    │   └── 🟢 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🔴 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🟢 This pull request is a draft
    └── 🟢 Then
        └── 🟢 Not (🔴 This label is applied to pull request: review/triage-pending)

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link

codecov bot commented Mar 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@leohhhn leohhhn changed the title docs: add more r/docs items feat(examples): add more r/docs items Mar 20, 2025

var out string

func init() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the init just after global vars (~line 10)

Comment on lines +7 to +9
var (
slice []int
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var (
slice []int
)
var slice []int

slice []int
)

// SetSlice takes in a complex argument, which needs to be called via MsgRun
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// SetSlice takes in a complex argument, which needs to be called via MsgRun
// SetSlice takes a complex argument that must be called using MsgRun or from another contract that imports this one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, can you try to execute MsgRun complexargs.SetSlice([]int{1,2,3}), shut down your chain, and then wake it up again? I believe it may currently only work with the same memory, but after a reboot, we could encounter a problem (or not).

If possible, this example should also include a more complex setter, such as SetStruct(locallyDefinedStructType{...}). However, I'm pretty sure it will fail in maketx run if we have non-persisted objects. This needs to be tested, please.

`

out = strings.Replace(out, "\"", "`", -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try this altearntive:

out = ```
# Complex argument functions

Exposed... `MsgCall` ...
```


// SetSlice takes in a complex argument, which needs to be called via MsgRun
func SetSlice(newSlice []int) {
slice = newSlice
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sad not to return this in the render function.


func init() {
admins.Add(lc.OwnableMain.Owner()) // @leohhhn
admins.Add(mc.Addr()) // @moul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: i'll remove my mc.Addr in favor of a mc.Authz using p/moul/authz, so that i can easily manage my authorization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please start a r/devrels/config using p/moul/authz and add both your key and mine.

@@ -29,6 +43,8 @@ Explore various examples to learn more about Gno functionality and usage.
- [Official documentation](https://github.com/gnolang/gno/tree/master/docs) <!-- should be /docs with gnoweb embedding the docs/ folder. -->
`
content := ""
content += frontMatter() // XXX: try to remove this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we SHOULD remove this, since it was rollbacked.

// Entry needs to be under r/docs to be valid.
// Title and description are required.
func AddEntry(title, description string) {
if !admins.Has(std.OriginCaller()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevrealm, not origcaller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to do a registry pattern, so prevrealm is the actual docs item, and the deployer is the origcaller. Until we have sys/teams this is the way to go, I think. then we can do a team based perms to deploy to r/docs

Comment on lines +109 to +110
chronological.Set(id.String(), entry)
alphabetical.Set(entryPath, entry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep a single list, but allow for an arbitrary weight argument that we can modify. It is our responsibility to manage the weight to display items as we choose.

admins.Add(mc.Addr()) // @moul
}

const docsPrefix = "gno.land/r/docs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const docsPrefix = "gno.land/r/docs"
var docsPrefix = std.ChainDomain()+"/r/docs"

// Disallow non-code calls and non-docs realms
// This will automatically reject non-code calls as pkgpath of a user realm is ""
if !strings.HasPrefix(entryPath, docsPrefix) {
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
panic("invalid path")


// Need both title and description
if title == "" || description == "" {
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
panic("empty title or desc")

Comment on lines +4 to +16
out := "# Emitting events in Gno\n\n"

out += `Emitting events in blockchain systems is one of the ways to make off-chain life easier.
To emit an event, simply use the **Emit()** function found in the **std** package.
This function takes in string arguments as follows:
`
out += "```\nstd.Emit(\"EventName\", \"key1\", \"value1\", \"key2\", \"value2\")\n```\n"
out += "The function takes in a variadic number of key:value pairs after the event name.\n\n"
out += "Events are stored in block results, and can be listened to via the [tx-indexer](https://github.com/gnolang/tx-indexer)."

return out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
out := "# Emitting events in Gno\n\n"
out += `Emitting events in blockchain systems is one of the ways to make off-chain life easier.
To emit an event, simply use the **Emit()** function found in the **std** package.
This function takes in string arguments as follows:
`
out += "```\nstd.Emit(\"EventName\", \"key1\", \"value1\", \"key2\", \"value2\")\n```\n"
out += "The function takes in a variadic number of key:value pairs after the event name.\n\n"
out += "Events are stored in block results, and can be listened to via the [tx-indexer](https://github.com/gnolang/tx-indexer)."
return out
return ```# Emitting ...
...
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you see with @alexiscolin what's the current status of README.md embedding as a block in gnoweb? I think that you should move most of your text in the README.md, while keeping the Render empty or minimakl.

@@ -0,0 +1,95 @@
package routing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

Comment on lines +9 to +37
var (
// Initialize the router object
router = mux.NewRouter()
)

func init() {
// Pass specific path patterns and their function handlers
// The handler functions need to have a specific signature:
// func(*mux.ResponseWriter, *mux.Request)

// Below are some examples for specific path patterns and their handlers

// When no render path is passed, ie the root render
router.HandleFunc("", homeHandler)

// When a specific render path is passed
router.HandleFunc("staticpage", staticpageHanlder)

// When a render path with a variable is passed, ie `addr`
router.HandleFunc("user/{name}", profileHandler)

// When a wildcard path is passed
router.HandleFunc("wildcard/*/", wildcardHandler)
}

// Render displays the current number value, last update timestamp, and a link to call Add with 42
func Render(path string) string {
// Pass the render path to the router
return router.Render(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var (
// Initialize the router object
router = mux.NewRouter()
)
func init() {
// Pass specific path patterns and their function handlers
// The handler functions need to have a specific signature:
// func(*mux.ResponseWriter, *mux.Request)
// Below are some examples for specific path patterns and their handlers
// When no render path is passed, ie the root render
router.HandleFunc("", homeHandler)
// When a specific render path is passed
router.HandleFunc("staticpage", staticpageHanlder)
// When a render path with a variable is passed, ie `addr`
router.HandleFunc("user/{name}", profileHandler)
// When a wildcard path is passed
router.HandleFunc("wildcard/*/", wildcardHandler)
}
// Render displays the current number value, last update timestamp, and a link to call Add with 42
func Render(path string) string {
// Pass the render path to the router
return router.Render(path)
// Render displays the current number value, last update timestamp, and a link to call Add with 42
func Render(path string) string {
// Initialize the router object
router = mux.NewRouter()
// Pass specific path patterns and their function handlers
// The handler functions need to have a specific signature:
// func(*mux.ResponseWriter, *mux.Request)
// Below are some examples for specific path patterns and their handlers
// When no render path is passed, ie the root render
router.HandleFunc("", homeHandler)
// When a specific render path is passed
router.HandleFunc("staticpage", staticpageHanlder)
// When a render path with a variable is passed, ie `addr`
router.HandleFunc("user/{name}", profileHandler)
// When a wildcard path is passed
router.HandleFunc("wildcard/*/", wildcardHandler)
// Pass the render path to the router
return router.Render(path)

// This data might be privileged because only certain users
// have the edit rights, or for other reasons.
type SafeObject struct {
privilegedData string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the admin inside (configurable), so it's a "dynbamic sfae object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: No status
Status: Triage
Development

Successfully merging this pull request may close these issues.

3 participants