gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + gno.land: a blockchain for timeless code and fair open-source.
https://gno.land/
Other
901 stars 378 forks source link

Enhance Realm Rendering in Gno by Supporting Multiple Render Methods #1422

Open gfanton opened 11 months ago

gfanton commented 11 months ago

In the current implementation, each realm possesses a Render method that returns a Markdown representation of the realm. While there are ongoing efforts to enhance this default rendering format (as discussed in issue #439), the current method is limited in scope, supporting only this single, default rendering format.

To extend the versatility and usability of realms, I propose introducing additional rendering methods. This enhancement would allow realms to support various formats like HTML, JSON, etc., akin to how structures in Go can implement custom MarshalJSON and UnmarshalJSON methods for JSON processing.

Implementation

This proposal, while introducing versatile rendering options like RenderHTML or RenderJSON complements the ongoing improvements to the default Render method discussed in issue #439. It aims to broaden the utility and adaptability of Gno realms without conflicting with existing enhancement efforts. By offering diverse rendering capabilities alongside a reliable Markdown fallback, this enhancement seeks to cater to a wider range of use cases, from web views to specific data representation.

moul commented 11 months ago

I'm not sure about making this a default on gno.land, but it's a good and simple idea.

If we do this, we could try having RenderJSON return an interface{} and do the marshaling from the VMKeeper. This could help since we don't have the reflect package, yet. Maybe RenderObject is a better name?

For RenderHTML, I think we shouldn't use it to avoid issues with XSS and cookies.

moul commented 6 months ago

Sure, here's an improved version of your comment:


New API I'm proposing for this:

// AnyHandler can be called by Gno import and "maketx q_eval --format=json".
// Depends on #1776.
// In the future, it could also be callable via a proxy server at api.gno.land to simulate a REST API.
func AnyHandler(input MyFirstType) MySecondType {
    // Implementation here
}

// Render returns Markdown content for the given path.
func Render(path string) string {
    // Implementation here
}

// RenderHTML returns HTML content for the given path.
// The HTML is published on a special permissionless playground server, like html.gno.land.
func RenderHTML(path string) string {
    // Implementation here
}

The goal is to avoid having MIME type support in Render() and to avoid any marshalling.

grepsuzette commented 6 months ago

To extend the versatility and usability of realms, I propose introducing additional rendering methods. This enhancement would allow realms to support various formats like HTML, JSON, etc., akin to how structures in Go can implement custom MarshalJSON and UnmarshalJSON methods for JSON processing.

Since realms can already export whatever function they want, perhaps we should try and turn the question upside down: which kind of consumer/client/device can we think of, which would need a such standardized function?