-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
685 additions
and
401 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
// README at: https://github.com/devcontainers/templates/tree/main/src/go | ||
{ | ||
"name": "Sprout", | ||
"image": "mcr.microsoft.com/devcontainers/go:1.22-bullseye", | ||
// Configure tool-specific properties. | ||
"customizations": { | ||
// Configure properties specific to VS Code. | ||
"vscode": { | ||
// Set *default* container specific settings.json values on container create. | ||
"settings": { | ||
"go.toolsManagement.checkForUpdates": "local", | ||
"go.useLanguageServer": true, | ||
"go.gopath": "/go", | ||
"go.coverMode": "atomic", | ||
"go.coverOnSave": true, | ||
"go.disableConcurrentTests": true, | ||
"editor.formatOnSave": true, | ||
"go.lintTool": "golangci-lint", | ||
"editor.tabSize": 2, | ||
"editor.renderWhitespace": "all", | ||
"gopls": { | ||
"ui.completion.usePlaceholders": true, | ||
// Experimental settings | ||
"completeUnimported": true, // autocomplete unimported packages | ||
"deepCompletion": true, // enable deep completion | ||
"staticcheck": true | ||
}, | ||
"editor.codeActionsOnSave": { | ||
"source.organizeImports": "explicit", | ||
"source.fixAll": "explicit" | ||
}, | ||
"git.autofetch": true, | ||
"files.autoGuessEncoding": true, | ||
"files.encoding": "utf8", | ||
"go.delveConfig": { | ||
"apiVersion": 2, | ||
"showGlobalVariables": false | ||
}, | ||
"editor.rulers": [80], | ||
"search.useGlobalIgnoreFiles": true, | ||
"search.useParentIgnoreFiles": true | ||
}, | ||
|
||
// Add the IDs of extensions you want installed when the container is created. | ||
"extensions": [ | ||
"golang.Go", | ||
"aaron-bond.better-comments", | ||
"jasonnutter.vscode-codeowners", | ||
"jinliming2.vscode-go-template" | ||
] | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch test function", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "test", | ||
"program": "${workspaceFolder}", | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,2 @@ | ||
.PHONY: test | ||
test: | ||
@echo "==> Running tests" | ||
GO111MODULE=on go test -v | ||
|
||
.PHONY: test-cover | ||
test-cover: | ||
@echo "==> Running Tests with coverage" | ||
GO111MODULE=on go test -cover . | ||
go test ./... |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,80 @@ | ||
# Sprig: Template functions for Go templates | ||
# Sprout 🌱 | ||
|
||
[![GoDoc](https://img.shields.io/static/v1?label=godoc&message=reference&color=blue)](https://pkg.go.dev/github.com/Masterminds/sprig/v3) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/Masterminds/sprig)](https://goreportcard.com/report/github.com/Masterminds/sprig) | ||
[![Stability: Sustained](https://masterminds.github.io/stability/sustained.svg)](https://masterminds.github.io/stability/sustained.html) | ||
[![](https://github.com/Masterminds/sprig/workflows/Tests/badge.svg)](https://github.com/Masterminds/sprig/actions) | ||
> [!NOTE] | ||
> Sprout is an evolved variant of the [Masterminds/sprig](https://github.com/Masterminds/sprig) library, reimagined for modern Go versions. It introduces fresh functionalities and commits to maintaining the library, picking up where Sprig left off. Notably, Sprig had not seen updates for two years and was not compatible beyond Golang 1.13, necessitating the creation of Sprout. | ||
The Go language comes with a [built-in template | ||
language](http://golang.org/pkg/text/template/), but not | ||
very many template functions. Sprig is a library that provides more than 100 commonly | ||
used template functions. | ||
# Table of Content | ||
|
||
It is inspired by the template functions found in | ||
[Twig](http://twig.sensiolabs.org/documentation) and in various | ||
JavaScript libraries, such as [underscore.js](http://underscorejs.org/). | ||
|
||
## IMPORTANT NOTES | ||
|
||
Sprig leverages [mergo](https://github.com/imdario/mergo) to handle merges. In | ||
its v0.3.9 release, there was a behavior change that impacts merging template | ||
functions in sprig. It is currently recommended to use v0.3.10 or later of that package. | ||
Using v0.3.9 will cause sprig tests to fail. | ||
## Transitioning from Sprig | ||
|
||
## Package Versions | ||
For those looking to switch from Sprig to Sprout, the process is straightforward and involves just a couple of steps: | ||
1. Ensure your project uses Sprig's last version (v3.2.3). | ||
2. Update your import statements and package references as shown below: | ||
```diff | ||
import ( | ||
- "github.com/Masterminds/sprig/v3" | ||
+ "github.com/42atomys/sprout" | ||
|
||
There are two active major versions of the `sprig` package. | ||
"html/template" | ||
) | ||
|
||
* v3 is currently stable release series on the `master` branch. The Go API should | ||
remain compatible with v2, the current stable version. Behavior change behind | ||
some functions is the reason for the new major version. | ||
* v2 is the previous stable release series. It has been more than three years since | ||
the initial release of v2. You can read the documentation and see the code | ||
on the [release-2](https://github.com/Masterminds/sprig/tree/release-2) branch. | ||
Bug fixes to this major version will continue for some time. | ||
tpl := template.Must( | ||
template.New("base"). | ||
- Funcs(sprig.FuncMap()). | ||
+ Funcs(sprout.FuncMap()). | ||
ParseGlob("*.html") | ||
) | ||
``` | ||
|
||
## Usage | ||
|
||
**Template developers**: Please use Sprig's [function documentation](http://masterminds.github.io/sprig/) for | ||
detailed instructions and code snippets for the >100 template functions available. | ||
|
||
**Go developers**: If you'd like to include Sprig as a library in your program, | ||
our API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig). | ||
**For Template Creators**: Refer to the comprehensive function guide in Sprig's documentation for detailed instructions and examples across over 100 template functions. | ||
|
||
For standard usage, read on. | ||
**For Go Developers**: Integrate Sprout into your applications by consulting our API documentation available on GoDoc.org. | ||
|
||
### Load the Sprig library | ||
For general library usage, proceed as follows. | ||
|
||
To load the Sprig `FuncMap`: | ||
### Integrating the Sprout Library | ||
To utilize Sprout's functions within your templates: | ||
|
||
```go | ||
|
||
```golang | ||
import ( | ||
"github.com/Masterminds/sprig/v3" | ||
"github.com/42atomys/sprout" | ||
"html/template" | ||
) | ||
|
||
// This example illustrates that the FuncMap *must* be set before the | ||
// templates themselves are loaded. | ||
// Ensure the FuncMap is set before loading the templates. | ||
tpl := template.Must( | ||
template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html") | ||
template.New("base").Funcs(sprout.FuncMap()).ParseGlob("*.html") | ||
) | ||
|
||
|
||
``` | ||
|
||
### Calling the functions inside of templates | ||
### Template Function Invocation | ||
Adhering to Go's conventions, all Sprout functions are lowercase, differing from method naming which employs TitleCase. For instance, this template snippet: | ||
|
||
By convention, all functions are lowercase. This seems to follow the Go | ||
idiom for template functions (as opposed to template methods, which are | ||
TitleCase). For example, this: | ||
|
||
``` | ||
```golang | ||
{{ "hello!" | upper | repeat 5 }} | ||
``` | ||
|
||
produces this: | ||
|
||
Will output: | ||
``` | ||
HELLO!HELLO!HELLO!HELLO!HELLO! | ||
``` | ||
|
||
## Principles Driving Our Function Selection | ||
|
||
We followed these principles to decide which functions to add and how to implement them: | ||
|
||
- Use template functions to build layout. The following | ||
types of operations are within the domain of template functions: | ||
- Formatting | ||
- Layout | ||
- Simple type conversions | ||
- Utilities that assist in handling common formatting and layout needs (e.g. arithmetic) | ||
- Template functions should not return errors unless there is no way to print | ||
a sensible value. For example, converting a string to an integer should not | ||
produce an error if conversion fails. Instead, it should display a default | ||
value. | ||
- Simple math is necessary for grid layouts, pagers, and so on. Complex math | ||
(anything other than arithmetic) should be done outside of templates. | ||
- Template functions only deal with the data passed into them. They never retrieve | ||
data from a source. | ||
- Finally, do not override core Go template functions. | ||
### Development Philosophy (Currently in reflexion to create our) | ||
|
||
Our approach to extending and refining Sprout was guided by several key principles: | ||
|
||
- Empowering layout construction through template functions. | ||
- Designing template functions that avoid returning errors when possible, instead displaying default values for smoother user experiences. | ||
- Ensuring template functions operate solely on provided data, without external data fetching. | ||
- Maintaining the integrity of core Go template functionalities without overrides. | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"crypto/x509" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"strconv" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"testing" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"testing" | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"strings" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
/* | ||
Package sprig provides template functions for Go. | ||
package sprout provides template functions for Go. | ||
This package contains a number of utility functions for working with data | ||
inside of Go `html/template` and `text/template` files. | ||
To add these functions, use the `template.Funcs()` method: | ||
t := templates.New("foo").Funcs(sprig.FuncMap()) | ||
t := templates.New("foo").Funcs(sprout.FuncMap()) | ||
Note that you should add the function map before you parse any template files. | ||
In several cases, Sprig reverses the order of arguments from the way they | ||
In several cases, sprout reverses the order of arguments from the way they | ||
appear in the standard library. This is to make it easier to pipe | ||
arguments into functions. | ||
See http://masterminds.github.io/sprig/ for more detailed documentation on each of the available functions. | ||
See http://masterminds.github.io/sprout/ for more detailed documentation on each of the available functions. | ||
*/ | ||
package sprig | ||
package sprout |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package sprig | ||
package sprout | ||
|
||
import ( | ||
"testing" | ||
|
Oops, something went wrong.