Skip to content

Commit

Permalink
chore: migrate to sprout 🌱
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Mar 29, 2024
1 parent 581758e commit c0e8676
Show file tree
Hide file tree
Showing 39 changed files with 685 additions and 401 deletions.
54 changes: 54 additions & 0 deletions .devcontainer/devcontainer.json
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"
]
}
}
}
6 changes: 2 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x, 1.18.x, 1.19.x]
go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand All @@ -15,6 +15,4 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3
- name: Test
env:
GO111MODULE: on
run: go test -cover .
run: make test
15 changes: 15 additions & 0 deletions .vscode/launch.json
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}",
}
]
}
9 changes: 1 addition & 8 deletions Makefile
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 ./...
116 changes: 48 additions & 68 deletions README.md
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.






42 changes: 41 additions & 1 deletion crypto.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"bytes"
Expand Down Expand Up @@ -36,6 +36,46 @@ import (
"golang.org/x/crypto/scrypt"
)

type cryptoRandomStringOpts struct {
letters bool
numbers bool
ascii bool
chars []rune
}

func cryptoRandomString(count int, opts cryptoRandomStringOpts) string {
source := []rune{}
if opts.chars == nil {
if opts.ascii {
for i := 32; i <= 126; i++ {
source = append(source, rune(i))
}
}

if opts.letters {
for i := 'a'; i <= 'z'; i++ {
source = append(source, i)
}
for i := 'A'; i <= 'Z'; i++ {
source = append(source, i)
}
}
if opts.numbers {
for i := '0'; i <= '9'; i++ {
source = append(source, i)
}
}
}

// Generate random string
return strings.Map(func(r rune) rune {
if bigInt, err := rand.Int(rand.Reader, big.NewInt(int64(len(source)))); err == nil {
return source[bigInt.Int64()]
}
return rune(-1) // Should not happen, indicates an error
}, strings.Repeat(" ", count))
}

func sha256sum(input string) string {
hash := sha256.Sum256([]byte(input))
return hex.EncodeToString(hash[:])
Expand Down
2 changes: 1 addition & 1 deletion crypto_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"crypto/x509"
Expand Down
2 changes: 1 addition & 1 deletion date.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"strconv"
Expand Down
2 changes: 1 addition & 1 deletion date_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion defaults_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions dict.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sprig
package sprout

import (
"github.com/imdario/mergo"
"dario.cat/mergo"
"github.com/mitchellh/copystructure"
)

Expand Down
2 changes: 1 addition & 1 deletion dict_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"strings"
Expand Down
10 changes: 5 additions & 5 deletions doc.go
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
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"fmt"
Expand All @@ -11,7 +11,7 @@ func Example() {
vars := map[string]interface{}{"Name": " John Jacob Jingleheimer Schmidt "}
tpl := `Hello {{.Name | trim | lower}}`

// Get the Sprig function map.
// Get the sprout function map.
fmap := TxtFuncMap()
t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))

Expand Down
2 changes: 1 addition & 1 deletion flow_control_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion functions_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sprig
package sprout

import (
"testing"
Expand Down
Loading

0 comments on commit c0e8676

Please sign in to comment.