Transaction
8AC3FA60DAB4BD…08A7749032B0
Block 25,652 · index 0 · indexed
Summary
- Hash
- 8AC3FA60DAB4BD014FEC0038622D4F150E8954A0DB0D94DE747D08A7749032B0
- Block
- 25,652
- Size
- 4770 bytes
- Gas used
- 10,544,586 / 12,653,563
- Fee
- 1000000ugnot
- Memo
- gnopublish
- Status
- success
Messages
Arguments · 9
- #1dynreplacer
- #2README.md
- #3# `gno.land/p/moul/dynreplacer/v0` Simple template engine for dynamic content replacement. <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. > ⚠️ **Disclaimer:** provided as-is, without warranty; not security-audited. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
- #4dynreplacer.gno
- #5// Package dynreplacer provides a simple template engine for handling dynamic // content replacement. It is similar to strings.Replacer but with lazy // execution of replacements, making it more optimization-friendly in several // cases. While strings.Replacer requires all replacement values to be computed // upfront, dynreplacer only executes the callback functions for placeholders // that actually exist in the template, avoiding unnecessary computations. // // The package ensures efficient, non-recursive replacement of placeholders in a // single pass. This lazy evaluation approach is particularly beneficial when: // - Some replacement values are expensive to compute // - Not all placeholders are guaranteed to be present in the template // - Templates are reused with different content // // Example usage: // // r := dynreplacer.New( // dynreplacer.Pair{":name:", func() string { return "World" }}, // dynreplacer.Pair{":greeting:", func() string { return "Hello" }}, // ) // result := r.Replace("Hello :name:!") // Returns "Hello World!" // // The replacer caches computed values, so subsequent calls with the same // placeholder will reuse the cached value instead of executing the callback // again: // // r := dynreplacer.New() // r.RegisterCallback(":expensive:", func() string { return "computed" }) // r.Replace("Value1: :expensive:") // Computes the value // r.Replace("Value2: :expensive:") // Uses cached value // r.ClearCache() // Force re-computation on next use package dynreplacer import ( "strings" ) // Replacer manages dynamic placeholders, their associated functions, and cached // values. type Replacer struct { callbacks map[string]func() string cachedValues map[string]string } // Pair represents a placeholder and its callback function type Pair struct { Placeholder string Callback func() string } // New creates a new Replacer instance with optional initial replacements. // It accepts pairs where each pair consists of a placeholder string and // its corresponding callback function. // // Example: // // New( // Pair{":name:", func() string { return "World" }}, // Pair{":greeting:", func() string { return "Hello" }}, // ) func New(pairs ...Pair) *Replacer { r := &Replacer{ callbacks: make(map[string]func() string), cachedValues: make(map[string]string), } for _, pair := range pairs { r.RegisterCallback(pair.Placeholder, pair.Callback) } return r } // RegisterCallback associates a placeholder with a function to generate its // content. func (r *Replacer) RegisterCallback(placeholder string, callback func() string) { r.callbacks[placeholder] = callback } // Replace processes the given layout, replacing placeholders with cached or // newly computed values. func (r *Replacer) Replace(layout string) string { replacements := []string{} // Check for placeholders and compute/retrieve values hasReplacements := false for placeholder, callback := range r.callbacks { if strings.Contains(layout, placeholder) { value, exists := r.cachedValues[placeholder] if !exists { value = callback() r.cachedValues[placeholder] = value } replacements = append(replacements, placeholder, value) hasReplacements = true } } // If no replacements were found, return the original layout if !hasReplacements { return layout } // Create a strings.Replacer with all computed replacements replacer := strings.NewReplacer(replacements...) return replacer.Replace(layout) } // ClearCache clears all cached values, forcing re-computation on next Replace. func (r *Replacer) ClearCache() { r.cachedValues = make(map[string]string) }
- #6gnomod.toml
- #7module = "gno.land/p/moul/dynreplacer/v0" gno = "0.9" [addpkg] creator = "g1manfred47kzduec920z88wfr64ylksmdcedlf5"
- #8/gno.MemPackageType
- #9 MPUserProd
Result log
msg:0,success:true,log:,events:[]