feat(template): add FastParser for TinyGo compatibility#400
Open
cxjava wants to merge 1 commit intonicksnyder:mainfrom
Open
feat(template): add FastParser for TinyGo compatibility#400cxjava wants to merge 1 commit intonicksnyder:mainfrom
cxjava wants to merge 1 commit intonicksnyder:mainfrom
Conversation
Add FastParser as an alternative template parser using valyala/fasttemplate.
This provides TinyGo compatibility since text/template has limited support
in TinyGo due to heavy reflection usage.
FastParser supports:
- Simple placeholder substitution: {{.Key}} and {{Key}}
- Custom delimiters
- map[string]interface{}, map[string]string, and struct data types
- Plural forms (One, Other, etc.)
Limitations (use TextParser instead):
- Template functions
- Conditionals (if/else)
- Range loops
- Nested field access
- Method calls
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #400 +/- ##
==========================================
+ Coverage 73.97% 74.24% +0.26%
==========================================
Files 20 21 +1
Lines 1679 1778 +99
==========================================
+ Hits 1242 1320 +78
- Misses 376 389 +13
- Partials 61 69 +8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
FastParseras an alternative template parser using valyala/fasttemplate, primarily for TinyGo compatibility.Motivation
TinyGo has limited support for the standard library's
text/templateandhtml/templatepackages due to their heavy use of reflection and other features that are difficult to compile to WebAssembly or embedded targets. This makes go-i18n unusable in TinyGo environments when templates contain placeholders.FastParserprovides a lightweight alternative that works reliably in TinyGo while still supporting the common use case of simple placeholder substitution.Changes
FastParserimplementingtemplate.ParserinterfaceFastParserlocalizer_test.gogithub.1485827954.workers.dev/valyala/fasttemplatedependencyUsage
Supported Features
{{.Key}}and{{Key}}<<>>)map[string]interface{},map[string]string, and structsLimitations
For these features, continue using
TextParser:{{printf "%s" .Name}}){{if .Cond}}...{{end}}){{range .Items}}...{{end}}){{.User.Name}}){{.Name.String}})Benchmark
FastParser is slightly faster (~5%) for simple placeholder substitution.