Playground
doc.gno
// v0 - Unaudited: This is an initial version that has not yet been formally audited. // A fully audited version will be published as a subsequent release. // Use in production at your own risk. // // Package fqname provides utilities for handling fully qualified identifiers // in Gno, typically a package path followed by a dot and a symbol name. package fqname
fqname.gno
// Package fqname provides utilities for handling fully qualified identifiers in
// Gno. A fully qualified identifier typically includes a package path followed
// by a dot (.) and then the name of a variable, function, type, or other
// package-level declaration.
package fqname
import (
"strings"
)
// Parse splits a fully qualified identifier into its package path and name
// components. It handles cases with and without slashes in the package path.
//
// pkgpath, name := fqname.Parse("gno.land/p/nt/avl/v0.Tree")
// ufmt.Sprintf("Package: %s, Name: %s\n", id.Package, id.Name)
// // Output: Package: gno.land/p/nt/avl/v0, Name: Tree
func Parse(fqname string) (pkgpath, name string) {
// Find the index of the last slash.
lastSlashIndex := strings.LastIndex(fqname, "/")
if lastSlashIndex == -1 {
// No slash found, handle it as a simple package name with dot notation.
dotIndex := strings.LastIndex(fqname, ".")
if dotIndex == -1 {
return fqname, ""
}
return fqname[:dotIndex], fqname[dotIndex+1:]
}
// Get the part after the last slash.
afterSlash := fqname[lastSlashIndex+1:]
// Check for a dot in the substring after the last slash.
dotIndex := strings.Index(afterSlash, ".")
if dotIndex == -1 {
// No dot found after the last slash
return fqname, ""
}
// Split at the dot to separate the base and the suffix.
base := fqname[:lastSlashIndex+1+dotIndex]
suffix := afterSlash[dotIndex+1:]
return base, suffix
}
// Construct a qualified identifier.
//
// fqName := fqname.Construct("gno.land/r/demo/foo20", "Token")
// fmt.Println("Fully Qualified Name:", fqName)
// // Output: gno.land/r/demo/foo20.Token
func Construct(pkgpath, name string) string {
// TODO: ensure pkgpath is valid - and as such last part does not contain a dot.
if name == "" {
return pkgpath
}
return pkgpath + "." + name
}
// RenderLink creates a formatted link for a fully qualified identifier.
// If the package path starts with "gno.land", it converts it to a markdown link.
// If the domain is different or missing, it returns the input as is.
func RenderLink(pkgPath, slug string) string {
if strings.HasPrefix(pkgPath, "gno.land") {
pkgLink := strings.TrimPrefix(pkgPath, "gno.land")
if slug != "" {
safeSlug := escapeMarkdown(slug)
return "[" + pkgPath + "](" + pkgLink + ")." + safeSlug
}
return "[" + pkgPath + "](" + pkgLink + ")"
}
if slug != "" {
safeSlug := escapeMarkdown(slug)
return pkgPath + "." + safeSlug
}
return pkgPath
}
// escapeMarkdown escapes characters that could break markdown link syntax.
func escapeMarkdown(s string) string {
r := strings.NewReplacer(
"[", `\[`,
"]", `\]`,
"(", `\(`,
")", `\)`,
)
return r.Replace(s)
}
fqname_test.gno
package fqname
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestParse(t *testing.T) {
tests := []struct {
input string
expectedPkgPath string
expectedName string
}{
{"gno.land/p/nt/avl/v0.Tree", "gno.land/p/nt/avl/v0", "Tree"},
{"gno.land/p/nt/avl/v0", "gno.land/p/nt/avl/v0", ""},
{"gno.land/p/nt/avl/v0.Tree.Node", "gno.land/p/nt/avl/v0", "Tree.Node"},
{"gno.land/p/nt/avl/v0/nested.Package.Func", "gno.land/p/nt/avl/v0/nested", "Package.Func"},
{"path/filepath.Split", "path/filepath", "Split"},
{"path.Split", "path", "Split"},
{"path/filepath", "path/filepath", ""},
{"path", "path", ""},
{"", "", ""},
}
for _, tt := range tests {
pkgpath, name := Parse(tt.input)
uassert.Equal(t, tt.expectedPkgPath, pkgpath, "Package path did not match")
uassert.Equal(t, tt.expectedName, name, "Name did not match")
}
}
func TestConstruct(t *testing.T) {
tests := []struct {
pkgpath string
name string
expected string
}{
{"gno.land/r/demo/foo20", "Token", "gno.land/r/demo/foo20.Token"},
{"gno.land/r/demo/foo20", "", "gno.land/r/demo/foo20"},
{"path", "", "path"},
{"path", "Split", "path.Split"},
{"path/filepath", "", "path/filepath"},
{"path/filepath", "Split", "path/filepath.Split"},
{"", "JustName", ".JustName"},
{"", "", ""},
}
for _, tt := range tests {
result := Construct(tt.pkgpath, tt.name)
uassert.Equal(t, tt.expected, result, "Constructed FQName did not match expected")
}
}
func TestRenderLink(t *testing.T) {
tests := []struct {
pkgPath string
slug string
expected string
}{
{"gno.land/p/nt/avl/v0", "Tree", "[gno.land/p/nt/avl/v0](/p/nt/avl/v0).Tree"},
{"gno.land/p/nt/avl/v0", "", "[gno.land/p/nt/avl/v0](/p/nt/avl/v0)"},
{"github.com/a/b", "C", "github.com/a/b.C"},
{"example.com/pkg", "Func", "example.com/pkg.Func"},
{"gno.land/r/demo/foo20", "Token", "[gno.land/r/demo/foo20](/r/demo/foo20).Token"},
{"gno.land/r/demo/foo20", "", "[gno.land/r/demo/foo20](/r/demo/foo20)"},
{"", "", ""},
}
for _, tt := range tests {
result := RenderLink(tt.pkgPath, tt.slug)
uassert.Equal(t, tt.expected, result, "Rendered link did not match expected")
}
}
gnomod.toml
module = "gno.land/p/nt/fqname/v0" gno = "0.9" [addpkg] creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"