You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: implement registry system (alias: loader) (#46)
## Description
Following the discussion about function loading in #31, this huge
refactor implement each method behind multiples registries.
> [!WARNING]
> This pull request are in progress, somes tests must be done before
allowing it to be merge and release. Each feedback are welcome !
## TODO
- [x] Allow registry to register aliases with function
- [x] Test new package
- [x] Provide detailed documentation for each module and glossary for
handler and registry (WIP but available
[here](https://docs.atom.codes/sprout/~/changes/gP9HOVz2PoFWbplTHwVo))
## Changes
- Create the Handler and Registry interface
- Define the rules of a registry
- Add a Readme about how to create a registry
- Migrate all existing functions into separated registry
- Backward compatibility function `FuncsMap` register all registry by
default
## Fixes#10
## Checklist
- [x] I have read the **CONTRIBUTING.md** document.
- [x] My code follows the code style of this project.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] I have updated the documentation accordingly.
- [x] This change requires a change to the documentation on the website.
## Additional Information
This update following the discussions #31
---------
Co-authored-by: ccoVeille <[email protected]>
-[Sprig v3.2.3 vs Sprout v0.2](#sprig-v323-vs-sprout-v02)
50
50
-[Development Philosophy (Currently in reflexion to create our)](#development-philosophy-currently-in-reflexion-to-create-our)
51
51
52
52
53
53
## Transitioning from Sprig
54
54
55
-
Sprout is designed to be a drop-in replacement for Sprig in the v1.0, with the same function names and behavior. To use Sprout in your project, simply replace the Sprig import with Sprout:
55
+
Sprout provide a package `sprigin` to provide a drop-in replacement for Sprig in the v1.0, with the same function names and behavior. To use Sprout in your project, simply replace the Sprig import with sprigin:
56
+
57
+
> [!IMPORTANT]
58
+
> The `sprigin` package is a temporary solution to provide backward compatibility with Sprig. We recommend updating your code to use the Sprout package directly to take advantage of the new features and improvements.
59
+
>
60
+
> A complete guide are available in the [documentation](https://docs.atom.codes/sprout/migration-from-sprig).
56
61
57
62
```diff
58
63
import (
59
64
- "github.com/Masterminds/sprig/v3"
60
-
+ "github.com/go-sprout/sprout"
65
+
+ "github.com/go-sprout/sprout/sprigin"
61
66
)
62
67
63
68
tpl := template.Must(
64
69
template.New("base").
65
70
- Funcs(sprig.FuncMap()).
66
-
+ Funcs(sprout.FuncMap()).
71
+
+ Funcs(sprigin.FuncMap()).
67
72
ParseGlob("*.tmpl")
68
73
)
69
74
```
70
75
71
76
## Usage
72
77
73
-
To use Sprout in your project, import the library and use the `FuncMap` function to add the template functions to your template:
78
+
### Creating a Handler
79
+
A handler in Sprout is responsible for managing the function registries and functions. The DefaultHandler is the primary implementation provided by Sprout.
74
80
75
81
```go
76
-
import (
77
-
"github.com/go-sprout/sprout"
78
-
"text/template"
79
-
)
82
+
import"github.com/go-sprout/sprout"
80
83
81
-
tpl:= template.Must(
82
-
template.New("base").
83
-
Funcs(sprout.FuncMap()).
84
-
ParseGlob("*.tmpl")
85
-
)
86
-
```
84
+
handler:= sprout.New()
85
+
```
86
+
87
+
### Customizing the Handler
87
88
88
-
You can customize the behavior of Sprout by creating a `FunctionHandler` and passing it to the `FuncMap` function or using the configuration functions provided by Sprout:
89
+
Sprout supports various customization options using handler options:
89
90
90
91
```go
91
-
handler:= sprout.NewFunctionHandler(
92
+
handler:= sprout.New(
92
93
// Add your logger to the handler to log errors and debug information using the
93
94
// standard slog package or any other logger that implements the slog.Logger interface.
"github.com/go-sprout/sprout/registry/std"// default, empty, any, all, ...
127
117
)
128
-
```
129
118
130
-
### Usage: Alias
119
+
//...
131
120
132
-
Sprout provides the ability to set an alias for a function. This feature is useful for backward compatibility with Sprig. You can set an alias for a function using the `WithAlias` or `WithAliases` configuration functions.
133
-
134
-
See more about the alias in the [documentation](https://docs.atom.codes/sprout/function-aliases).
135
-
136
-
```go
137
-
sprout.NewFunctionHandler(
138
-
sprout.WithAlias("hello", "hi"),
121
+
handler.AddRegistries(
122
+
conversion.NewRegistry(),
123
+
std.NewRegistry(),
139
124
)
140
125
```
141
126
142
-
### Usage: Error Handling
143
-
144
-
Sprout provides three error handling behaviors:
145
-
-`ErrHandlingReturnDefaultValue`: Sprout returns the default value of the return type without crashes or panics.
146
-
-`ErrHandlingPanic`: Sprout panics when an error occurs.
147
-
-`ErrHandlingErrorChannel`: Sprout sends errors to the error channel.
148
-
149
-
You can set the error handling behavior using the `WithErrHandling` configuration function:
127
+
### Building Function Maps
150
128
129
+
To use Sprout with templating engines like `html/template` or `text/template`, you need to build the function map:
This will render the template with all functions and aliases available.
158
145
159
-
If you set the error handling behavior to `ErrHandlingReturnDefaultValue`, Sprout will return the default value of the return type without crashes or panics to ensure a smooth user experience when an error occurs.
160
146
161
-
#### Panic
147
+
##Usage: Quick Example
162
148
163
-
If you set the error handling behavior to `ErrHandlingPanic`, Sprout will panic when an error occurs to ensure that the error is not ignored and sended back to template execution.
149
+
Here is a quick example of how to use Sprout with the `text/template` package:
150
+
```go
151
+
package main
164
152
165
-
#### Error Channel
153
+
import (
154
+
"os"
155
+
"text/template"
166
156
167
-
If you set the error handling behavior to `ErrHandlingErrorChannel`, you can pass an error channel to the `WithErrorChannel` configuration function. Sprout will send errors to the error channel:
0 commit comments