Skip to content

Commit

Permalink
chore: readme refactor 🌱 (#4)
Browse files Browse the repository at this point in the history
This pull request juste refactor the repository README to match the
vision and others readme of my projects.
  • Loading branch information
42atomys committed Apr 3, 2024
1 parent f38aa72 commit d64a48e
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 49 deletions.
20 changes: 20 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
coverage:
precision: 1
range: "75...90"
status:
project:
default:
target: 80%
threshold: 2%
patch:
default:
target: 80%
threshold: 2%
github_checks:
annotations: true
ignore:
- benchmark/**
- docs/**
comment:
behavior: new
require_changes: true
Binary file added .github/profile/images/logo_landing_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/profile/images/logo_landing_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ jobs:
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Test
run: make test
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
codecov_yml_path: .github/codecov.yml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

vendor/
/.glide

coverage.out
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test:
go test ./...
go test -coverprofile=coverage.out ./...
195 changes: 149 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,98 +1,201 @@
# Sprout 🌱
<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset=".github/profile/images/logo_landing_light.png">
<source media="(prefers-color-scheme: light)" srcset=".github/profile/images/logo_landing_dark.png">
<img alt="Sprout Logo" width="700" src="">
</picture>
<hr />
</div>

<div align="center">
<a target="_blank" href="https://github.com/42atomys/sprout/actions/workflows/test.yaml"><img src="https://img.shields.io/github/actions/workflow/status/42atomys/sprout/test.yaml?branch=main&label=tests"></a>
<a target="_blank" href="https://goreportcard.com/report/github.com/42atomys/sprout"><img src="https://goreportcard.com/badge/github.com/42atomys/sprout" /></a>
<a target="_blank" href="https://codeclimate.com/github/42atomys/sprout"><img alt="Code Climate maintainability" src="https://img.shields.io/codeclimate/maintainability/42atomys/sprout"></a>
<a target="_blank" href="https://codecov.io/gh/42atomys/sprout"><img alt="Codecov" src="https://img.shields.io/codecov/c/github/42atomys/sprout"></a>
<img src="https://img.shields.io/github/v/release/42atomys/sprout?label=last%20release" alt="GitHub release (latest by date)">
<img src="https://img.shields.io/github/contributors/42atomys/sprout?color=blueviolet" alt="GitHub contributors">
<img src="https://img.shields.io/github/stars/42atomys/sprout?style=flat&color=blueviolet" alt="GitHub Repo stars">
<a target="_blank" href="https://pkg.go.dev/github.com/42atomys/sprout"><img src="https://pkg.go.dev/badge/github.com/42atomys/sprout.svg" alt="Go Reference"></a>
<br />
<h3> <a target="_blank" href="https://docs.atom.codes/sprout">Official Documentation</a></h3>
<hr/>
</div>

> [!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.
## Motivation

Sprout was born out of the need for a modernized, maintained, and performant template function library. Sprig, the predecessor to Sprout, had not seen updates for two years and was not optimized for later versions of Golang. Sprout aims to fill this gap by providing a library that is actively maintained, compatible with the latest Go versions, and optimized for performance.

## Roadmap to Sprout v1.0

You can track our progress towards Sprout v1.0 by following the documentation page
[here](https://docs.atom.codes/sprout/roadmap-to-sprout-v1.0).

## Table of Contents

- [Table of Contents](#table-of-contents)
- [Motivation](#motivation)
- [Roadmap to Sprout v1.0](#roadmap-to-sprout-v10)
- [Transitioning from Sprig](#transitioning-from-sprig)
- [Performence Benchmarks](#performence-benchmarks)
- [Sprig v3.2.3 vs Sprout v0.1](#sprig-v323-vs-sprout-v01)
- [Usage](#usage)
- [Integrating the Sprout Library](#integrating-the-sprout-library)
- [Template Function Invocation](#template-function-invocation)
- [Usage: Logger](#usage-logger)
- [Usage: Alias](#usage-alias)
- [Usage: Error Handling](#usage-error-handling)
- [Default Value](#default-value)
- [Panic](#panic)
- [Error Channel](#error-channel)
- [Performence Benchmarks](#performence-benchmarks)
- [Sprig v3.2.3 vs Sprout v0.2](#sprig-v323-vs-sprout-v02)
- [Development Philosophy (Currently in reflexion to create our)](#development-philosophy-currently-in-reflexion-to-create-our)


## Transitioning from Sprig

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:
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:

```diff
import (
- "github.com/Masterminds/sprig/v3"
+ "github.com/42atomys/sprout"

"html/template"
)

tpl := template.Must(
template.New("base").
- Funcs(sprig.FuncMap()).
+ Funcs(sprout.FuncMap()).
ParseGlob("*.html")
ParseGlob("*.tmpl")
)
```

## Performence Benchmarks
## Usage

To see all the benchmarks, please refer to the [benchmarks](benchmarks/README.md) directory.
To use Sprout in your project, import the library and use the `FuncMap` function to add the template functions to your template:

### Sprig v3.2.3 vs Sprout v0.1
```go
import (
"github.com/42atomys/sprout"
"text/template"
)

tpl := template.Must(
template.New("base").
Funcs(sprout.FuncMap()).
ParseGlob("*.tmpl")
)
```
goos: linux
goarch: amd64
pkg: sprout_benchmarks
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkSprig-12 1 3869134593 ns/op 45438616 B/op 24098 allocs/op
BenchmarkSprout-12 1 1814126036 ns/op 38284040 B/op 11627 allocs/op
PASS
ok sprout_benchmarks 5.910s

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:

```go
handler := sprout.NewFunctionHandler(
// Add your logger to the handler to log errors and debug information using the
// standard slog package or any other logger that implements the slog.Logger interface.
// By default, Sprout uses a slog.TextHandler.
sprout.WithLogger(slogLogger),
// Set the error handling behavior for the handler. By default, Sprout returns the default value of the return type without crashes or panics.
sprout.WithErrHandling(sprout.ErrHandlingReturnDefaultValue),
// Set the error channel for the handler. By default, Sprout does not use an error channel. If you set an error channel, Sprout will send errors to it.
// This options is only used when the error handling behavior is set to
// `ErrHandlingErrorChannel`
sprout.WithErrorChannel(errChan),
// Set the alias for a function. By default, Sprout use alias for some functions for backward compatibility with Sprig.
sprout.WithAlias("hello", "hi"),
)

// Use the handler with the FuncMap function. The handler will be used to handle all template functions.
tpl := template.Must(
template.New("base").
Funcs(sprout.FuncMap(sprout.WithFunctionHandler(handler))).
ParseGlob("*.tmpl")
)
```
### Usage: Logger

**Time improvement**: 53.1%
**Memory improvement**: 15.7%
Sprout uses the `slog` package for logging. You can pass your logger
to the `WithLogger` configuration function to log errors and debug information:

So, Sprout v0.1 is approximately 53.1% faster and uses 15.7% less memory than Sprig v3.2.3. 🚀
```go
// Create a new logger using the slog package.
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

// Use the handler with the FuncMap function.
tpl := template.Must(
template.New("base").
Funcs(sprout.FuncMap(sprout.WithLogger(logger))).
ParseGlob("*.tmpl")
)
```

## Usage
### Usage: Alias

**For Template Creators**: Refer to the comprehensive function guide in Sprig's documentation for detailed instructions and examples across over 100 template functions.
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.

**For Go Developers**: Integrate Sprout into your applications by consulting our API documentation available on GoDoc.org.
See more about the alias in the [documentation](https://docs.atom.codes/sprout/function-aliases).

For general library usage, proceed as follows.
```go
sprout.NewFunctionHandler(
sprout.WithAlias("hello", "hi"),
)
```

### Integrating the Sprout Library
To utilize Sprout's functions within your templates:
### Usage: Error Handling

Sprout provides three error handling behaviors:
- `ErrHandlingReturnDefaultValue`: Sprout returns the default value of the return type without crashes or panics.
- `ErrHandlingPanic`: Sprout panics when an error occurs.
- `ErrHandlingErrorChannel`: Sprout sends errors to the error channel.

```golang
import (
"github.com/42atomys/sprout"
"html/template"
)
You can set the error handling behavior using the `WithErrHandling` configuration function:

// Ensure the FuncMap is set before loading the templates.
tpl := template.Must(
template.New("base").Funcs(sprout.FuncMap()).ParseGlob("*.html")
```go
sprout.NewFunctionHandler(
sprout.WithErrHandling(sprout.ErrHandlingReturnDefaultValue),
)
```

### 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:
#### Default Value

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.

#### Panic

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.

```golang
{{ "hello!" | upper | repeat 5 }}
#### Error Channel

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:

```go
errChan := make(chan error)

sprout.NewFunctionHandler(
sprout.WithErrHandling(sprout.ErrHandlingErrorChannel),
sprout.WithErrorChannel(errChan),
)
```
Will output:

## Performence Benchmarks

To see all the benchmarks, please refer to the [benchmarks](benchmarks/README.md) directory.

### Sprig v3.2.3 vs Sprout v0.2
```
HELLO!HELLO!HELLO!HELLO!HELLO!
goos: linux
goarch: amd64
pkg: sprout_benchmarks
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkSprig-12 1 3869134593 ns/op 45438616 B/op 24098 allocs/op
BenchmarkSprout-12 1 1814126036 ns/op 38284040 B/op 11627 allocs/op
PASS
ok sprout_benchmarks 5.910s
```

**Time improvement**: 53.1%
**Memory improvement**: 15.7%

So, Sprout v0.2 is approximately 53.1% faster and uses 15.7% less memory than Sprig v3.2.3. 🚀

## Development Philosophy (Currently in reflexion to create our)

Our approach to extending and refining Sprout was guided by several key principles:
Expand Down

0 comments on commit d64a48e

Please sign in to comment.