Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

hello.gno

1.38 Kb · 59 lines
 1// Package hello is a canary realm: it proves the end-to-end pipeline (workspace
 2// resolution of a versioned local dependency, gno test, gno lint, and CI)
 3// works from the very first commit. It has no external dependencies. Remove it
 4// once real realms land.
 5package hello
 6
 7import (
 8	"strings"
 9
10	"gno.land/p/moul/greet/v0"
11)
12
13// greets counts how many times Greet has been called (persistent state).
14var greets int
15
16// Greet records a greeting from the caller and returns it. Crossing function:
17// it mutates persistent state, so it takes `cur realm` per the gno 0.9
18// interrealm convention.
19func Greet(cur realm, name string) string {
20	greets++
21	return greet.Greet(name)
22}
23
24// Count returns how many greetings have been recorded. Read-only.
25func Count() int { return greets }
26
27// Render is the gnoweb Markdown view.
28func Render(path string) string {
29	var b strings.Builder
30	b.WriteString("# r/moul/hello/v0\n\n")
31	b.WriteString("Canary realm for the gno-contracts repository.\n\n")
32	b.WriteString("- Greetings recorded: ")
33	b.WriteString(itoa(greets))
34	b.WriteString("\n")
35	return b.String()
36}
37
38// itoa avoids importing strconv for a single conversion in the canary.
39func itoa(n int) string {
40	if n == 0 {
41		return "0"
42	}
43	neg := n < 0
44	if neg {
45		n = -n
46	}
47	var buf [20]byte
48	i := len(buf)
49	for n > 0 {
50		i--
51		buf[i] = byte('0' + n%10)
52		n /= 10
53	}
54	if neg {
55		i--
56		buf[i] = '-'
57	}
58	return string(buf[i:])
59}