mirror of
https://github.com/Luzifer/nginx-sso.git
synced 2024-12-20 12:51:17 +00:00
Vendor dependencies
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
d7f9bf5874
commit
9a467952cc
978 changed files with 276302 additions and 0 deletions
100
Gopkg.lock
generated
Normal file
100
Gopkg.lock
generated
Normal file
|
@ -0,0 +1,100 @@
|
|||
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/GeertJohan/yubigo"
|
||||
packages = ["."]
|
||||
revision = "b1764f04aa9ba3c98a15084e7e13c1a69753e1da"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/Luzifer/go_helpers"
|
||||
packages = ["str"]
|
||||
revision = "8fdddb7041fe962e750caa553a0714f94e261c4a"
|
||||
version = "v2.3.1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/Luzifer/rconfig"
|
||||
packages = ["."]
|
||||
revision = "7aef1d393c1e2d0758901853b59981c7adc67c7e"
|
||||
version = "v1.2.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/flosch/pongo2"
|
||||
packages = ["."]
|
||||
revision = "1f4be1efe3b3529b7e58861f75d70120a9567dc4"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/gorilla/context"
|
||||
packages = ["."]
|
||||
revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a"
|
||||
version = "v1.1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/gorilla/securecookie"
|
||||
packages = ["."]
|
||||
revision = "667fe4e3466a040b780561fe9b51a83a3753eefc"
|
||||
version = "v1.1"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/gorilla/sessions"
|
||||
packages = ["."]
|
||||
revision = "ca9ada44574153444b00d3fd9c8559e4cc95f896"
|
||||
version = "v1.1"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/juju/errors"
|
||||
packages = ["."]
|
||||
revision = "c7d06af17c68cd34c835053720b21f6549d9b0ee"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/sirupsen/logrus"
|
||||
packages = ["."]
|
||||
revision = "d682213848ed68c0a260ca37d6dd5ace8423f5ba"
|
||||
version = "v1.0.4"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/spf13/pflag"
|
||||
packages = ["."]
|
||||
revision = "e57e3eeb33f795204c1ca35f56c44f83227c6e66"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/crypto"
|
||||
packages = [
|
||||
"bcrypt",
|
||||
"blowfish",
|
||||
"ssh/terminal"
|
||||
]
|
||||
revision = "1875d0a70c90e57f11972aefd42276df65e895b9"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/sys"
|
||||
packages = [
|
||||
"unix",
|
||||
"windows"
|
||||
]
|
||||
revision = "ff2a66f350cefa5c93a634eadb5d25bb60c85a9c"
|
||||
|
||||
[[projects]]
|
||||
branch = "v2"
|
||||
name = "gopkg.in/validator.v2"
|
||||
packages = ["."]
|
||||
revision = "460c83432a98c35224a6fe352acf8b23e067ad06"
|
||||
|
||||
[[projects]]
|
||||
branch = "v2"
|
||||
name = "gopkg.in/yaml.v2"
|
||||
packages = ["."]
|
||||
revision = "d670f9405373e636a5a2765eea47fac0c9bc91a4"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "8d56acebac43d560504fd8420c68a9021e29a60ee6b09d477ddc0d2e815a5606"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
53
Gopkg.toml
Normal file
53
Gopkg.toml
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Gopkg.toml example
|
||||
#
|
||||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
||||
# for detailed Gopkg.toml documentation.
|
||||
#
|
||||
# required = ["github.com/user/thing/cmd/thing"]
|
||||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project"
|
||||
# version = "1.0.0"
|
||||
#
|
||||
# [[constraint]]
|
||||
# name = "github.com/user/project2"
|
||||
# branch = "dev"
|
||||
# source = "github.com/myfork/project2"
|
||||
#
|
||||
# [[override]]
|
||||
# name = "github.com/x/y"
|
||||
# version = "2.4.0"
|
||||
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/GeertJohan/yubigo"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/Luzifer/go_helpers"
|
||||
version = "2.3.1"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/Luzifer/rconfig"
|
||||
version = "1.2.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/flosch/pongo2"
|
||||
branch = "master"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/gorilla/sessions"
|
||||
version = "1.1.0"
|
||||
|
||||
[[constraint]]
|
||||
name = "github.com/sirupsen/logrus"
|
||||
version = "1.0.4"
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "golang.org/x/crypto"
|
||||
|
||||
[[constraint]]
|
||||
branch = "v2"
|
||||
name = "gopkg.in/yaml.v2"
|
5
vendor/github.com/GeertJohan/yubigo/.gitignore
generated
vendored
Normal file
5
vendor/github.com/GeertJohan/yubigo/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
/yubigo
|
||||
/yubigo.sublime-project
|
||||
/yubigo.sublime-workspace
|
||||
/test
|
||||
.apikey
|
22
vendor/github.com/GeertJohan/yubigo/LICENSE
generated
vendored
Normal file
22
vendor/github.com/GeertJohan/yubigo/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
Copyright (c) 2012, Geert-Johan Riemer
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
84
vendor/github.com/GeertJohan/yubigo/readme.md
generated
vendored
Normal file
84
vendor/github.com/GeertJohan/yubigo/readme.md
generated
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
|
||||
## yubigo
|
||||
|
||||
Yubigo is a Yubikey client API library that provides an easy way to integrate the Yubikey into any Go application.
|
||||
|
||||
## Installation
|
||||
|
||||
Installation is simple. Use go get:
|
||||
`go get github.com/GeertJohan/yubigo`
|
||||
|
||||
## Usage
|
||||
|
||||
Make sure to import the library: `import "github.com/GeertJohan/yubigo"`
|
||||
|
||||
For use with the default Yubico servers, make sure you have an API key. [Request a key][getapikey].
|
||||
|
||||
**Basic OTP checking usage:**
|
||||
```go
|
||||
|
||||
// create a new yubiAuth instance with id and key
|
||||
yubiAuth, err := yubigo.NewYubiAuth("1234", "fdsaffqaf4vrc2q3cds=")
|
||||
if err != nil {
|
||||
// probably an invalid key was given
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// verify an OTP string
|
||||
result, ok, err := yubiAuth.Verify("ccccccbetgjevivbklihljgtbenbfrefccveiglnjfbc")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if ok {
|
||||
// succes!! The OTP is valid!
|
||||
log.Printf("Used query was: %s\n", result.GetRequestQuery()) // this query string includes the url of the api-server that responded first.
|
||||
} else {
|
||||
// fail! The OTP is invalid or has been used before.
|
||||
log.Println("The given OTP is invalid!!!")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
**Do not verify HTTPS certificate:**
|
||||
```go
|
||||
// Disable HTTPS cert verification. Use true to enable again.
|
||||
yubiAuth.HttpsVerifyCertificate(false)
|
||||
```
|
||||
|
||||
|
||||
**HTTP instead of HTTPS:**
|
||||
```go
|
||||
// Disable HTTPS. Use true to enable again.
|
||||
yubiAuth.UseHttps(false)
|
||||
```
|
||||
|
||||
|
||||
**Custom API server:**
|
||||
```go
|
||||
// Set a list of n servers, each server as host + path.
|
||||
// Do not prepend with protocol
|
||||
yubiAuth.SetApiServerList("api0.server.com/api/verify", "api1.server.com/api/verify", "otherserver.com/api/verify")
|
||||
```
|
||||
|
||||
## Licence
|
||||
|
||||
This project is licensed under a Simplified BSD license. Please read the [LICENSE file][license].
|
||||
|
||||
|
||||
## Todo
|
||||
- Test files
|
||||
- More documentation
|
||||
- Getters/Setters for some options on the YubiAuth object.
|
||||
|
||||
## Protocol & Package documentation
|
||||
|
||||
This project is implementing a pure-Go Yubico OTP Validation Client and is following the [Yubico Validation Protocol Version 2.0][validationProtocolV20].
|
||||
|
||||
You will find "go doc"-like [package documentation at go.pkgdoc.org][pkgdoc].
|
||||
|
||||
|
||||
[license]: https://github.com/GeertJohan/yubigo/blob/master/LICENSE
|
||||
[getapikey]: https://upgrade.yubico.com/getapikey/
|
||||
[pkgdoc]: http://go.pkgdoc.org/github.com/GeertJohan/yubigo
|
||||
[validationProtocolV20]: http://code.google.com/p/yubikey-val-server-php/wiki/ValidationProtocolV20
|
565
vendor/github.com/GeertJohan/yubigo/yubigo.go
generated
vendored
Normal file
565
vendor/github.com/GeertJohan/yubigo/yubigo.go
generated
vendored
Normal file
|
@ -0,0 +1,565 @@
|
|||
package yubigo
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
dvorakToQwerty = strings.NewReplacer(
|
||||
"j", "c", "x", "b", "e", "d", ".", "e", "u", "f", "i", "g", "d", "h", "c", "i",
|
||||
"h", "j", "t", "k", "n", "l", "b", "n", "p", "r", "y", "t", "g", "u", "k", "v",
|
||||
"J", "C", "X", "B", "E", "D", ".", "E", "U", "F", "I", "G", "D", "H", "C", "I",
|
||||
"H", "J", "T", "K", "N", "L", "B", "N", "P", "R", "Y", "T", "G", "U", "K", "V")
|
||||
matchDvorak = regexp.MustCompile(`^[jxe.uidchtnbpygkJXE.UIDCHTNBPYGK]{32,48}$`)
|
||||
matchQwerty = regexp.MustCompile(`^[cbdefghijklnrtuvCBDEFGHIJKLNRTUV]{32,48}$`)
|
||||
signatureUrlFix = regexp.MustCompile(`\+`)
|
||||
)
|
||||
|
||||
// Package variable used to override the http client used for communication
|
||||
// with Yubico. If nil the standard http.Client will be used - if overriding
|
||||
// you need to ensure the transport options are set.
|
||||
var HTTPClient *http.Client = nil
|
||||
|
||||
// Parse and verify the given OTP string into prefix (identity) and ciphertext.
|
||||
// Function returns a non-nil error when given OTP is not in valid format.
|
||||
// NOTE: This function does NOT verify if the OTP is correct and unused/unique.
|
||||
func ParseOTP(otp string) (prefix string, ciphertext string, err error) {
|
||||
if len(otp) < 32 || len(otp) > 48 {
|
||||
err = errors.New("OTP has wrong length.")
|
||||
return
|
||||
}
|
||||
|
||||
// When otp matches dvorak-otp, then translate to qwerty.
|
||||
if matchDvorak.MatchString(otp) {
|
||||
otp = dvorakToQwerty.Replace(otp)
|
||||
}
|
||||
|
||||
// Verify that otp matches qwerty expectations
|
||||
if !matchQwerty.MatchString(otp) {
|
||||
err = errors.New("Given string is not a valid Yubikey OTP. It contains invalid characters and/or the length is wrong.")
|
||||
return
|
||||
}
|
||||
|
||||
l := len(otp)
|
||||
prefix = otp[0 : l-32]
|
||||
ciphertext = otp[l-32 : l]
|
||||
return
|
||||
}
|
||||
|
||||
type YubiAuth struct {
|
||||
id string
|
||||
key []byte
|
||||
apiServerList []string
|
||||
protocol string
|
||||
verifyCertificate bool
|
||||
workers []*verifyWorker
|
||||
use sync.Mutex
|
||||
debug bool
|
||||
}
|
||||
|
||||
type verifyWorker struct {
|
||||
ya *YubiAuth // YubiAuth this worker belongs to
|
||||
id int // Worker id
|
||||
client *http.Client // http client standing by ready for work
|
||||
apiServer string // API server URL
|
||||
work chan *workRequest // Channel on which the worker receives work
|
||||
stop chan bool // Channel for stop signal
|
||||
}
|
||||
|
||||
type workRequest struct {
|
||||
paramString *string
|
||||
resultChan chan *workResult
|
||||
}
|
||||
|
||||
type workResult struct {
|
||||
response *http.Response
|
||||
requestQuery string
|
||||
err error // indicates a failing server/network. This doesn't mean the OTP is invalid.
|
||||
}
|
||||
|
||||
func (vw *verifyWorker) process() {
|
||||
if vw.ya.debug {
|
||||
log.Printf("worker[%d]: Started.\n", vw.id)
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case w := <-vw.work:
|
||||
|
||||
// Create url
|
||||
url := vw.ya.protocol + vw.apiServer + *w.paramString
|
||||
|
||||
if vw.ya.debug {
|
||||
log.Printf("worker[%d]: Have work. Requesting: %s\n", vw.id, url)
|
||||
}
|
||||
|
||||
// Create request
|
||||
request, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
w.resultChan <- &workResult{
|
||||
response: nil,
|
||||
requestQuery: url,
|
||||
err: fmt.Errorf("Could not create http request. Error: %s\n", err),
|
||||
}
|
||||
continue
|
||||
}
|
||||
request.Header.Add("User-Agent", "github.com/GeertJohan/yubigo")
|
||||
|
||||
// Call server
|
||||
response, err := vw.client.Do(request)
|
||||
|
||||
// If we received an error from the client, return that (wrapped) on the channel.
|
||||
if err != nil {
|
||||
w.resultChan <- &workResult{
|
||||
response: nil,
|
||||
requestQuery: url,
|
||||
err: fmt.Errorf("Http client error: %s\n", err),
|
||||
}
|
||||
if vw.ya.debug {
|
||||
log.Printf("worker[%d]: Http client error: %s", vw.id, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// It seems everything is ok! return the response (wrapped) on the channel.
|
||||
if vw.ya.debug {
|
||||
log.Printf("worker[%d] Received result from api server. Sending on channel.", vw.id)
|
||||
}
|
||||
w.resultChan <- &workResult{
|
||||
response: response,
|
||||
requestQuery: url,
|
||||
err: nil,
|
||||
}
|
||||
continue
|
||||
case <-vw.stop:
|
||||
if vw.ya.debug {
|
||||
log.Printf("worker[%d]: received stop signal.\n", vw.id)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create a yubiAuth instance with given API-id and API-key.
|
||||
// Returns an error when the key could not be base64 decoded.
|
||||
// To use yubigo with the Yubico Web Service (default api servers), create an API id+key here: https://upgrade.yubico.com/getapikey/
|
||||
// Debugging is disabled. For debugging: use NewYubiAuthDebug(..)
|
||||
func NewYubiAuth(id string, key string) (auth *YubiAuth, err error) {
|
||||
return NewYubiAuthDebug(id, key, false)
|
||||
}
|
||||
|
||||
// Create a yubiAuth instance for given API-id and API-key.
|
||||
// Has third parameter `debug`. When debug is true this YubiAuth instance will spam the console with logging messages.
|
||||
// Returns an error when the key could not be base64 decoded.
|
||||
// To use yubigo with the Yubico Web Service (default api servers), create an API id+key here: https://upgrade.yubico.com/getapikey/
|
||||
func NewYubiAuthDebug(id string, key string, debug bool) (auth *YubiAuth, err error) {
|
||||
keyBytes, err := base64.StdEncoding.DecodeString(key)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Given key seems to be invalid. Could not base64_decode. Error: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("NewYubiAuthDebug: Given key is base64 decodable. Creating new YubiAuth instance with api id '%s'.\n", id)
|
||||
}
|
||||
|
||||
auth = &YubiAuth{
|
||||
id: id,
|
||||
key: keyBytes,
|
||||
|
||||
apiServerList: []string{"api.yubico.com/wsapi/2.0/verify",
|
||||
"api2.yubico.com/wsapi/2.0/verify",
|
||||
"api3.yubico.com/wsapi/2.0/verify",
|
||||
"api4.yubico.com/wsapi/2.0/verify",
|
||||
"api5.yubico.com/wsapi/2.0/verify"},
|
||||
|
||||
protocol: "https://",
|
||||
verifyCertificate: true,
|
||||
|
||||
debug: debug,
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("NewYubiAuthDebug: Using yubico web servers: %#v\n", auth.apiServerList)
|
||||
log.Println("NewYubiAuthDebug: Going to build workers.")
|
||||
}
|
||||
|
||||
// Build workers
|
||||
auth.buildWorkers()
|
||||
|
||||
// All done :)
|
||||
return
|
||||
}
|
||||
|
||||
// Stops existing workers and creates new ones.
|
||||
func (ya *YubiAuth) buildWorkers() {
|
||||
// Unexported (internal) method, so no locking.
|
||||
|
||||
// create tls config
|
||||
tlsConfig := &tls.Config{}
|
||||
if !ya.verifyCertificate {
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
// stop all existing workers
|
||||
for _, worker := range ya.workers {
|
||||
worker.stop <- true
|
||||
}
|
||||
|
||||
// create new (empty) slice with exact capacity
|
||||
ya.workers = make([]*verifyWorker, 0, len(ya.apiServerList))
|
||||
|
||||
// start new workers. One for each apiServerString
|
||||
for id, apiServer := range ya.apiServerList {
|
||||
// create worker instance with new http.Client instance
|
||||
worker := &verifyWorker{
|
||||
ya: ya,
|
||||
id: id,
|
||||
apiServer: apiServer + "?",
|
||||
work: make(chan *workRequest),
|
||||
stop: make(chan bool),
|
||||
}
|
||||
|
||||
if HTTPClient == nil {
|
||||
worker.client = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
worker.client = HTTPClient
|
||||
}
|
||||
|
||||
ya.workers = append(ya.workers, worker)
|
||||
|
||||
// start worker process in new goroutine
|
||||
go worker.process()
|
||||
}
|
||||
}
|
||||
|
||||
// Use this method to specify a list of servers for verification.
|
||||
// Each server string should contain host + path.
|
||||
// Example: "api.yubico.com/wsapi/2.0/verify".
|
||||
func (ya *YubiAuth) SetApiServerList(urls ...string) {
|
||||
// Lock
|
||||
ya.use.Lock()
|
||||
defer ya.use.Unlock()
|
||||
|
||||
// save setting
|
||||
ya.apiServerList = urls
|
||||
|
||||
// rebuild workers (api server url's have changed)
|
||||
ya.buildWorkers()
|
||||
}
|
||||
|
||||
// Retrieve the the ist of servers that are being used for verification.
|
||||
func (ya *YubiAuth) GetApiServerList() []string {
|
||||
return ya.apiServerList
|
||||
}
|
||||
|
||||
// Enable or disable the use of https
|
||||
func (ya *YubiAuth) UseHttps(useHttps bool) {
|
||||
// Lock
|
||||
ya.use.Lock()
|
||||
defer ya.use.Unlock()
|
||||
|
||||
// change setting
|
||||
if useHttps {
|
||||
ya.protocol = "https://"
|
||||
} else {
|
||||
ya.protocol = "http://"
|
||||
}
|
||||
|
||||
// no need to rebuild workers, they re-read ya.protocol on each request.
|
||||
}
|
||||
|
||||
// Enable or disable https certificate verification
|
||||
// Disable this at your own risk.
|
||||
func (ya *YubiAuth) HttpsVerifyCertificate(verifyCertificate bool) {
|
||||
// Lock
|
||||
ya.use.Lock()
|
||||
defer ya.use.Unlock()
|
||||
|
||||
// save setting
|
||||
ya.verifyCertificate = verifyCertificate
|
||||
|
||||
// rebuild workers (client has to be changed)
|
||||
ya.buildWorkers()
|
||||
}
|
||||
|
||||
// The verify method calls the API with given OTP and returns if the OTP is valid or not.
|
||||
// This method will return an error if something unexpected happens
|
||||
// If no error was returned, the returned 'ok bool' indicates if the OTP is valid
|
||||
// if the 'ok bool' is true, additional informtion can be found in the returned YubiResponse object
|
||||
func (ya *YubiAuth) Verify(otp string) (yr *YubiResponse, ok bool, err error) {
|
||||
// Lock
|
||||
ya.use.Lock()
|
||||
defer ya.use.Unlock()
|
||||
|
||||
// check the OTP
|
||||
_, _, err = ParseOTP(otp)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// create slice to store parameters for this verification request
|
||||
paramSlice := make([]string, 0)
|
||||
paramSlice = append(paramSlice, "id="+ya.id)
|
||||
paramSlice = append(paramSlice, "otp="+otp)
|
||||
|
||||
// Create 40 characters nonce
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
k := make([]rune, 40)
|
||||
for i := 0; i < 40; i++ {
|
||||
c := rand.Intn(35)
|
||||
if c < 10 {
|
||||
c += 48 // numbers (0-9) (0+48 == 48 == '0', 9+48 == 57 == '9')
|
||||
} else {
|
||||
c += 87 // lower case alphabets (a-z) (10+87 == 97 == 'a', 35+87 == 122 = 'z')
|
||||
}
|
||||
k[i] = rune(c)
|
||||
}
|
||||
nonce := string(k)
|
||||
paramSlice = append(paramSlice, "nonce="+nonce)
|
||||
|
||||
// These settings are hardcoded in the library for now.
|
||||
//++ TODO(GeertJohan): add these values to the yubiAuth object and create getters/setters
|
||||
// paramSlice = append(paramSlice, "timestamp=1")
|
||||
paramSlice = append(paramSlice, "sl=secure")
|
||||
|
||||
//++ TODO(GeertJohan): Add timeout support?
|
||||
//++ //paramSlice = append(paramSlice, "timeout=")
|
||||
|
||||
// sort the slice
|
||||
sort.Strings(paramSlice)
|
||||
|
||||
// create parameter string
|
||||
paramString := strings.Join(paramSlice, "&")
|
||||
|
||||
// generate signature
|
||||
if len(ya.key) > 0 {
|
||||
hmacenc := hmac.New(sha1.New, ya.key)
|
||||
_, err := hmacenc.Write([]byte(paramString))
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("Could not calculate signature. Error: %s\n", err)
|
||||
}
|
||||
signature := base64.StdEncoding.EncodeToString(hmacenc.Sum([]byte{}))
|
||||
signature = signatureUrlFix.ReplaceAllString(signature, `%2B`)
|
||||
paramString = paramString + "&h=" + signature
|
||||
}
|
||||
|
||||
// create result channel, buffersize equals the amount of workers.
|
||||
resultChan := make(chan *workResult, len(ya.workers))
|
||||
|
||||
// create workRequest instance
|
||||
wr := &workRequest{
|
||||
paramString: ¶mString,
|
||||
resultChan: resultChan,
|
||||
}
|
||||
|
||||
// send workRequest to each worker
|
||||
for _, worker := range ya.workers {
|
||||
worker.work <- wr
|
||||
}
|
||||
|
||||
// count the errors so we can handle when all servers fail (network fail for instance)
|
||||
errCount := 0
|
||||
|
||||
// local result var, will contain the first result we have
|
||||
var result *workResult
|
||||
|
||||
// keep looping until we have a good result
|
||||
for {
|
||||
// listen for result from a worker
|
||||
result = <-resultChan
|
||||
|
||||
// check for error
|
||||
if result.err != nil {
|
||||
// increment error counter
|
||||
errCount++
|
||||
|
||||
if ya.debug {
|
||||
// debug logging
|
||||
log.Printf("A server (%s) gave error back: %s\n", result.requestQuery, result.err)
|
||||
}
|
||||
|
||||
if errCount == len(ya.apiServerList) {
|
||||
// All workers are done, there's nothing left to try. we return an error.
|
||||
return nil, false, errors.New("None of the servers responded properly.")
|
||||
}
|
||||
|
||||
// we have an error, but not all workers responded yet, so lets wait for the next result.
|
||||
continue
|
||||
}
|
||||
|
||||
// create a yubiResult from the workers response.
|
||||
yr, err = newYubiResponse(result)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
// Check for "REPLAYED_REQUEST" result.
|
||||
if status, _ := yr.resultParameters["status"]; status == "REPLAYED_REQUEST" {
|
||||
// The result status is "REPLAYED_REQUEST".
|
||||
// This means that the server for this request got sync with an other server before our request.
|
||||
// Lets wait for the result from the other server.
|
||||
// See: http://forum.yubico.com/viewtopic.php?f=3&t=701
|
||||
|
||||
// increment error counter
|
||||
errCount++
|
||||
|
||||
if ya.debug {
|
||||
// debug logging
|
||||
log.Println("Got replayed request: ", result.response.Body)
|
||||
}
|
||||
|
||||
if errCount == len(ya.apiServerList) {
|
||||
// All workers are done, there' is nothing left to try. We return an error.
|
||||
return nil, false, errors.New("None of the servers responded properly.")
|
||||
}
|
||||
|
||||
// We have a replayed request, but not all workers responded yet, so lets wait for the next result.
|
||||
continue
|
||||
}
|
||||
|
||||
// No error or REPLAYED_REQUEST. Seems like we have a proper result.
|
||||
break
|
||||
}
|
||||
|
||||
// check status
|
||||
status, ok := yr.resultParameters["status"]
|
||||
if !ok || status != "OK" {
|
||||
switch status {
|
||||
case "BAD_OTP":
|
||||
return yr, false, nil
|
||||
case "REPLAYED_OTP":
|
||||
return yr, false, errors.New("The OTP is valid, but has been used before. If you receive this error, you might be the victim of a man-in-the-middle attack.")
|
||||
case "BAD_SIGNATURE":
|
||||
return yr, false, errors.New("Signature verification at the api server failed. The used id/key combination could be invalid or is not activated (yet).")
|
||||
case "NO_SUCH_CLIENT":
|
||||
return yr, false, errors.New("The api server does not accept the given id. It might be invalid or is not activated (yet).")
|
||||
case "OPERATION_NOT_ALLOWED":
|
||||
return yr, false, errors.New("The api server does not allow the given api id to verify OTPs.")
|
||||
case "BACKEND_ERROR":
|
||||
return yr, false, errors.New("The api server seems to be broken. Please contact the api servers system administration (yubico servers? contact yubico).")
|
||||
case "NOT_ENOUGH_ANSWERS":
|
||||
return yr, false, errors.New("The api server could not get requested number of syncs during before timeout")
|
||||
case "REPLAYED_REQUEST":
|
||||
panic("Unexpected. This status should've been catched in the worker response loop.")
|
||||
return yr, false, errors.New("The api server has seen this unique request before. If you receive this error, you might be the victim of a man-in-the-middle attack.")
|
||||
default:
|
||||
return yr, false, fmt.Errorf("Unknown status parameter (%s) sent by api server.", status)
|
||||
}
|
||||
}
|
||||
|
||||
// check otp
|
||||
otpCheck, ok := yr.resultParameters["otp"]
|
||||
if !ok || otp != otpCheck {
|
||||
return nil, false, errors.New("Could not validate otp value from server response.")
|
||||
}
|
||||
|
||||
// check nonce
|
||||
nonceCheck, ok := yr.resultParameters["nonce"]
|
||||
if !ok || nonce != nonceCheck {
|
||||
return nil, false, errors.New("Could not validate nonce value from server response.")
|
||||
}
|
||||
|
||||
// check attached signature with remake of that signature, if key is actually in use.
|
||||
if len(ya.key) > 0 {
|
||||
receivedSignature, ok := yr.resultParameters["h"]
|
||||
if !ok || len(receivedSignature) == 0 {
|
||||
return nil, false, errors.New("No signature hash was attached by the api server, we do expect one though. This might be a hacking attempt.")
|
||||
}
|
||||
|
||||
// create a slice with the same size-1 as the parameters map (we're leaving the hash itself out of it's replica calculation)
|
||||
receivedValuesSlice := make([]string, 0, len(yr.resultParameters)-1)
|
||||
for key, value := range yr.resultParameters {
|
||||
if key != "h" {
|
||||
receivedValuesSlice = append(receivedValuesSlice, key+"="+value)
|
||||
}
|
||||
}
|
||||
sort.Strings(receivedValuesSlice)
|
||||
receivedValuesString := strings.Join(receivedValuesSlice, "&")
|
||||
hmacenc := hmac.New(sha1.New, ya.key)
|
||||
_, err := hmacenc.Write([]byte(receivedValuesString))
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("Could not calculate signature replica. Error: %s\n", err)
|
||||
}
|
||||
recievedSignatureReplica := base64.StdEncoding.EncodeToString(hmacenc.Sum([]byte{}))
|
||||
|
||||
if receivedSignature != recievedSignatureReplica {
|
||||
return nil, false, errors.New("The received signature hash is not valid. This might be a hacking attempt.")
|
||||
}
|
||||
}
|
||||
|
||||
// we're done!
|
||||
yr.validOTP = true
|
||||
return yr, true, nil
|
||||
|
||||
}
|
||||
|
||||
// Contains details about yubikey OTP verification.
|
||||
type YubiResponse struct {
|
||||
requestQuery string
|
||||
resultParameters map[string]string
|
||||
validOTP bool
|
||||
}
|
||||
|
||||
func newYubiResponse(result *workResult) (*YubiResponse, error) {
|
||||
bodyReader := bufio.NewReader(result.response.Body)
|
||||
yr := &YubiResponse{}
|
||||
yr.resultParameters = make(map[string]string)
|
||||
yr.requestQuery = result.requestQuery
|
||||
for {
|
||||
// read through the response lines
|
||||
line, err := bodyReader.ReadString('\n')
|
||||
|
||||
// handle error, which at one point should be an expected io.EOF (end of file)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break // successfully done with reading lines, lets break this for loop
|
||||
}
|
||||
return nil, fmt.Errorf("Could not read result body from the server. Error: %s\n", err)
|
||||
}
|
||||
|
||||
// parse result lines, split on first '=', trim \n and \r
|
||||
keyvalue := strings.SplitN(line, "=", 2)
|
||||
if len(keyvalue) == 2 {
|
||||
yr.resultParameters[keyvalue[0]] = strings.Trim(keyvalue[1], "\n\r")
|
||||
}
|
||||
}
|
||||
return yr, nil
|
||||
}
|
||||
|
||||
// Returns wether the verification was successful
|
||||
func (yr *YubiResponse) IsValidOTP() bool {
|
||||
return yr.validOTP
|
||||
}
|
||||
|
||||
// Get the requestQuery that was used during verification.
|
||||
func (yr *YubiResponse) GetRequestQuery() string {
|
||||
return yr.requestQuery
|
||||
}
|
||||
|
||||
// Retrieve a parameter from the api's response
|
||||
func (yr *YubiResponse) GetResultParameter(key string) (value string) {
|
||||
value, ok := yr.resultParameters[key]
|
||||
if !ok {
|
||||
value = ""
|
||||
}
|
||||
return value
|
||||
}
|
7
vendor/github.com/Luzifer/go_helpers/.travis.yml
generated
vendored
Normal file
7
vendor/github.com/Luzifer/go_helpers/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.7
|
||||
- 1.8
|
||||
- 1.9
|
||||
- tip
|
43
vendor/github.com/Luzifer/go_helpers/History.md
generated
vendored
Normal file
43
vendor/github.com/Luzifer/go_helpers/History.md
generated
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
# 2.3.1 / 2017-11-05
|
||||
|
||||
* Fix TIP version error: Sprintf format %s has arg of wrong type byte
|
||||
* Travis: Test on Go 1.7, 1.8, 1.9, tip
|
||||
|
||||
# 2.3.0 / 2017-11-05
|
||||
|
||||
* Implement digest header generation
|
||||
|
||||
# 2.2.0 / 2017-04-13
|
||||
|
||||
* Add HTTPLogHandler
|
||||
|
||||
# 2.1.0 / 2016-12-23
|
||||
|
||||
* Add time.Duration formatter
|
||||
|
||||
# 2.0.0 / 2016-10-12
|
||||
|
||||
* Drop Go1.5 / Go1.6 support with using contexts
|
||||
* Add github-binary update helper
|
||||
|
||||
# 1.4.0 / 2016-05-29
|
||||
|
||||
* Added environment helpers
|
||||
|
||||
# 1.3.0 / 2016-05-18
|
||||
|
||||
* Added AccessLogResponseWriter
|
||||
|
||||
# 1.2.0 / 2016-05-16
|
||||
|
||||
* Added helper to find binaries in path or directory
|
||||
|
||||
# 1.1.0 / 2016-05-06
|
||||
|
||||
* Added Haversine helper functions
|
||||
|
||||
|
||||
1.0.0 / 2016-04-23
|
||||
==================
|
||||
|
||||
* First versioned revision for use with gopkg.in
|
37
vendor/github.com/Luzifer/go_helpers/accessLogger/accessLogger.go
generated
vendored
Normal file
37
vendor/github.com/Luzifer/go_helpers/accessLogger/accessLogger.go
generated
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
package accessLogger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AccessLogResponseWriter struct {
|
||||
StatusCode int
|
||||
Size int
|
||||
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func New(res http.ResponseWriter) *AccessLogResponseWriter {
|
||||
return &AccessLogResponseWriter{
|
||||
StatusCode: 200,
|
||||
Size: 0,
|
||||
ResponseWriter: res,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AccessLogResponseWriter) Write(out []byte) (int, error) {
|
||||
s, err := a.ResponseWriter.Write(out)
|
||||
a.Size += s
|
||||
return s, err
|
||||
}
|
||||
|
||||
func (a *AccessLogResponseWriter) WriteHeader(code int) {
|
||||
a.StatusCode = code
|
||||
a.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (a *AccessLogResponseWriter) HTTPResponseType() string {
|
||||
return fmt.Sprintf("%cxx", strconv.FormatInt(int64(a.StatusCode), 10)[0])
|
||||
}
|
61
vendor/github.com/Luzifer/go_helpers/duration/time.go
generated
vendored
Normal file
61
vendor/github.com/Luzifer/go_helpers/duration/time.go
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
package duration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/leekchan/gtf"
|
||||
)
|
||||
|
||||
const defaultDurationFormat = `{{if gt .Years 0}}{{.Years}} year{{.Years|pluralize "s"}}, {{end}}` +
|
||||
`{{if gt .Days 0}}{{.Days}} day{{.Days|pluralize "s"}}, {{end}}` +
|
||||
`{{if gt .Hours 0}}{{.Hours}} hour{{.Hours|pluralize "s"}}, {{end}}` +
|
||||
`{{if gt .Minutes 0}}{{.Minutes}} minute{{.Minutes|pluralize "s"}}, {{end}}` +
|
||||
`{{if gt .Seconds 0}}{{.Seconds}} second{{.Seconds|pluralize "s"}}{{end}}`
|
||||
|
||||
func HumanizeDuration(in time.Duration) string {
|
||||
f, err := CustomHumanizeDuration(in, defaultDurationFormat)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return strings.Trim(f, " ,")
|
||||
}
|
||||
|
||||
func CustomHumanizeDuration(in time.Duration, tpl string) (string, error) {
|
||||
result := struct{ Years, Days, Hours, Minutes, Seconds int64 }{}
|
||||
|
||||
in = time.Duration(math.Abs(float64(in)))
|
||||
|
||||
for in > 0 {
|
||||
switch {
|
||||
case in > 365.25*24*time.Hour:
|
||||
result.Years = int64(in / (365 * 24 * time.Hour))
|
||||
in = in - time.Duration(result.Years)*365*24*time.Hour
|
||||
case in > 24*time.Hour:
|
||||
result.Days = int64(in / (24 * time.Hour))
|
||||
in = in - time.Duration(result.Days)*24*time.Hour
|
||||
case in > time.Hour:
|
||||
result.Hours = int64(in / time.Hour)
|
||||
in = in - time.Duration(result.Hours)*time.Hour
|
||||
case in > time.Minute:
|
||||
result.Minutes = int64(in / time.Minute)
|
||||
in = in - time.Duration(result.Minutes)*time.Minute
|
||||
default:
|
||||
result.Seconds = int64(in / time.Second)
|
||||
in = 0
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.New("timeformat").Funcs(template.FuncMap(gtf.GtfFuncMap)).Parse(tpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
tmpl.Execute(buf, result)
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
35
vendor/github.com/Luzifer/go_helpers/duration/time_test.go
generated
vendored
Normal file
35
vendor/github.com/Luzifer/go_helpers/duration/time_test.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
package duration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCustomFormat(t *testing.T) {
|
||||
d := 389*24*time.Hour +
|
||||
12*time.Hour +
|
||||
31*time.Minute +
|
||||
54*time.Second +
|
||||
346*time.Millisecond
|
||||
|
||||
f := `{{.Years}} - {{.Days}} - {{.Hours}} - {{.Minutes}} - {{.Seconds}}`
|
||||
e := `1 - 24 - 12 - 31 - 54`
|
||||
|
||||
if s, _ := CustomHumanizeDuration(d, f); s != e {
|
||||
t.Errorf("Got unexpected result: expected=%q result=%q", e, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultFormat(t *testing.T) {
|
||||
d := 389*24*time.Hour +
|
||||
12*time.Hour +
|
||||
31*time.Minute +
|
||||
54*time.Second +
|
||||
346*time.Millisecond
|
||||
|
||||
e := `1 year, 24 days, 12 hours, 31 minutes, 54 seconds`
|
||||
|
||||
if s := HumanizeDuration(d); s != e {
|
||||
t.Errorf("Got unexpected result: expected=%q result=%q", e, s)
|
||||
}
|
||||
}
|
26
vendor/github.com/Luzifer/go_helpers/env/env.go
generated
vendored
Normal file
26
vendor/github.com/Luzifer/go_helpers/env/env.go
generated
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
package env
|
||||
|
||||
import "strings"
|
||||
|
||||
// ListToMap converts a list of strings in format KEY=VALUE into a map
|
||||
func ListToMap(list []string) map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, entry := range list {
|
||||
if len(entry) == 0 || entry[0] == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(entry, "=", 2)
|
||||
out[parts[0]] = strings.Trim(parts[1], "\"")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// MapToList converts a map into a list of strings in format KEY=VALUE
|
||||
func MapToList(envMap map[string]string) []string {
|
||||
out := []string{}
|
||||
for k, v := range envMap {
|
||||
out = append(out, k+"="+v)
|
||||
}
|
||||
return out
|
||||
}
|
13
vendor/github.com/Luzifer/go_helpers/env/env_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/go_helpers/env/env_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package env_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnv(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Env Suite")
|
||||
}
|
55
vendor/github.com/Luzifer/go_helpers/env/env_test.go
generated
vendored
Normal file
55
vendor/github.com/Luzifer/go_helpers/env/env_test.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
package env_test
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
. "github.com/Luzifer/go_helpers/env"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Env", func() {
|
||||
|
||||
Context("ListToMap", func() {
|
||||
var (
|
||||
list = []string{
|
||||
"FIRST_KEY=firstvalue",
|
||||
"SECOND_KEY=secondvalue",
|
||||
"WEIRD=",
|
||||
"",
|
||||
}
|
||||
emap = map[string]string{
|
||||
"FIRST_KEY": "firstvalue",
|
||||
"SECOND_KEY": "secondvalue",
|
||||
"WEIRD": "",
|
||||
}
|
||||
)
|
||||
|
||||
It("should convert the list in the expected way", func() {
|
||||
Expect(ListToMap(list)).To(Equal(emap))
|
||||
})
|
||||
})
|
||||
|
||||
Context("MapToList", func() {
|
||||
var (
|
||||
list = []string{
|
||||
"FIRST_KEY=firstvalue",
|
||||
"SECOND_KEY=secondvalue",
|
||||
"WEIRD=",
|
||||
}
|
||||
emap = map[string]string{
|
||||
"FIRST_KEY": "firstvalue",
|
||||
"SECOND_KEY": "secondvalue",
|
||||
"WEIRD": "",
|
||||
}
|
||||
)
|
||||
|
||||
It("should convert the map in the expected way", func() {
|
||||
l := MapToList(emap)
|
||||
sort.Strings(l) // Workaround: The test needs the elements to be in same order
|
||||
Expect(l).To(Equal(list))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
13
vendor/github.com/Luzifer/go_helpers/float/float_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/go_helpers/float/float_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package float_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFloat(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Float Suite")
|
||||
}
|
14
vendor/github.com/Luzifer/go_helpers/float/round.go
generated
vendored
Normal file
14
vendor/github.com/Luzifer/go_helpers/float/round.go
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
package float
|
||||
|
||||
import "math"
|
||||
|
||||
// Round returns a float rounded according to "Round to nearest, ties away from zero" IEEE floaing point rounding rule
|
||||
func Round(x float64) float64 {
|
||||
var absx, y float64
|
||||
absx = math.Abs(x)
|
||||
y = math.Floor(absx)
|
||||
if absx-y >= 0.5 {
|
||||
y += 1.0
|
||||
}
|
||||
return math.Copysign(y, x)
|
||||
}
|
35
vendor/github.com/Luzifer/go_helpers/float/round_test.go
generated
vendored
Normal file
35
vendor/github.com/Luzifer/go_helpers/float/round_test.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
package float_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
. "github.com/Luzifer/go_helpers/float"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Round", func() {
|
||||
|
||||
It("should match the example table of IEEE 754 rules", func() {
|
||||
Expect(Round(11.5)).To(Equal(12.0))
|
||||
Expect(Round(12.5)).To(Equal(13.0))
|
||||
Expect(Round(-11.5)).To(Equal(-12.0))
|
||||
Expect(Round(-12.5)).To(Equal(-13.0))
|
||||
})
|
||||
|
||||
It("should have correct rounding for numbers near 0.5", func() {
|
||||
Expect(Round(0.499999999997)).To(Equal(0.0))
|
||||
Expect(Round(-0.499999999997)).To(Equal(0.0))
|
||||
})
|
||||
|
||||
It("should be able to handle +/-Inf", func() {
|
||||
Expect(Round(math.Inf(1))).To(Equal(math.Inf(1)))
|
||||
Expect(Round(math.Inf(-1))).To(Equal(math.Inf(-1)))
|
||||
})
|
||||
|
||||
It("should be able to handle NaN", func() {
|
||||
Expect(math.IsNaN(Round(math.NaN()))).To(Equal(true))
|
||||
})
|
||||
|
||||
})
|
213
vendor/github.com/Luzifer/go_helpers/github/updater.go
generated
vendored
Normal file
213
vendor/github.com/Luzifer/go_helpers/github/updater.go
generated
vendored
Normal file
|
@ -0,0 +1,213 @@
|
|||
package github
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
update "github.com/inconshreveable/go-update"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 60 * time.Second
|
||||
defaultNamingScheme = `{{.ProductName}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}`
|
||||
)
|
||||
|
||||
var (
|
||||
errReleaseNotFound = errors.New("Release not found")
|
||||
)
|
||||
|
||||
// Updater is the core struct of the update library holding all configurations
|
||||
type Updater struct {
|
||||
repo string
|
||||
myVersion string
|
||||
|
||||
HTTPClient *http.Client
|
||||
RequestTimeout time.Duration
|
||||
Context context.Context
|
||||
Filename string
|
||||
|
||||
releaseCache string
|
||||
}
|
||||
|
||||
// NewUpdater initializes a new Updater and tries to guess the Filename
|
||||
func NewUpdater(repo, myVersion string) (*Updater, error) {
|
||||
var err error
|
||||
u := &Updater{
|
||||
repo: repo,
|
||||
myVersion: myVersion,
|
||||
|
||||
HTTPClient: http.DefaultClient,
|
||||
RequestTimeout: defaultTimeout,
|
||||
Context: context.Background(),
|
||||
}
|
||||
|
||||
u.Filename, err = u.compileFilename()
|
||||
|
||||
return u, err
|
||||
}
|
||||
|
||||
// HasUpdate checks which tag was used in the latest version and compares it to the current version. If it differs the function will return true. No comparison is done to determine whether the found version is higher than the current one.
|
||||
func (u *Updater) HasUpdate(forceRefresh bool) (bool, error) {
|
||||
if forceRefresh {
|
||||
u.releaseCache = ""
|
||||
}
|
||||
|
||||
latest, err := u.getLatestRelease()
|
||||
switch err {
|
||||
case nil:
|
||||
return u.myVersion != latest, nil
|
||||
case errReleaseNotFound:
|
||||
return false, nil
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// Apply downloads the new binary from Github, fetches the SHA256 sum from the SHA256SUMS file and applies the update to the currently running binary
|
||||
func (u *Updater) Apply() error {
|
||||
updateAvailable, err := u.HasUpdate(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !updateAvailable {
|
||||
return nil
|
||||
}
|
||||
|
||||
checksum, err := u.getSHA256(u.Filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newRelease, err := u.getFile(u.Filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer newRelease.Close()
|
||||
|
||||
return update.Apply(newRelease, update.Options{
|
||||
Checksum: checksum,
|
||||
})
|
||||
}
|
||||
|
||||
func (u Updater) getSHA256(filename string) ([]byte, error) {
|
||||
shaFile, err := u.getFile("SHA256SUMS")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer shaFile.Close()
|
||||
|
||||
scanner := bufio.NewScanner(shaFile)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if !strings.Contains(line, u.Filename) {
|
||||
continue
|
||||
}
|
||||
|
||||
return hex.DecodeString(line[0:64])
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("No SHA256 found for file %q", u.Filename)
|
||||
}
|
||||
|
||||
func (u Updater) getFile(filename string) (io.ReadCloser, error) {
|
||||
release, err := u.getLatestRelease()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requestURL := fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", u.repo, release, filename)
|
||||
req, err := http.NewRequest("GET", requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, _ := context.WithTimeout(u.Context, u.RequestTimeout)
|
||||
|
||||
res, err := u.HTTPClient.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("File not found: %q", requestURL)
|
||||
}
|
||||
|
||||
return res.Body, nil
|
||||
}
|
||||
|
||||
func (u *Updater) getLatestRelease() (string, error) {
|
||||
if u.releaseCache != "" {
|
||||
return u.releaseCache, nil
|
||||
}
|
||||
|
||||
result := struct {
|
||||
TagName string `json:"tag_name"`
|
||||
}{}
|
||||
|
||||
requestURL := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", u.repo)
|
||||
req, err := http.NewRequest("GET", requestURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(u.Context, u.RequestTimeout)
|
||||
defer cancel()
|
||||
|
||||
res, err := u.HTTPClient.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if err = json.NewDecoder(res.Body).Decode(&result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if res.StatusCode != 200 || result.TagName == "" {
|
||||
return "", errReleaseNotFound
|
||||
}
|
||||
|
||||
u.releaseCache = result.TagName
|
||||
|
||||
return result.TagName, nil
|
||||
}
|
||||
|
||||
func (u Updater) compileFilename() (string, error) {
|
||||
repoName := strings.Split(u.repo, "/")
|
||||
if len(repoName) != 2 {
|
||||
return "", errors.New("Repository name not in format <owner>/<repository>")
|
||||
}
|
||||
|
||||
tpl, err := template.New("filename").Parse(defaultNamingScheme)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var ext string
|
||||
if runtime.GOOS == "windows" {
|
||||
ext = ".exe"
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
if err = tpl.Execute(buf, map[string]interface{}{
|
||||
"GOOS": runtime.GOOS,
|
||||
"GOARCH": runtime.GOARCH,
|
||||
"EXT": ext,
|
||||
"ProductName": repoName[1],
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
55
vendor/github.com/Luzifer/go_helpers/http/digest.go
generated
vendored
Normal file
55
vendor/github.com/Luzifer/go_helpers/http/digest.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Luzifer/go_helpers/str"
|
||||
)
|
||||
|
||||
func GetDigestAuth(resp *http.Response, method, requestPath, user, password string) string {
|
||||
params := map[string]string{}
|
||||
for _, part := range strings.Split(resp.Header.Get("Www-Authenticate"), " ") {
|
||||
if !strings.Contains(part, `="`) {
|
||||
continue
|
||||
}
|
||||
spl := strings.Split(strings.Trim(part, " ,"), "=")
|
||||
if !str.StringInSlice(spl[0], []string{"nonce", "realm", "qop"}) {
|
||||
continue
|
||||
}
|
||||
params[spl[0]] = strings.Trim(spl[1], `"`)
|
||||
}
|
||||
|
||||
b := make([]byte, 8)
|
||||
io.ReadFull(rand.Reader, b)
|
||||
|
||||
params["cnonce"] = fmt.Sprintf("%x", b)
|
||||
params["nc"] = "1"
|
||||
params["uri"] = requestPath
|
||||
params["username"] = user
|
||||
params["response"] = getMD5([]string{
|
||||
getMD5([]string{params["username"], params["realm"], password}),
|
||||
params["nonce"],
|
||||
params["nc"],
|
||||
params["cnonce"],
|
||||
params["qop"],
|
||||
getMD5([]string{method, requestPath}),
|
||||
})
|
||||
|
||||
authParts := []string{}
|
||||
for k, v := range params {
|
||||
authParts = append(authParts, fmt.Sprintf("%s=%q", k, v))
|
||||
}
|
||||
return "Digest " + strings.Join(authParts, ", ")
|
||||
}
|
||||
|
||||
func getMD5(in []string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(strings.Join(in, ":")))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
35
vendor/github.com/Luzifer/go_helpers/http/logHandler.go
generated
vendored
Normal file
35
vendor/github.com/Luzifer/go_helpers/http/logHandler.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Luzifer/go_helpers/accessLogger"
|
||||
)
|
||||
|
||||
type HTTPLogHandler struct {
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
func NewHTTPLogHandler(h http.Handler) http.Handler {
|
||||
return HTTPLogHandler{Handler: h}
|
||||
}
|
||||
|
||||
func (l HTTPLogHandler) ServeHTTP(res http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
ares := accessLogger.New(res)
|
||||
|
||||
l.Handler.ServeHTTP(ares, r)
|
||||
|
||||
log.Printf("%s - \"%s %s\" %d %d \"%s\" \"%s\" %s",
|
||||
r.RemoteAddr,
|
||||
r.Method,
|
||||
r.URL.Path,
|
||||
ares.StatusCode,
|
||||
ares.Size,
|
||||
r.Header.Get("Referer"),
|
||||
r.Header.Get("User-Agent"),
|
||||
time.Since(start),
|
||||
)
|
||||
}
|
21
vendor/github.com/Luzifer/go_helpers/position/haversine.go
generated
vendored
Normal file
21
vendor/github.com/Luzifer/go_helpers/position/haversine.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
package position
|
||||
|
||||
import "math"
|
||||
|
||||
const (
|
||||
earthRadius = float64(6371)
|
||||
)
|
||||
|
||||
func Haversine(lonFrom float64, latFrom float64, lonTo float64, latTo float64) (distance float64) {
|
||||
var deltaLat = (latTo - latFrom) * (math.Pi / 180)
|
||||
var deltaLon = (lonTo - lonFrom) * (math.Pi / 180)
|
||||
|
||||
var a = math.Sin(deltaLat/2)*math.Sin(deltaLat/2) +
|
||||
math.Cos(latFrom*(math.Pi/180))*math.Cos(latTo*(math.Pi/180))*
|
||||
math.Sin(deltaLon/2)*math.Sin(deltaLon/2)
|
||||
var c = 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
||||
|
||||
distance = earthRadius * c
|
||||
|
||||
return
|
||||
}
|
34
vendor/github.com/Luzifer/go_helpers/position/haversine_test.go
generated
vendored
Normal file
34
vendor/github.com/Luzifer/go_helpers/position/haversine_test.go
generated
vendored
Normal file
|
@ -0,0 +1,34 @@
|
|||
package position_test
|
||||
|
||||
import (
|
||||
. "github.com/Luzifer/go_helpers/position"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Haversine", func() {
|
||||
|
||||
var testCases = []struct {
|
||||
SourceLat float64
|
||||
SourceLon float64
|
||||
DestLat float64
|
||||
DestLon float64
|
||||
Distance float64
|
||||
}{
|
||||
{50.066389, -5.714722, 58.643889, -3.070000, 968.8535441168448},
|
||||
{50.063995, -5.609464, 53.553027, 9.993782, 1137.894906816002},
|
||||
{53.553027, 9.993782, 53.554528, 9.991357, 0.23133816528015647},
|
||||
{50, 9, 51, 9, 111.19492664455873},
|
||||
{0, 9, 0, 10, 111.19492664455873},
|
||||
{1, 0, -1, 0, 222.38985328911747},
|
||||
}
|
||||
|
||||
It("should have the documented distance", func() {
|
||||
for i := range testCases {
|
||||
tc := testCases[i]
|
||||
Expect(Haversine(tc.SourceLon, tc.SourceLat, tc.DestLon, tc.DestLat)).To(Equal(tc.Distance))
|
||||
}
|
||||
})
|
||||
|
||||
})
|
13
vendor/github.com/Luzifer/go_helpers/position/position_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/go_helpers/position/position_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package position_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPosition(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Position Suite")
|
||||
}
|
21
vendor/github.com/Luzifer/go_helpers/str/slice.go
generated
vendored
Normal file
21
vendor/github.com/Luzifer/go_helpers/str/slice.go
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
package str
|
||||
|
||||
// AppendIfMissing adds a string to a slice when it's not present yet
|
||||
func AppendIfMissing(slice []string, s string) []string {
|
||||
for _, e := range slice {
|
||||
if e == s {
|
||||
return slice
|
||||
}
|
||||
}
|
||||
return append(slice, s)
|
||||
}
|
||||
|
||||
// StringInSlice checks for the existence of a string in the slice
|
||||
func StringInSlice(a string, list []string) bool {
|
||||
for _, b := range list {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
52
vendor/github.com/Luzifer/go_helpers/str/slice_test.go
generated
vendored
Normal file
52
vendor/github.com/Luzifer/go_helpers/str/slice_test.go
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
package str_test
|
||||
|
||||
import (
|
||||
. "github.com/Luzifer/go_helpers/str"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Slice", func() {
|
||||
|
||||
Context("AppendIfMissing", func() {
|
||||
var sl = []string{
|
||||
"test1",
|
||||
"test2",
|
||||
"test3",
|
||||
}
|
||||
|
||||
It("should not append existing elements", func() {
|
||||
Expect(len(AppendIfMissing(sl, "test1"))).To(Equal(3))
|
||||
Expect(len(AppendIfMissing(sl, "test2"))).To(Equal(3))
|
||||
Expect(len(AppendIfMissing(sl, "test3"))).To(Equal(3))
|
||||
})
|
||||
|
||||
It("should append not existing elements", func() {
|
||||
Expect(len(AppendIfMissing(sl, "test4"))).To(Equal(4))
|
||||
Expect(len(AppendIfMissing(sl, "test5"))).To(Equal(4))
|
||||
Expect(len(AppendIfMissing(sl, "test6"))).To(Equal(4))
|
||||
})
|
||||
})
|
||||
|
||||
Context("StringInSlice", func() {
|
||||
var sl = []string{
|
||||
"test1",
|
||||
"test2",
|
||||
"test3",
|
||||
}
|
||||
|
||||
It("should find elements of slice", func() {
|
||||
Expect(StringInSlice("test1", sl)).To(Equal(true))
|
||||
Expect(StringInSlice("test2", sl)).To(Equal(true))
|
||||
Expect(StringInSlice("test3", sl)).To(Equal(true))
|
||||
})
|
||||
|
||||
It("should not find elements not in slice", func() {
|
||||
Expect(StringInSlice("test4", sl)).To(Equal(false))
|
||||
Expect(StringInSlice("test5", sl)).To(Equal(false))
|
||||
Expect(StringInSlice("test6", sl)).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
13
vendor/github.com/Luzifer/go_helpers/str/str_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/go_helpers/str/str_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package str_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStr(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Str Suite")
|
||||
}
|
54
vendor/github.com/Luzifer/go_helpers/which/which.go
generated
vendored
Normal file
54
vendor/github.com/Luzifer/go_helpers/which/which.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package which
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Common named errors to match in programs using this library
|
||||
var (
|
||||
ErrBinaryNotFound = errors.New("Requested binary was not found")
|
||||
ErrNoSearchSpecified = errors.New("You need to specify a binary to search")
|
||||
)
|
||||
|
||||
// FindInPath searches the specified binary in directories listed in $PATH and returns first match
|
||||
func FindInPath(binary string) (string, error) {
|
||||
pathEnv := os.Getenv("PATH")
|
||||
if len(pathEnv) == 0 {
|
||||
return "", errors.New("Found empty $PATH, not able to search $PATH")
|
||||
}
|
||||
|
||||
for _, part := range strings.Split(pathEnv, ":") {
|
||||
if len(part) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if found, err := FindInDirectory(binary, part); err != nil {
|
||||
return "", err
|
||||
} else if found {
|
||||
return path.Join(part, binary), nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", ErrBinaryNotFound
|
||||
}
|
||||
|
||||
// FindInDirectory checks whether the specified file is present in the directory
|
||||
func FindInDirectory(binary, directory string) (bool, error) {
|
||||
if len(binary) == 0 {
|
||||
return false, ErrNoSearchSpecified
|
||||
}
|
||||
|
||||
_, err := os.Stat(path.Join(directory, binary))
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
return true, nil
|
||||
case os.IsNotExist(err):
|
||||
return false, nil
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
}
|
13
vendor/github.com/Luzifer/go_helpers/which/which_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/go_helpers/which/which_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package which_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWhich(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Which Suite")
|
||||
}
|
63
vendor/github.com/Luzifer/go_helpers/which/which_test.go
generated
vendored
Normal file
63
vendor/github.com/Luzifer/go_helpers/which/which_test.go
generated
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
package which_test
|
||||
|
||||
import (
|
||||
. "github.com/Luzifer/go_helpers/which"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Which", func() {
|
||||
var (
|
||||
result string
|
||||
err error
|
||||
found bool
|
||||
)
|
||||
|
||||
Context("With a file available on linux systems", func() {
|
||||
BeforeEach(func() {
|
||||
found, err = FindInDirectory("bash", "/bin")
|
||||
})
|
||||
|
||||
It("should not have errored", func() {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
It("should have found the binary at /bin/bash", func() {
|
||||
Expect(found).To(BeTrue())
|
||||
})
|
||||
})
|
||||
|
||||
Context("Searching bash on the system", func() {
|
||||
BeforeEach(func() {
|
||||
result, err = FindInPath("bash")
|
||||
})
|
||||
|
||||
It("should not have errored", func() {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
It("should have a result", func() {
|
||||
Expect(len(result)).NotTo(Equal(0))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Searching a non existent file", func() {
|
||||
BeforeEach(func() {
|
||||
result, err = FindInPath("dfqoiwurgtqi3uegrds")
|
||||
})
|
||||
|
||||
It("should have errored", func() {
|
||||
Expect(err).To(Equal(ErrBinaryNotFound))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Searching an empty file", func() {
|
||||
BeforeEach(func() {
|
||||
result, err = FindInPath("")
|
||||
})
|
||||
|
||||
It("should have errored", func() {
|
||||
Expect(err).To(Equal(ErrNoSearchSpecified))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
8
vendor/github.com/Luzifer/rconfig/.travis.yml
generated
vendored
Normal file
8
vendor/github.com/Luzifer/rconfig/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.6
|
||||
- 1.7
|
||||
- tip
|
||||
|
||||
script: go test -v -race -cover ./...
|
9
vendor/github.com/Luzifer/rconfig/History.md
generated
vendored
Normal file
9
vendor/github.com/Luzifer/rconfig/History.md
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 1.2.0 / 2017-06-19
|
||||
|
||||
* Add ParseAndValidate method
|
||||
|
||||
# 1.1.0 / 2016-06-28
|
||||
|
||||
* Support time.Duration config parameters
|
||||
* Added goreportcard badge
|
||||
* Added testcase for using bool with ENV and default
|
13
vendor/github.com/Luzifer/rconfig/LICENSE
generated
vendored
Normal file
13
vendor/github.com/Luzifer/rconfig/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
Copyright 2015 Knut Ahlers <knut@ahlers.me>
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
87
vendor/github.com/Luzifer/rconfig/README.md
generated
vendored
Normal file
87
vendor/github.com/Luzifer/rconfig/README.md
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
[![Build Status](https://travis-ci.org/Luzifer/rconfig.svg?branch=master)](https://travis-ci.org/Luzifer/rconfig)
|
||||
[![License: Apache v2.0](https://badge.luzifer.io/v1/badge?color=5d79b5&title=license&text=Apache+v2.0)](http://www.apache.org/licenses/LICENSE-2.0)
|
||||
[![Documentation](https://badge.luzifer.io/v1/badge?title=godoc&text=reference)](https://godoc.org/github.com/Luzifer/rconfig)
|
||||
[![Go Report](http://goreportcard.com/badge/Luzifer/rconfig)](http://goreportcard.com/report/Luzifer/rconfig)
|
||||
|
||||
## Description
|
||||
|
||||
> Package rconfig implements a CLI configuration reader with struct-embedded defaults, environment variables and posix compatible flag parsing using the [pflag](https://github.com/spf13/pflag) library.
|
||||
|
||||
## Installation
|
||||
|
||||
Install by running:
|
||||
|
||||
```
|
||||
go get -u github.com/Luzifer/rconfig
|
||||
```
|
||||
|
||||
OR fetch a specific version:
|
||||
|
||||
```
|
||||
go get -u gopkg.in/luzifer/rconfig.v1
|
||||
```
|
||||
|
||||
Run tests by running:
|
||||
|
||||
```
|
||||
go test -v -race -cover github.com/Luzifer/rconfig
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
A very simple usecase is to just configure a struct inside the vars section of your `main.go` and to parse the commandline flags from the `main()` function:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Luzifer/rconfig"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg = struct {
|
||||
Username string `default:"unknown" flag:"user" description:"Your name"`
|
||||
Details struct {
|
||||
Age int `default:"25" flag:"age" env:"age" description:"Your age"`
|
||||
}
|
||||
}{}
|
||||
)
|
||||
|
||||
func main() {
|
||||
rconfig.Parse(&cfg)
|
||||
|
||||
fmt.Printf("Hello %s, happy birthday for your %dth birthday.",
|
||||
cfg.Username,
|
||||
cfg.Details.Age)
|
||||
}
|
||||
```
|
||||
|
||||
### Provide variable defaults by using a file
|
||||
|
||||
Given you have a file `~/.myapp.yml` containing some secrets or usernames (for the example below username is assumed to be "luzifer") as a default configuration for your application you can use this source code to load the defaults from that file using the `vardefault` tag in your configuration struct.
|
||||
|
||||
The order of the directives (lower number = higher precedence):
|
||||
|
||||
1. Flags provided in command line
|
||||
1. Environment variables
|
||||
1. Variable defaults (`vardefault` tag in the struct)
|
||||
1. `default` tag in the struct
|
||||
|
||||
```go
|
||||
var cfg = struct {
|
||||
Username string `vardefault:"username" flag:"username" description:"Your username"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
rconfig.SetVariableDefaults(rconfig.VarDefaultsFromYAMLFile("~/.myapp.yml"))
|
||||
rconfig.Parse(&cfg)
|
||||
|
||||
fmt.Printf("Username = %s", cfg.Username)
|
||||
// Output: Username = luzifer
|
||||
}
|
||||
```
|
||||
|
||||
## More info
|
||||
|
||||
You can see the full reference documentation of the rconfig package [at godoc.org](https://godoc.org/github.com/Luzifer/rconfig), or through go's standard documentation system by running `godoc -http=:6060` and browsing to [http://localhost:6060/pkg/github.com/Luzifer/rconfig](http://localhost:6060/pkg/github.com/Luzifer/rconfig) after installation.
|
70
vendor/github.com/Luzifer/rconfig/bool_test.go
generated
vendored
Normal file
70
vendor/github.com/Luzifer/rconfig/bool_test.go
generated
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing bool parsing", func() {
|
||||
type t struct {
|
||||
Test1 bool `default:"true"`
|
||||
Test2 bool `default:"false" flag:"test2"`
|
||||
Test3 bool `default:"true" flag:"test3,t"`
|
||||
Test4 bool `flag:"test4"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--test2",
|
||||
"-t",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test1).To(Equal(true))
|
||||
Expect(cfg.Test2).To(Equal(true))
|
||||
Expect(cfg.Test3).To(Equal(true))
|
||||
Expect(cfg.Test4).To(Equal(false))
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("Testing to set bool from ENV with default", func() {
|
||||
type t struct {
|
||||
Test1 bool `default:"true" env:"TEST1"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
os.Unsetenv("TEST1")
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test1).To(Equal(true))
|
||||
})
|
||||
})
|
356
vendor/github.com/Luzifer/rconfig/config.go
generated
vendored
Normal file
356
vendor/github.com/Luzifer/rconfig/config.go
generated
vendored
Normal file
|
@ -0,0 +1,356 @@
|
|||
// Package rconfig implements a CLI configuration reader with struct-embedded
|
||||
// defaults, environment variables and posix compatible flag parsing using
|
||||
// the pflag library.
|
||||
package rconfig
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
validator "gopkg.in/validator.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
fs *pflag.FlagSet
|
||||
variableDefaults map[string]string
|
||||
)
|
||||
|
||||
func init() {
|
||||
variableDefaults = make(map[string]string)
|
||||
}
|
||||
|
||||
// Parse takes the pointer to a struct filled with variables which should be read
|
||||
// from ENV, default or flag. The precedence in this is flag > ENV > default. So
|
||||
// if a flag is specified on the CLI it will overwrite the ENV and otherwise ENV
|
||||
// overwrites the default specified.
|
||||
//
|
||||
// For your configuration struct you can use the following struct-tags to control
|
||||
// the behavior of rconfig:
|
||||
//
|
||||
// default: Set a default value
|
||||
// vardefault: Read the default value from the variable defaults
|
||||
// env: Read the value from this environment variable
|
||||
// flag: Flag to read in format "long,short" (for example "listen,l")
|
||||
// description: A help text for Usage output to guide your users
|
||||
//
|
||||
// The format you need to specify those values you can see in the example to this
|
||||
// function.
|
||||
//
|
||||
func Parse(config interface{}) error {
|
||||
return parse(config, nil)
|
||||
}
|
||||
|
||||
// ParseAndValidate works exactly like Parse but implements an additional run of
|
||||
// the go-validator package on the configuration struct. Therefore additonal struct
|
||||
// tags are supported like described in the readme file of the go-validator package:
|
||||
//
|
||||
// https://github.com/go-validator/validator/tree/v2#usage
|
||||
func ParseAndValidate(config interface{}) error {
|
||||
return parseAndValidate(config, nil)
|
||||
}
|
||||
|
||||
// Args returns the non-flag command-line arguments.
|
||||
func Args() []string {
|
||||
return fs.Args()
|
||||
}
|
||||
|
||||
// Usage prints a basic usage with the corresponding defaults for the flags to
|
||||
// os.Stdout. The defaults are derived from the `default` struct-tag and the ENV.
|
||||
func Usage() {
|
||||
if fs != nil && fs.Parsed() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
}
|
||||
|
||||
// SetVariableDefaults presets the parser with a map of default values to be used
|
||||
// when specifying the vardefault tag
|
||||
func SetVariableDefaults(defaults map[string]string) {
|
||||
variableDefaults = defaults
|
||||
}
|
||||
|
||||
func parseAndValidate(in interface{}, args []string) error {
|
||||
if err := parse(in, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return validator.Validate(in)
|
||||
}
|
||||
|
||||
func parse(in interface{}, args []string) error {
|
||||
if args == nil {
|
||||
args = os.Args
|
||||
}
|
||||
|
||||
fs = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||
if err := execTags(in, fs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fs.Parse(args)
|
||||
}
|
||||
|
||||
func execTags(in interface{}, fs *pflag.FlagSet) error {
|
||||
if reflect.TypeOf(in).Kind() != reflect.Ptr {
|
||||
return errors.New("Calling parser with non-pointer")
|
||||
}
|
||||
|
||||
if reflect.ValueOf(in).Elem().Kind() != reflect.Struct {
|
||||
return errors.New("Calling parser with pointer to non-struct")
|
||||
}
|
||||
|
||||
st := reflect.ValueOf(in).Elem()
|
||||
for i := 0; i < st.NumField(); i++ {
|
||||
valField := st.Field(i)
|
||||
typeField := st.Type().Field(i)
|
||||
|
||||
if typeField.Tag.Get("default") == "" && typeField.Tag.Get("env") == "" && typeField.Tag.Get("flag") == "" && typeField.Type.Kind() != reflect.Struct {
|
||||
// None of our supported tags is present and it's not a sub-struct
|
||||
continue
|
||||
}
|
||||
|
||||
value := varDefault(typeField.Tag.Get("vardefault"), typeField.Tag.Get("default"))
|
||||
value = envDefault(typeField.Tag.Get("env"), value)
|
||||
parts := strings.Split(typeField.Tag.Get("flag"), ",")
|
||||
|
||||
switch typeField.Type {
|
||||
case reflect.TypeOf(time.Duration(0)):
|
||||
v, err := time.ParseDuration(value)
|
||||
if err != nil {
|
||||
if value == "" {
|
||||
v = time.Duration(0)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
if len(parts) == 1 {
|
||||
fs.DurationVar(valField.Addr().Interface().(*time.Duration), parts[0], v, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
fs.DurationVarP(valField.Addr().Interface().(*time.Duration), parts[0], parts[1], v, typeField.Tag.Get("description"))
|
||||
}
|
||||
} else {
|
||||
valField.Set(reflect.ValueOf(v))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
switch typeField.Type.Kind() {
|
||||
case reflect.String:
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
if len(parts) == 1 {
|
||||
fs.StringVar(valField.Addr().Interface().(*string), parts[0], value, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
fs.StringVarP(valField.Addr().Interface().(*string), parts[0], parts[1], value, typeField.Tag.Get("description"))
|
||||
}
|
||||
} else {
|
||||
valField.SetString(value)
|
||||
}
|
||||
|
||||
case reflect.Bool:
|
||||
v := value == "true"
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
if len(parts) == 1 {
|
||||
fs.BoolVar(valField.Addr().Interface().(*bool), parts[0], v, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
fs.BoolVarP(valField.Addr().Interface().(*bool), parts[0], parts[1], v, typeField.Tag.Get("description"))
|
||||
}
|
||||
} else {
|
||||
valField.SetBool(v)
|
||||
}
|
||||
|
||||
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
|
||||
vt, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
if value == "" {
|
||||
vt = 0
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
registerFlagInt(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
valField.SetInt(vt)
|
||||
}
|
||||
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
vt, err := strconv.ParseUint(value, 10, 64)
|
||||
if err != nil {
|
||||
if value == "" {
|
||||
vt = 0
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
registerFlagUint(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
valField.SetUint(vt)
|
||||
}
|
||||
|
||||
case reflect.Float32, reflect.Float64:
|
||||
vt, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
if value == "" {
|
||||
vt = 0.0
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if typeField.Tag.Get("flag") != "" {
|
||||
registerFlagFloat(typeField.Type.Kind(), fs, valField.Addr().Interface(), parts, vt, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
valField.SetFloat(vt)
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
if err := execTags(valField.Addr().Interface(), fs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
switch typeField.Type.Elem().Kind() {
|
||||
case reflect.Int:
|
||||
def := []int{}
|
||||
for _, v := range strings.Split(value, ",") {
|
||||
it, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
def = append(def, int(it))
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
fs.IntSliceVar(valField.Addr().Interface().(*[]int), parts[0], def, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
fs.IntSliceVarP(valField.Addr().Interface().(*[]int), parts[0], parts[1], def, typeField.Tag.Get("description"))
|
||||
}
|
||||
case reflect.String:
|
||||
del := typeField.Tag.Get("delimiter")
|
||||
if len(del) == 0 {
|
||||
del = ","
|
||||
}
|
||||
def := strings.Split(value, del)
|
||||
if len(parts) == 1 {
|
||||
fs.StringSliceVar(valField.Addr().Interface().(*[]string), parts[0], def, typeField.Tag.Get("description"))
|
||||
} else {
|
||||
fs.StringSliceVarP(valField.Addr().Interface().(*[]string), parts[0], parts[1], def, typeField.Tag.Get("description"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func registerFlagFloat(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts []string, vt float64, desc string) {
|
||||
switch t {
|
||||
case reflect.Float32:
|
||||
if len(parts) == 1 {
|
||||
fs.Float32Var(field.(*float32), parts[0], float32(vt), desc)
|
||||
} else {
|
||||
fs.Float32VarP(field.(*float32), parts[0], parts[1], float32(vt), desc)
|
||||
}
|
||||
case reflect.Float64:
|
||||
if len(parts) == 1 {
|
||||
fs.Float64Var(field.(*float64), parts[0], float64(vt), desc)
|
||||
} else {
|
||||
fs.Float64VarP(field.(*float64), parts[0], parts[1], float64(vt), desc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts []string, vt int64, desc string) {
|
||||
switch t {
|
||||
case reflect.Int:
|
||||
if len(parts) == 1 {
|
||||
fs.IntVar(field.(*int), parts[0], int(vt), desc)
|
||||
} else {
|
||||
fs.IntVarP(field.(*int), parts[0], parts[1], int(vt), desc)
|
||||
}
|
||||
case reflect.Int8:
|
||||
if len(parts) == 1 {
|
||||
fs.Int8Var(field.(*int8), parts[0], int8(vt), desc)
|
||||
} else {
|
||||
fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc)
|
||||
}
|
||||
case reflect.Int32:
|
||||
if len(parts) == 1 {
|
||||
fs.Int32Var(field.(*int32), parts[0], int32(vt), desc)
|
||||
} else {
|
||||
fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc)
|
||||
}
|
||||
case reflect.Int64:
|
||||
if len(parts) == 1 {
|
||||
fs.Int64Var(field.(*int64), parts[0], int64(vt), desc)
|
||||
} else {
|
||||
fs.Int64VarP(field.(*int64), parts[0], parts[1], int64(vt), desc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func registerFlagUint(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts []string, vt uint64, desc string) {
|
||||
switch t {
|
||||
case reflect.Uint:
|
||||
if len(parts) == 1 {
|
||||
fs.UintVar(field.(*uint), parts[0], uint(vt), desc)
|
||||
} else {
|
||||
fs.UintVarP(field.(*uint), parts[0], parts[1], uint(vt), desc)
|
||||
}
|
||||
case reflect.Uint8:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint8Var(field.(*uint8), parts[0], uint8(vt), desc)
|
||||
} else {
|
||||
fs.Uint8VarP(field.(*uint8), parts[0], parts[1], uint8(vt), desc)
|
||||
}
|
||||
case reflect.Uint16:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint16Var(field.(*uint16), parts[0], uint16(vt), desc)
|
||||
} else {
|
||||
fs.Uint16VarP(field.(*uint16), parts[0], parts[1], uint16(vt), desc)
|
||||
}
|
||||
case reflect.Uint32:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint32Var(field.(*uint32), parts[0], uint32(vt), desc)
|
||||
} else {
|
||||
fs.Uint32VarP(field.(*uint32), parts[0], parts[1], uint32(vt), desc)
|
||||
}
|
||||
case reflect.Uint64:
|
||||
if len(parts) == 1 {
|
||||
fs.Uint64Var(field.(*uint64), parts[0], uint64(vt), desc)
|
||||
} else {
|
||||
fs.Uint64VarP(field.(*uint64), parts[0], parts[1], uint64(vt), desc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func envDefault(env, def string) string {
|
||||
value := def
|
||||
|
||||
if env != "" {
|
||||
if e := os.Getenv(env); e != "" {
|
||||
value = e
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func varDefault(name, def string) string {
|
||||
value := def
|
||||
|
||||
if name != "" {
|
||||
if v, ok := variableDefaults[name]; ok {
|
||||
value = v
|
||||
}
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
41
vendor/github.com/Luzifer/rconfig/duration_test.go
generated
vendored
Normal file
41
vendor/github.com/Luzifer/rconfig/duration_test.go
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Duration", func() {
|
||||
type t struct {
|
||||
Test time.Duration `flag:"duration"`
|
||||
TestS time.Duration `flag:"other-duration,o"`
|
||||
TestDef time.Duration `default:"30h"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--duration=23s", "-o", "45m",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(23 * time.Second))
|
||||
Expect(cfg.TestS).To(Equal(45 * time.Minute))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(30 * time.Hour))
|
||||
})
|
||||
})
|
56
vendor/github.com/Luzifer/rconfig/errors_test.go
generated
vendored
Normal file
56
vendor/github.com/Luzifer/rconfig/errors_test.go
generated
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing errors", func() {
|
||||
|
||||
It("should not accept string as int", func() {
|
||||
Expect(parse(&struct {
|
||||
A int `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as float", func() {
|
||||
Expect(parse(&struct {
|
||||
A float32 `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as uint", func() {
|
||||
Expect(parse(&struct {
|
||||
A uint `default:"a"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string as uint in sub-struct", func() {
|
||||
Expect(parse(&struct {
|
||||
B struct {
|
||||
A uint `default:"a"`
|
||||
}
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept string slice as int slice", func() {
|
||||
Expect(parse(&struct {
|
||||
A []int `default:"a,bn"`
|
||||
}{}, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept variables not being pointers", func() {
|
||||
cfg := struct {
|
||||
A string `default:"a"`
|
||||
}{}
|
||||
|
||||
Expect(parse(cfg, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should not accept variables not being pointers to structs", func() {
|
||||
cfg := "test"
|
||||
|
||||
Expect(parse(cfg, []string{})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
})
|
37
vendor/github.com/Luzifer/rconfig/example_test.go
generated
vendored
Normal file
37
vendor/github.com/Luzifer/rconfig/example_test.go
generated
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ExampleParse() {
|
||||
// We're building an example configuration with a sub-struct to be filled
|
||||
// by the Parse command.
|
||||
config := struct {
|
||||
Username string `default:"unknown" flag:"user,u" description:"Your name"`
|
||||
Details struct {
|
||||
Age int `default:"25" flag:"age" description:"Your age"`
|
||||
}
|
||||
}{}
|
||||
|
||||
// To have more relieable results we're setting os.Args to a known value.
|
||||
// In real-life use cases you wouldn't do this but parse the original
|
||||
// commandline arguments.
|
||||
os.Args = []string{
|
||||
"example",
|
||||
"--user=Luzifer",
|
||||
}
|
||||
|
||||
Parse(&config)
|
||||
|
||||
fmt.Printf("Hello %s, happy birthday for your %dth birthday.",
|
||||
config.Username,
|
||||
config.Details.Age)
|
||||
|
||||
// You can also show an usage message for your user
|
||||
Usage()
|
||||
|
||||
// Output:
|
||||
// Hello Luzifer, happy birthday for your 25th birthday.
|
||||
}
|
44
vendor/github.com/Luzifer/rconfig/float_test.go
generated
vendored
Normal file
44
vendor/github.com/Luzifer/rconfig/float_test.go
generated
vendored
Normal file
|
@ -0,0 +1,44 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing float parsing", func() {
|
||||
type t struct {
|
||||
Test32 float32 `flag:"float32"`
|
||||
Test32P float32 `flag:"float32p,3"`
|
||||
Test64 float64 `flag:"float64"`
|
||||
Test64P float64 `flag:"float64p,6"`
|
||||
TestDef float32 `default:"66.256"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--float32=5.5", "-3", "6.6",
|
||||
"--float64=7.7", "-6", "8.8",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test32).To(Equal(float32(5.5)))
|
||||
Expect(cfg.Test32P).To(Equal(float32(6.6)))
|
||||
Expect(cfg.Test64).To(Equal(float64(7.7)))
|
||||
Expect(cfg.Test64P).To(Equal(float64(8.8)))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(float32(66.256)))
|
||||
})
|
||||
})
|
128
vendor/github.com/Luzifer/rconfig/general_test.go
generated
vendored
Normal file
128
vendor/github.com/Luzifer/rconfig/general_test.go
generated
vendored
Normal file
|
@ -0,0 +1,128 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing general parsing", func() {
|
||||
type t struct {
|
||||
Test string `default:"foo" env:"shell" flag:"shell" description:"Test"`
|
||||
Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"`
|
||||
DefaultFlag string `default:"goo"`
|
||||
SadFlag string
|
||||
}
|
||||
|
||||
type tValidated struct {
|
||||
Test string `flag:"test" default:"" validate:"nonzero"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
Context("with defined arguments", func() {
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--shell=test23",
|
||||
"-t", "bla",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have parsed the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal("test23"))
|
||||
Expect(cfg.Test2).To(Equal("bla"))
|
||||
Expect(cfg.SadFlag).To(Equal(""))
|
||||
Expect(cfg.DefaultFlag).To(Equal("goo"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("with no arguments", func() {
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the default value", func() {
|
||||
Expect(cfg.Test).To(Equal("foo"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("with no arguments and set env", func() {
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
os.Setenv("shell", "test546")
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
os.Unsetenv("shell")
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the value from env", func() {
|
||||
Expect(cfg.Test).To(Equal("test546"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("with additional arguments", func() {
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--shell=test23",
|
||||
"-t", "bla",
|
||||
"positional1", "positional2",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have parsed the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal("test23"))
|
||||
Expect(cfg.Test2).To(Equal("bla"))
|
||||
Expect(cfg.SadFlag).To(Equal(""))
|
||||
Expect(cfg.DefaultFlag).To(Equal("goo"))
|
||||
})
|
||||
It("should have detected the positional arguments", func() {
|
||||
Expect(Args()).To(Equal([]string{"positional1", "positional2"}))
|
||||
})
|
||||
})
|
||||
|
||||
Context("making use of the validator package", func() {
|
||||
var cfgValidated tValidated
|
||||
|
||||
BeforeEach(func() {
|
||||
cfgValidated = tValidated{}
|
||||
args = []string{}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parseAndValidate(&cfgValidated, args)
|
||||
})
|
||||
|
||||
It("should have errored", func() { Expect(err).To(HaveOccurred()) })
|
||||
})
|
||||
|
||||
})
|
54
vendor/github.com/Luzifer/rconfig/int_test.go
generated
vendored
Normal file
54
vendor/github.com/Luzifer/rconfig/int_test.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing int parsing", func() {
|
||||
type t struct {
|
||||
Test int `flag:"int"`
|
||||
TestP int `flag:"intp,i"`
|
||||
Test8 int8 `flag:"int8"`
|
||||
Test8P int8 `flag:"int8p,8"`
|
||||
Test32 int32 `flag:"int32"`
|
||||
Test32P int32 `flag:"int32p,3"`
|
||||
Test64 int64 `flag:"int64"`
|
||||
Test64P int64 `flag:"int64p,6"`
|
||||
TestDef int8 `default:"66"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=1", "-i", "2",
|
||||
"--int8=3", "-8", "4",
|
||||
"--int32=5", "-3", "6",
|
||||
"--int64=7", "-6", "8",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(1))
|
||||
Expect(cfg.TestP).To(Equal(2))
|
||||
Expect(cfg.Test8).To(Equal(int8(3)))
|
||||
Expect(cfg.Test8P).To(Equal(int8(4)))
|
||||
Expect(cfg.Test32).To(Equal(int32(5)))
|
||||
Expect(cfg.Test32P).To(Equal(int32(6)))
|
||||
Expect(cfg.Test64).To(Equal(int64(7)))
|
||||
Expect(cfg.Test64P).To(Equal(int64(8)))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(int8(66)))
|
||||
})
|
||||
})
|
40
vendor/github.com/Luzifer/rconfig/os-args_test.go
generated
vendored
Normal file
40
vendor/github.com/Luzifer/rconfig/os-args_test.go
generated
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
package rconfig_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/Luzifer/rconfig"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing os.Args", func() {
|
||||
type t struct {
|
||||
A string `default:"a" flag:"a"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
cfg t
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = Parse(&cfg)
|
||||
})
|
||||
|
||||
Context("With only valid arguments", func() {
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
os.Args = []string{"--a=bar"}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.A).To(Equal("bar"))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
87
vendor/github.com/Luzifer/rconfig/precedence_test.go
generated
vendored
Normal file
87
vendor/github.com/Luzifer/rconfig/precedence_test.go
generated
vendored
Normal file
|
@ -0,0 +1,87 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Precedence", func() {
|
||||
|
||||
type t struct {
|
||||
A int `default:"1" vardefault:"a" env:"a" flag:"avar,a" description:"a"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
cfg t
|
||||
args []string
|
||||
vardefaults map[string]string
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
cfg = t{}
|
||||
SetVariableDefaults(vardefaults)
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
Context("Provided: Flag, Env, Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
args = []string{"-a", "5"}
|
||||
os.Setenv("a", "8")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the flag value", func() {
|
||||
Expect(cfg.A).To(Equal(5))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Provided: Env, Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
args = []string{}
|
||||
os.Setenv("a", "8")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the env value", func() {
|
||||
Expect(cfg.A).To(Equal(8))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Provided: Default, VarDefault", func() {
|
||||
BeforeEach(func() {
|
||||
args = []string{}
|
||||
os.Unsetenv("a")
|
||||
vardefaults = map[string]string{
|
||||
"a": "3",
|
||||
}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the vardefault value", func() {
|
||||
Expect(cfg.A).To(Equal(3))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Provided: Default", func() {
|
||||
BeforeEach(func() {
|
||||
args = []string{}
|
||||
os.Unsetenv("a")
|
||||
vardefaults = map[string]string{}
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have used the default value", func() {
|
||||
Expect(cfg.A).To(Equal(1))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
13
vendor/github.com/Luzifer/rconfig/rconfig_suite_test.go
generated
vendored
Normal file
13
vendor/github.com/Luzifer/rconfig/rconfig_suite_test.go
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
package rconfig_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRconfig(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Rconfig Suite")
|
||||
}
|
51
vendor/github.com/Luzifer/rconfig/slice_test.go
generated
vendored
Normal file
51
vendor/github.com/Luzifer/rconfig/slice_test.go
generated
vendored
Normal file
|
@ -0,0 +1,51 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing slices", func() {
|
||||
type t struct {
|
||||
Int []int `default:"1,2,3" flag:"int"`
|
||||
String []string `default:"a,b,c" flag:"string"`
|
||||
IntP []int `default:"1,2,3" flag:"intp,i"`
|
||||
StringP []string `default:"a,b,c" flag:"stringp,s"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=4,5", "-s", "hallo,welt",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values for int-slice", func() {
|
||||
Expect(len(cfg.Int)).To(Equal(2))
|
||||
Expect(cfg.Int).To(Equal([]int{4, 5}))
|
||||
Expect(cfg.Int).NotTo(Equal([]int{5, 4}))
|
||||
})
|
||||
It("should have the expected values for int-shorthand-slice", func() {
|
||||
Expect(len(cfg.IntP)).To(Equal(3))
|
||||
Expect(cfg.IntP).To(Equal([]int{1, 2, 3}))
|
||||
})
|
||||
It("should have the expected values for string-slice", func() {
|
||||
Expect(len(cfg.String)).To(Equal(3))
|
||||
Expect(cfg.String).To(Equal([]string{"a", "b", "c"}))
|
||||
})
|
||||
It("should have the expected values for string-shorthand-slice", func() {
|
||||
Expect(len(cfg.StringP)).To(Equal(2))
|
||||
Expect(cfg.StringP).To(Equal([]string{"hallo", "welt"}))
|
||||
})
|
||||
})
|
36
vendor/github.com/Luzifer/rconfig/sub-struct_test.go
generated
vendored
Normal file
36
vendor/github.com/Luzifer/rconfig/sub-struct_test.go
generated
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing sub-structs", func() {
|
||||
type t struct {
|
||||
Test string `default:"blubb"`
|
||||
Sub struct {
|
||||
Test string `default:"Hallo"`
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal("blubb"))
|
||||
Expect(cfg.Sub.Test).To(Equal("Hallo"))
|
||||
})
|
||||
})
|
59
vendor/github.com/Luzifer/rconfig/uint_test.go
generated
vendored
Normal file
59
vendor/github.com/Luzifer/rconfig/uint_test.go
generated
vendored
Normal file
|
@ -0,0 +1,59 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing uint parsing", func() {
|
||||
type t struct {
|
||||
Test uint `flag:"int"`
|
||||
TestP uint `flag:"intp,i"`
|
||||
Test8 uint8 `flag:"int8"`
|
||||
Test8P uint8 `flag:"int8p,8"`
|
||||
Test16 uint16 `flag:"int16"`
|
||||
Test16P uint16 `flag:"int16p,1"`
|
||||
Test32 uint32 `flag:"int32"`
|
||||
Test32P uint32 `flag:"int32p,3"`
|
||||
Test64 uint64 `flag:"int64"`
|
||||
Test64P uint64 `flag:"int64p,6"`
|
||||
TestDef uint8 `default:"66"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
args []string
|
||||
cfg t
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
args = []string{
|
||||
"--int=1", "-i", "2",
|
||||
"--int8=3", "-8", "4",
|
||||
"--int32=5", "-3", "6",
|
||||
"--int64=7", "-6", "8",
|
||||
"--int16=9", "-1", "10",
|
||||
}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.Test).To(Equal(uint(1)))
|
||||
Expect(cfg.TestP).To(Equal(uint(2)))
|
||||
Expect(cfg.Test8).To(Equal(uint8(3)))
|
||||
Expect(cfg.Test8P).To(Equal(uint8(4)))
|
||||
Expect(cfg.Test32).To(Equal(uint32(5)))
|
||||
Expect(cfg.Test32P).To(Equal(uint32(6)))
|
||||
Expect(cfg.Test64).To(Equal(uint64(7)))
|
||||
Expect(cfg.Test64P).To(Equal(uint64(8)))
|
||||
Expect(cfg.Test16).To(Equal(uint16(9)))
|
||||
Expect(cfg.Test16P).To(Equal(uint16(10)))
|
||||
|
||||
Expect(cfg.TestDef).To(Equal(uint8(66)))
|
||||
})
|
||||
})
|
27
vendor/github.com/Luzifer/rconfig/vardefault_providers.go
generated
vendored
Normal file
27
vendor/github.com/Luzifer/rconfig/vardefault_providers.go
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// VarDefaultsFromYAMLFile reads contents of a file and calls VarDefaultsFromYAML
|
||||
func VarDefaultsFromYAMLFile(filename string) map[string]string {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return make(map[string]string)
|
||||
}
|
||||
|
||||
return VarDefaultsFromYAML(data)
|
||||
}
|
||||
|
||||
// VarDefaultsFromYAML creates a vardefaults map from YAML raw data
|
||||
func VarDefaultsFromYAML(in []byte) map[string]string {
|
||||
out := make(map[string]string)
|
||||
err := yaml.Unmarshal(in, &out)
|
||||
if err != nil {
|
||||
return make(map[string]string)
|
||||
}
|
||||
return out
|
||||
}
|
122
vendor/github.com/Luzifer/rconfig/vardefault_test.go
generated
vendored
Normal file
122
vendor/github.com/Luzifer/rconfig/vardefault_test.go
generated
vendored
Normal file
|
@ -0,0 +1,122 @@
|
|||
package rconfig
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Testing variable defaults", func() {
|
||||
|
||||
type t struct {
|
||||
MySecretValue string `default:"secret" env:"foo" vardefault:"my_secret_value"`
|
||||
MyUsername string `default:"luzifer" vardefault:"username"`
|
||||
SomeVar string `flag:"var" description:"some variable"`
|
||||
IntVar int64 `vardefault:"int_var" default:"23"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
cfg t
|
||||
args = []string{}
|
||||
vardefaults = map[string]string{
|
||||
"my_secret_value": "veryverysecretkey",
|
||||
"unkownkey": "hi there",
|
||||
"int_var": "42",
|
||||
}
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
cfg = t{}
|
||||
})
|
||||
|
||||
JustBeforeEach(func() {
|
||||
err = parse(&cfg, args)
|
||||
})
|
||||
|
||||
Context("With manually provided variables", func() {
|
||||
BeforeEach(func() {
|
||||
SetVariableDefaults(vardefaults)
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.IntVar).To(Equal(int64(42)))
|
||||
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey"))
|
||||
Expect(cfg.MyUsername).To(Equal("luzifer"))
|
||||
Expect(cfg.SomeVar).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
Context("With defaults from YAML data", func() {
|
||||
BeforeEach(func() {
|
||||
yamlData := []byte("---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n")
|
||||
SetVariableDefaults(VarDefaultsFromYAML(yamlData))
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.IntVar).To(Equal(int64(42)))
|
||||
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey"))
|
||||
Expect(cfg.MyUsername).To(Equal("luzifer"))
|
||||
Expect(cfg.SomeVar).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
Context("With defaults from YAML file", func() {
|
||||
var tmp *os.File
|
||||
|
||||
BeforeEach(func() {
|
||||
tmp, _ = ioutil.TempFile("", "")
|
||||
yamlData := "---\nmy_secret_value: veryverysecretkey\nunknownkey: hi there\nint_var: 42\n"
|
||||
tmp.WriteString(yamlData)
|
||||
SetVariableDefaults(VarDefaultsFromYAMLFile(tmp.Name()))
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
tmp.Close()
|
||||
os.Remove(tmp.Name())
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.IntVar).To(Equal(int64(42)))
|
||||
Expect(cfg.MySecretValue).To(Equal("veryverysecretkey"))
|
||||
Expect(cfg.MyUsername).To(Equal("luzifer"))
|
||||
Expect(cfg.SomeVar).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
Context("With defaults from invalid YAML data", func() {
|
||||
BeforeEach(func() {
|
||||
yamlData := []byte("---\nmy_secret_value = veryverysecretkey\nunknownkey = hi there\nint_var = 42\n")
|
||||
SetVariableDefaults(VarDefaultsFromYAML(yamlData))
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.IntVar).To(Equal(int64(23)))
|
||||
Expect(cfg.MySecretValue).To(Equal("secret"))
|
||||
Expect(cfg.MyUsername).To(Equal("luzifer"))
|
||||
Expect(cfg.SomeVar).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
Context("With defaults from non existent YAML file", func() {
|
||||
BeforeEach(func() {
|
||||
file := "/tmp/this_file_should_not_exist_146e26723r"
|
||||
SetVariableDefaults(VarDefaultsFromYAMLFile(file))
|
||||
})
|
||||
|
||||
It("should not have errored", func() { Expect(err).NotTo(HaveOccurred()) })
|
||||
It("should have the expected values", func() {
|
||||
Expect(cfg.IntVar).To(Equal(int64(23)))
|
||||
Expect(cfg.MySecretValue).To(Equal("secret"))
|
||||
Expect(cfg.MyUsername).To(Equal("luzifer"))
|
||||
Expect(cfg.SomeVar).To(Equal(""))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
41
vendor/github.com/flosch/pongo2/.gitignore
generated
vendored
Normal file
41
vendor/github.com/flosch/pongo2/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
.idea
|
||||
.vscode
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
.project
|
||||
EBNF.txt
|
||||
test1.tpl
|
||||
pongo2_internal_test.go
|
||||
tpl-error.out
|
||||
/count.out
|
||||
/cover.out
|
||||
*.swp
|
||||
*.iml
|
||||
/cpu.out
|
||||
/mem.out
|
||||
/pongo2.test
|
||||
*.error
|
||||
/profile
|
||||
/coverage.out
|
||||
/pongo2_internal_test.ignore
|
13
vendor/github.com/flosch/pongo2/.travis.yml
generated
vendored
Normal file
13
vendor/github.com/flosch/pongo2/.travis.yml
generated
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.7
|
||||
- tip
|
||||
install:
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
- go get github.com/mattn/goveralls
|
||||
- go get gopkg.in/check.v1
|
||||
- go get github.com/juju/errors
|
||||
script:
|
||||
- go test -v -covermode=count -coverprofile=coverage.out -bench . -cpu 1,4
|
||||
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN || true'
|
10
vendor/github.com/flosch/pongo2/AUTHORS
generated
vendored
Normal file
10
vendor/github.com/flosch/pongo2/AUTHORS
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
Main author and maintainer of pongo2:
|
||||
|
||||
* Florian Schlachter <flori@n-schlachter.de>
|
||||
|
||||
Contributors (in no specific order):
|
||||
|
||||
* @romanoaugusto88
|
||||
* @vitalbh
|
||||
|
||||
Feel free to add yourself to the list or to modify your entry if you did a contribution.
|
20
vendor/github.com/flosch/pongo2/LICENSE
generated
vendored
Normal file
20
vendor/github.com/flosch/pongo2/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 Florian Schlachter
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
255
vendor/github.com/flosch/pongo2/README.md
generated
vendored
Normal file
255
vendor/github.com/flosch/pongo2/README.md
generated
vendored
Normal file
|
@ -0,0 +1,255 @@
|
|||
# [pongo](https://en.wikipedia.org/wiki/Pongo_%28genus%29)2
|
||||
|
||||
[![Join the chat at https://gitter.im/flosch/pongo2](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/flosch/pongo2)
|
||||
[![GoDoc](https://godoc.org/github.com/flosch/pongo2?status.svg)](https://godoc.org/github.com/flosch/pongo2)
|
||||
[![Build Status](https://travis-ci.org/flosch/pongo2.svg?branch=master)](https://travis-ci.org/flosch/pongo2)
|
||||
[![Coverage Status](https://coveralls.io/repos/flosch/pongo2/badge.svg?branch=master)](https://coveralls.io/r/flosch/pongo2?branch=master)
|
||||
[![gratipay](http://img.shields.io/badge/gratipay-support%20pongo-brightgreen.svg)](https://gratipay.com/flosch/)
|
||||
[![Bountysource](https://www.bountysource.com/badge/tracker?tracker_id=3654947)](https://www.bountysource.com/trackers/3654947-pongo2?utm_source=3654947&utm_medium=shield&utm_campaign=TRACKER_BADGE)
|
||||
|
||||
pongo2 is the successor of [pongo](https://github.com/flosch/pongo), a Django-syntax like templating-language.
|
||||
|
||||
Install/update using `go get` (no dependencies required by pongo2):
|
||||
```
|
||||
go get -u github.com/flosch/pongo2
|
||||
```
|
||||
|
||||
Please use the [issue tracker](https://github.com/flosch/pongo2/issues) if you're encountering any problems with pongo2 or if you need help with implementing tags or filters ([create a ticket!](https://github.com/flosch/pongo2/issues/new)). If possible, please use [playground](https://www.florian-schlachter.de/pongo2/) to create a short test case on what's wrong and include the link to the snippet in your issue.
|
||||
|
||||
**New**: [Try pongo2 out in the pongo2 playground.](https://www.florian-schlachter.de/pongo2/)
|
||||
|
||||
## First impression of a template
|
||||
|
||||
```HTML+Django
|
||||
<html><head><title>Our admins and users</title></head>
|
||||
{# This is a short example to give you a quick overview of pongo2's syntax. #}
|
||||
|
||||
{% macro user_details(user, is_admin=false) %}
|
||||
<div class="user_item">
|
||||
<!-- Let's indicate a user's good karma -->
|
||||
<h2 {% if (user.karma >= 40) || (user.karma > calc_avg_karma(userlist)+5) %}
|
||||
class="karma-good"{% endif %}>
|
||||
|
||||
<!-- This will call user.String() automatically if available: -->
|
||||
{{ user }}
|
||||
</h2>
|
||||
|
||||
<!-- Will print a human-readable time duration like "3 weeks ago" -->
|
||||
<p>This user registered {{ user.register_date|naturaltime }}.</p>
|
||||
|
||||
<!-- Let's allow the users to write down their biography using markdown;
|
||||
we will only show the first 15 words as a preview -->
|
||||
<p>The user's biography:</p>
|
||||
<p>{{ user.biography|markdown|truncatewords_html:15 }}
|
||||
<a href="/user/{{ user.id }}/">read more</a></p>
|
||||
|
||||
{% if is_admin %}<p>This user is an admin!</p>{% endif %}
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
<body>
|
||||
<!-- Make use of the macro defined above to avoid repetitive HTML code
|
||||
since we want to use the same code for admins AND members -->
|
||||
|
||||
<h1>Our admins</h1>
|
||||
{% for admin in adminlist %}
|
||||
{{ user_details(admin, true) }}
|
||||
{% endfor %}
|
||||
|
||||
<h1>Our members</h1>
|
||||
{% for user in userlist %}
|
||||
{{ user_details(user) }}
|
||||
{% endfor %}
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## Development status
|
||||
|
||||
**Latest stable release**: v3.0 (`go get -u gopkg.in/flosch/pongo2.v3` / [`v3`](https://github.com/flosch/pongo2/tree/v3)-branch) [[read the announcement](https://www.florian-schlachter.de/post/pongo2-v3/)]
|
||||
|
||||
**Current development**: v4 (`master`-branch)
|
||||
|
||||
*Note*: With the release of pongo v4 the branch v2 will be deprecated.
|
||||
|
||||
**Deprecated versions** (not supported anymore): v1
|
||||
|
||||
| Topic | Status |
|
||||
| ------------------------------------ | -------------------------------------------------------------------------------------- |
|
||||
| Django version compatibility: | [1.7](https://docs.djangoproject.com/en/1.7/ref/templates/builtins/) |
|
||||
| *Missing* (planned) **filters**: | none ([hints](https://github.com/flosch/pongo2/blob/master/filters_builtin.go#L3)) |
|
||||
| *Missing* (planned) **tags**: | none ([hints](https://github.com/flosch/pongo2/blob/master/tags.go#L3)) |
|
||||
|
||||
Please also have a look on the [caveats](https://github.com/flosch/pongo2#caveats) and on the [official add-ons](https://github.com/flosch/pongo2#official).
|
||||
|
||||
## Features (and new in pongo2)
|
||||
|
||||
* Entirely rewritten from the ground-up.
|
||||
* [Advanced C-like expressions](https://github.com/flosch/pongo2/blob/master/template_tests/expressions.tpl).
|
||||
* [Complex function calls within expressions](https://github.com/flosch/pongo2/blob/master/template_tests/function_calls_wrapper.tpl).
|
||||
* [Easy API to create new filters and tags](http://godoc.org/github.com/flosch/pongo2#RegisterFilter) ([including parsing arguments](http://godoc.org/github.com/flosch/pongo2#Parser))
|
||||
* Additional features:
|
||||
* Macros including importing macros from other files (see [template_tests/macro.tpl](https://github.com/flosch/pongo2/blob/master/template_tests/macro.tpl))
|
||||
* [Template sandboxing](https://godoc.org/github.com/flosch/pongo2#TemplateSet) ([directory patterns](http://golang.org/pkg/path/filepath/#Match), banned tags/filters)
|
||||
|
||||
## Recent API changes within pongo2
|
||||
|
||||
If you're using the `master`-branch of pongo2, you might be interested in this section. Since pongo2 is still in development (even though there is a first stable release!), there could be (backwards-incompatible) API changes over time. To keep track of these and therefore make it painless for you to adapt your codebase, I'll list them here.
|
||||
|
||||
* Function signature for tag execution changed: not taking a `bytes.Buffer` anymore; instead `Execute()`-functions are now taking a `TemplateWriter` interface.
|
||||
* Function signature for tag and filter parsing/execution changed (`error` return type changed to `*Error`).
|
||||
* `INodeEvaluator` has been removed and got replaced by `IEvaluator`. You can change your existing tags/filters by simply replacing the interface.
|
||||
* Two new helper functions: [`RenderTemplateFile()`](https://godoc.org/github.com/flosch/pongo2#RenderTemplateFile) and [`RenderTemplateString()`](https://godoc.org/github.com/flosch/pongo2#RenderTemplateString).
|
||||
* `Template.ExecuteRW()` is now [`Template.ExecuteWriter()`](https://godoc.org/github.com/flosch/pongo2#Template.ExecuteWriter)
|
||||
* `Template.Execute*()` functions do now take a `pongo2.Context` directly (no pointer anymore).
|
||||
|
||||
## How you can help
|
||||
|
||||
* Write [filters](https://github.com/flosch/pongo2/blob/master/filters_builtin.go#L3) / [tags](https://github.com/flosch/pongo2/blob/master/tags.go#L4) (see [tutorial](https://www.florian-schlachter.de/post/pongo2/)) by forking pongo2 and sending pull requests
|
||||
* Write/improve code tests (use the following command to see what tests are missing: `go test -v -cover -covermode=count -coverprofile=cover.out && go tool cover -html=cover.out` or have a look on [gocover.io/github.com/flosch/pongo2](http://gocover.io/github.com/flosch/pongo2))
|
||||
* Write/improve template tests (see the `template_tests/` directory)
|
||||
* Write middleware, libraries and websites using pongo2. :-)
|
||||
|
||||
# Documentation
|
||||
|
||||
For a documentation on how the templating language works you can [head over to the Django documentation](https://docs.djangoproject.com/en/dev/topics/templates/). pongo2 aims to be compatible with it.
|
||||
|
||||
You can access pongo2's API documentation on [godoc](https://godoc.org/github.com/flosch/pongo2).
|
||||
|
||||
## Blog post series
|
||||
|
||||
* [pongo2 v3 released](https://www.florian-schlachter.de/post/pongo2-v3/)
|
||||
* [pongo2 v2 released](https://www.florian-schlachter.de/post/pongo2-v2/)
|
||||
* [pongo2 1.0 released](https://www.florian-schlachter.de/post/pongo2-10/) [August 8th 2014]
|
||||
* [pongo2 playground](https://www.florian-schlachter.de/post/pongo2-playground/) [August 1st 2014]
|
||||
* [Release of pongo2 1.0-rc1 + pongo2-addons](https://www.florian-schlachter.de/post/pongo2-10-rc1/) [July 30th 2014]
|
||||
* [Introduction to pongo2 + migration- and "how to write tags/filters"-tutorial.](https://www.florian-schlachter.de/post/pongo2/) [June 29th 2014]
|
||||
|
||||
## Caveats
|
||||
|
||||
### Filters
|
||||
|
||||
* **date** / **time**: The `date` and `time` filter are taking the Golang specific time- and date-format (not Django's one) currently. [Take a look on the format here](http://golang.org/pkg/time/#Time.Format).
|
||||
* **stringformat**: `stringformat` does **not** take Python's string format syntax as a parameter, instead it takes Go's. Essentially `{{ 3.14|stringformat:"pi is %.2f" }}` is `fmt.Sprintf("pi is %.2f", 3.14)`.
|
||||
* **escape** / **force_escape**: Unlike Django's behaviour, the `escape`-filter is applied immediately. Therefore there is no need for a `force_escape`-filter yet.
|
||||
|
||||
### Tags
|
||||
|
||||
* **for**: All the `forloop` fields (like `forloop.counter`) are written with a capital letter at the beginning. For example, the `counter` can be accessed by `forloop.Counter` and the parentloop by `forloop.Parentloop`.
|
||||
* **now**: takes Go's time format (see **date** and **time**-filter).
|
||||
|
||||
### Misc
|
||||
|
||||
* **not in-operator**: You can check whether a map/struct/string contains a key/field/substring by using the in-operator (or the negation of it):
|
||||
`{% if key in map %}Key is in map{% else %}Key not in map{% endif %}` or `{% if !(key in map) %}Key is NOT in map{% else %}Key is in map{% endif %}`.
|
||||
|
||||
# Add-ons, libraries and helpers
|
||||
|
||||
## Official
|
||||
|
||||
* [ponginae](https://github.com/flosch/ponginae) - A web-framework for Go (using pongo2).
|
||||
* [pongo2-tools](https://github.com/flosch/pongo2-tools) - Official tools and helpers for pongo2
|
||||
* [pongo2-addons](https://github.com/flosch/pongo2-addons) - Official additional filters/tags for pongo2 (for example a **markdown**-filter). They are in their own repository because they're relying on 3rd-party-libraries.
|
||||
|
||||
## 3rd-party
|
||||
|
||||
* [beego-pongo2](https://github.com/oal/beego-pongo2) - A tiny little helper for using Pongo2 with [Beego](https://github.com/astaxie/beego).
|
||||
* [beego-pongo2.v2](https://github.com/ipfans/beego-pongo2.v2) - Same as `beego-pongo2`, but for pongo2 v2.
|
||||
* [macaron-pongo2](https://github.com/macaron-contrib/pongo2) - pongo2 support for [Macaron](https://github.com/Unknwon/macaron), a modular web framework.
|
||||
* [ginpongo2](https://github.com/ngerakines/ginpongo2) - middleware for [gin](github.com/gin-gonic/gin) to use pongo2 templates
|
||||
* [Build'n support for Iris' template engine](https://github.com/kataras/iris)
|
||||
* [pongo2gin](https://github.com/robvdl/pongo2gin) - alternative renderer for [gin](github.com/gin-gonic/gin) to use pongo2 templates
|
||||
* [pongo2-trans](https://github.com/digitalcrab/pongo2trans) - `trans`-tag implementation for internationalization
|
||||
* [tpongo2](https://github.com/tango-contrib/tpongo2) - pongo2 support for [Tango](https://github.com/lunny/tango), a micro-kernel & pluggable web framework.
|
||||
* [p2cli](https://github.com/wrouesnel/p2cli) - command line templating utility based on pongo2
|
||||
|
||||
Please add your project to this list and send me a pull request when you've developed something nice for pongo2.
|
||||
|
||||
# API-usage examples
|
||||
|
||||
Please see the documentation for a full list of provided API methods.
|
||||
|
||||
## A tiny example (template string)
|
||||
|
||||
```Go
|
||||
// Compile the template first (i. e. creating the AST)
|
||||
tpl, err := pongo2.FromString("Hello {{ name|capfirst }}!")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Now you can render the template with the given
|
||||
// pongo2.Context how often you want to.
|
||||
out, err := tpl.Execute(pongo2.Context{"name": "florian"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(out) // Output: Hello Florian!
|
||||
```
|
||||
|
||||
## Example server-usage (template file)
|
||||
|
||||
```Go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/flosch/pongo2"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Pre-compiling the templates at application startup using the
|
||||
// little Must()-helper function (Must() will panic if FromFile()
|
||||
// or FromString() will return with an error - that's it).
|
||||
// It's faster to pre-compile it anywhere at startup and only
|
||||
// execute the template later.
|
||||
var tplExample = pongo2.Must(pongo2.FromFile("example.html"))
|
||||
|
||||
func examplePage(w http.ResponseWriter, r *http.Request) {
|
||||
// Execute the template per HTTP request
|
||||
err := tplExample.ExecuteWriter(pongo2.Context{"query": r.FormValue("query")}, w)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", examplePage)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
```
|
||||
|
||||
# Benchmark
|
||||
|
||||
The benchmarks have been run on the my machine (`Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz`) using the command:
|
||||
|
||||
go test -bench . -cpu 1,2,4,8
|
||||
|
||||
All benchmarks are compiling (depends on the benchmark) and executing the `template_tests/complex.tpl` template.
|
||||
|
||||
The results are:
|
||||
|
||||
BenchmarkExecuteComplexWithSandboxActive 50000 60450 ns/op
|
||||
BenchmarkExecuteComplexWithSandboxActive-2 50000 56998 ns/op
|
||||
BenchmarkExecuteComplexWithSandboxActive-4 50000 60343 ns/op
|
||||
BenchmarkExecuteComplexWithSandboxActive-8 50000 64229 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithSandboxActive 10000 164410 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithSandboxActive-2 10000 156682 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithSandboxActive-4 10000 164821 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithSandboxActive-8 10000 171806 ns/op
|
||||
BenchmarkParallelExecuteComplexWithSandboxActive 50000 60428 ns/op
|
||||
BenchmarkParallelExecuteComplexWithSandboxActive-2 50000 31887 ns/op
|
||||
BenchmarkParallelExecuteComplexWithSandboxActive-4 100000 22810 ns/op
|
||||
BenchmarkParallelExecuteComplexWithSandboxActive-8 100000 18820 ns/op
|
||||
BenchmarkExecuteComplexWithoutSandbox 50000 56942 ns/op
|
||||
BenchmarkExecuteComplexWithoutSandbox-2 50000 56168 ns/op
|
||||
BenchmarkExecuteComplexWithoutSandbox-4 50000 57838 ns/op
|
||||
BenchmarkExecuteComplexWithoutSandbox-8 50000 60539 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithoutSandbox 10000 162086 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithoutSandbox-2 10000 159771 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithoutSandbox-4 10000 163826 ns/op
|
||||
BenchmarkCompileAndExecuteComplexWithoutSandbox-8 10000 169062 ns/op
|
||||
BenchmarkParallelExecuteComplexWithoutSandbox 50000 57152 ns/op
|
||||
BenchmarkParallelExecuteComplexWithoutSandbox-2 50000 30276 ns/op
|
||||
BenchmarkParallelExecuteComplexWithoutSandbox-4 100000 22065 ns/op
|
||||
BenchmarkParallelExecuteComplexWithoutSandbox-8 100000 18034 ns/op
|
||||
|
||||
Benchmarked on October 2nd 2014.
|
130
vendor/github.com/flosch/pongo2/context.go
generated
vendored
Normal file
130
vendor/github.com/flosch/pongo2/context.go
generated
vendored
Normal file
|
@ -0,0 +1,130 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
var reIdentifiers = regexp.MustCompile("^[a-zA-Z0-9_]+$")
|
||||
|
||||
// A Context type provides constants, variables, instances or functions to a template.
|
||||
//
|
||||
// pongo2 automatically provides meta-information or functions through the "pongo2"-key.
|
||||
// Currently, context["pongo2"] contains the following keys:
|
||||
// 1. version: returns the version string
|
||||
//
|
||||
// Template examples for accessing items from your context:
|
||||
// {{ myconstant }}
|
||||
// {{ myfunc("test", 42) }}
|
||||
// {{ user.name }}
|
||||
// {{ pongo2.version }}
|
||||
type Context map[string]interface{}
|
||||
|
||||
func (c Context) checkForValidIdentifiers() *Error {
|
||||
for k, v := range c {
|
||||
if !reIdentifiers.MatchString(k) {
|
||||
return &Error{
|
||||
Sender: "checkForValidIdentifiers",
|
||||
OrigError: errors.Errorf("context-key '%s' (value: '%+v') is not a valid identifier", k, v),
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update updates this context with the key/value-pairs from another context.
|
||||
func (c Context) Update(other Context) Context {
|
||||
for k, v := range other {
|
||||
c[k] = v
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// ExecutionContext contains all data important for the current rendering state.
|
||||
//
|
||||
// If you're writing a custom tag, your tag's Execute()-function will
|
||||
// have access to the ExecutionContext. This struct stores anything
|
||||
// about the current rendering process's Context including
|
||||
// the Context provided by the user (field Public).
|
||||
// You can safely use the Private context to provide data to the user's
|
||||
// template (like a 'forloop'-information). The Shared-context is used
|
||||
// to share data between tags. All ExecutionContexts share this context.
|
||||
//
|
||||
// Please be careful when accessing the Public data.
|
||||
// PLEASE DO NOT MODIFY THE PUBLIC CONTEXT (read-only).
|
||||
//
|
||||
// To create your own execution context within tags, use the
|
||||
// NewChildExecutionContext(parent) function.
|
||||
type ExecutionContext struct {
|
||||
template *Template
|
||||
|
||||
Autoescape bool
|
||||
Public Context
|
||||
Private Context
|
||||
Shared Context
|
||||
}
|
||||
|
||||
var pongo2MetaContext = Context{
|
||||
"version": Version,
|
||||
}
|
||||
|
||||
func newExecutionContext(tpl *Template, ctx Context) *ExecutionContext {
|
||||
privateCtx := make(Context)
|
||||
|
||||
// Make the pongo2-related funcs/vars available to the context
|
||||
privateCtx["pongo2"] = pongo2MetaContext
|
||||
|
||||
return &ExecutionContext{
|
||||
template: tpl,
|
||||
|
||||
Public: ctx,
|
||||
Private: privateCtx,
|
||||
Autoescape: true,
|
||||
}
|
||||
}
|
||||
|
||||
func NewChildExecutionContext(parent *ExecutionContext) *ExecutionContext {
|
||||
newctx := &ExecutionContext{
|
||||
template: parent.template,
|
||||
|
||||
Public: parent.Public,
|
||||
Private: make(Context),
|
||||
Autoescape: parent.Autoescape,
|
||||
}
|
||||
newctx.Shared = parent.Shared
|
||||
|
||||
// Copy all existing private items
|
||||
newctx.Private.Update(parent.Private)
|
||||
|
||||
return newctx
|
||||
}
|
||||
|
||||
func (ctx *ExecutionContext) Error(msg string, token *Token) *Error {
|
||||
return ctx.OrigError(errors.New(msg), token)
|
||||
}
|
||||
|
||||
func (ctx *ExecutionContext) OrigError(err error, token *Token) *Error {
|
||||
filename := ctx.template.name
|
||||
var line, col int
|
||||
if token != nil {
|
||||
// No tokens available
|
||||
// TODO: Add location (from where?)
|
||||
filename = token.Filename
|
||||
line = token.Line
|
||||
col = token.Col
|
||||
}
|
||||
return &Error{
|
||||
Template: ctx.template,
|
||||
Filename: filename,
|
||||
Line: line,
|
||||
Column: col,
|
||||
Token: token,
|
||||
Sender: "execution",
|
||||
OrigError: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx *ExecutionContext) Logf(format string, args ...interface{}) {
|
||||
ctx.template.set.logf(format, args...)
|
||||
}
|
31
vendor/github.com/flosch/pongo2/doc.go
generated
vendored
Normal file
31
vendor/github.com/flosch/pongo2/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
// A Django-syntax like template-engine
|
||||
//
|
||||
// Blog posts about pongo2 (including introduction and migration):
|
||||
// https://www.florian-schlachter.de/?tag=pongo2
|
||||
//
|
||||
// Complete documentation on the template language:
|
||||
// https://docs.djangoproject.com/en/dev/topics/templates/
|
||||
//
|
||||
// Try out pongo2 live in the pongo2 playground:
|
||||
// https://www.florian-schlachter.de/pongo2/
|
||||
//
|
||||
// Make sure to read README.md in the repository as well.
|
||||
//
|
||||
// A tiny example with template strings:
|
||||
//
|
||||
// (Snippet on playground: https://www.florian-schlachter.de/pongo2/?id=1206546277)
|
||||
//
|
||||
// // Compile the template first (i. e. creating the AST)
|
||||
// tpl, err := pongo2.FromString("Hello {{ name|capfirst }}!")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// // Now you can render the template with the given
|
||||
// // pongo2.Context how often you want to.
|
||||
// out, err := tpl.Execute(pongo2.Context{"name": "fred"})
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// fmt.Println(out) // Output: Hello Fred!
|
||||
//
|
||||
package pongo2
|
1
vendor/github.com/flosch/pongo2/docs/examples.md
generated
vendored
Normal file
1
vendor/github.com/flosch/pongo2/docs/examples.md
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
(Stub, TBA)
|
68
vendor/github.com/flosch/pongo2/docs/filters.md
generated
vendored
Normal file
68
vendor/github.com/flosch/pongo2/docs/filters.md
generated
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
TODO:
|
||||
|
||||
* What are filters?
|
||||
* List+explain all existing filters (pongo2 + pongo2-addons)
|
||||
|
||||
Implemented filters so far which needs documentation:
|
||||
|
||||
* escape
|
||||
* safe
|
||||
* escapejs
|
||||
* add
|
||||
* addslashes
|
||||
* capfirst
|
||||
* center
|
||||
* cut
|
||||
* date
|
||||
* default
|
||||
* default_if_none
|
||||
* divisibleby
|
||||
* first
|
||||
* floatformat
|
||||
* get_digit
|
||||
* iriencode
|
||||
* join
|
||||
* last
|
||||
* length
|
||||
* length_is
|
||||
* linebreaks
|
||||
* linebreaksbr
|
||||
* linenumbers
|
||||
* ljust
|
||||
* lower
|
||||
* make_list
|
||||
* phone2numeric
|
||||
* pluralize
|
||||
* random
|
||||
* removetags
|
||||
* rjust
|
||||
* slice
|
||||
* stringformat
|
||||
* striptags
|
||||
* time
|
||||
* title
|
||||
* truncatechars
|
||||
* truncatechars_html
|
||||
* truncatewords
|
||||
* truncatewords_html
|
||||
* upper
|
||||
* urlencode
|
||||
* urlize
|
||||
* urlizetrunc
|
||||
* wordcount
|
||||
* wordwrap
|
||||
* yesno
|
||||
|
||||
* filesizeformat*
|
||||
* slugify*
|
||||
* truncatesentences*
|
||||
* truncatesentences_html*
|
||||
* markdown*
|
||||
* intcomma*
|
||||
* ordinal*
|
||||
* naturalday*
|
||||
* timesince*
|
||||
* timeuntil*
|
||||
* naturaltime*
|
||||
|
||||
Filters marked with * are available through [pongo2-addons](https://github.com/flosch/pongo2-addons).
|
1
vendor/github.com/flosch/pongo2/docs/index.md
generated
vendored
Normal file
1
vendor/github.com/flosch/pongo2/docs/index.md
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
(Stub, TBA)
|
1
vendor/github.com/flosch/pongo2/docs/macros.md
generated
vendored
Normal file
1
vendor/github.com/flosch/pongo2/docs/macros.md
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
(Stub, TBA)
|
31
vendor/github.com/flosch/pongo2/docs/tags.md
generated
vendored
Normal file
31
vendor/github.com/flosch/pongo2/docs/tags.md
generated
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
TODO:
|
||||
|
||||
* What are tags?
|
||||
* List+explain all existing tags (pongo2 + pongo2-addons)
|
||||
|
||||
Implemented tags so far which needs documentation:
|
||||
|
||||
* autoescape
|
||||
* block
|
||||
* comment
|
||||
* cycle
|
||||
* extends
|
||||
* filter
|
||||
* firstof
|
||||
* for
|
||||
* if
|
||||
* ifchanged
|
||||
* ifequal
|
||||
* ifnotequal
|
||||
* import
|
||||
* include
|
||||
* lorem
|
||||
* macro
|
||||
* now
|
||||
* set
|
||||
* spaceless
|
||||
* ssi
|
||||
* templatetag
|
||||
* verbatim
|
||||
* widthratio
|
||||
* with
|
1
vendor/github.com/flosch/pongo2/docs/template_sets.md
generated
vendored
Normal file
1
vendor/github.com/flosch/pongo2/docs/template_sets.md
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
(Stub, TBA)
|
0
vendor/github.com/flosch/pongo2/docs/write_filters.md
generated
vendored
Normal file
0
vendor/github.com/flosch/pongo2/docs/write_filters.md
generated
vendored
Normal file
0
vendor/github.com/flosch/pongo2/docs/write_tags.md
generated
vendored
Normal file
0
vendor/github.com/flosch/pongo2/docs/write_tags.md
generated
vendored
Normal file
91
vendor/github.com/flosch/pongo2/error.go
generated
vendored
Normal file
91
vendor/github.com/flosch/pongo2/error.go
generated
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// The Error type is being used to address an error during lexing, parsing or
|
||||
// execution. If you want to return an error object (for example in your own
|
||||
// tag or filter) fill this object with as much information as you have.
|
||||
// Make sure "Sender" is always given (if you're returning an error within
|
||||
// a filter, make Sender equals 'filter:yourfilter'; same goes for tags: 'tag:mytag').
|
||||
// It's okay if you only fill in ErrorMsg if you don't have any other details at hand.
|
||||
type Error struct {
|
||||
Template *Template
|
||||
Filename string
|
||||
Line int
|
||||
Column int
|
||||
Token *Token
|
||||
Sender string
|
||||
OrigError error
|
||||
}
|
||||
|
||||
func (e *Error) updateFromTokenIfNeeded(template *Template, t *Token) *Error {
|
||||
if e.Template == nil {
|
||||
e.Template = template
|
||||
}
|
||||
|
||||
if e.Token == nil {
|
||||
e.Token = t
|
||||
if e.Line <= 0 {
|
||||
e.Line = t.Line
|
||||
e.Column = t.Col
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
// Returns a nice formatted error string.
|
||||
func (e *Error) Error() string {
|
||||
s := "[Error"
|
||||
if e.Sender != "" {
|
||||
s += " (where: " + e.Sender + ")"
|
||||
}
|
||||
if e.Filename != "" {
|
||||
s += " in " + e.Filename
|
||||
}
|
||||
if e.Line > 0 {
|
||||
s += fmt.Sprintf(" | Line %d Col %d", e.Line, e.Column)
|
||||
if e.Token != nil {
|
||||
s += fmt.Sprintf(" near '%s'", e.Token.Val)
|
||||
}
|
||||
}
|
||||
s += "] "
|
||||
s += e.OrigError.Error()
|
||||
return s
|
||||
}
|
||||
|
||||
// RawLine returns the affected line from the original template, if available.
|
||||
func (e *Error) RawLine() (line string, available bool, outErr error) {
|
||||
if e.Line <= 0 || e.Filename == "<string>" {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
filename := e.Filename
|
||||
if e.Template != nil {
|
||||
filename = e.Template.set.resolveFilename(e.Template, e.Filename)
|
||||
}
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
defer func() {
|
||||
err := file.Close()
|
||||
if err != nil && outErr == nil {
|
||||
outErr = err
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
l := 0
|
||||
for scanner.Scan() {
|
||||
l++
|
||||
if l == e.Line {
|
||||
return scanner.Text(), true, nil
|
||||
}
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
143
vendor/github.com/flosch/pongo2/filters.go
generated
vendored
Normal file
143
vendor/github.com/flosch/pongo2/filters.go
generated
vendored
Normal file
|
@ -0,0 +1,143 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
// FilterFunction is the type filter functions must fulfil
|
||||
type FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
|
||||
|
||||
var filters map[string]FilterFunction
|
||||
|
||||
func init() {
|
||||
filters = make(map[string]FilterFunction)
|
||||
}
|
||||
|
||||
// FilterExists returns true if the given filter is already registered
|
||||
func FilterExists(name string) bool {
|
||||
_, existing := filters[name]
|
||||
return existing
|
||||
}
|
||||
|
||||
// RegisterFilter registers a new filter. If there's already a filter with the same
|
||||
// name, RegisterFilter will panic. You usually want to call this
|
||||
// function in the filter's init() function:
|
||||
// http://golang.org/doc/effective_go.html#init
|
||||
//
|
||||
// See http://www.florian-schlachter.de/post/pongo2/ for more about
|
||||
// writing filters and tags.
|
||||
func RegisterFilter(name string, fn FilterFunction) error {
|
||||
if FilterExists(name) {
|
||||
return errors.Errorf("filter with name '%s' is already registered", name)
|
||||
}
|
||||
filters[name] = fn
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReplaceFilter replaces an already registered filter with a new implementation. Use this
|
||||
// function with caution since it allows you to change existing filter behaviour.
|
||||
func ReplaceFilter(name string, fn FilterFunction) error {
|
||||
if !FilterExists(name) {
|
||||
return errors.Errorf("filter with name '%s' does not exist (therefore cannot be overridden)", name)
|
||||
}
|
||||
filters[name] = fn
|
||||
return nil
|
||||
}
|
||||
|
||||
// MustApplyFilter behaves like ApplyFilter, but panics on an error.
|
||||
func MustApplyFilter(name string, value *Value, param *Value) *Value {
|
||||
val, err := ApplyFilter(name, value, param)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// ApplyFilter applies a filter to a given value using the given parameters.
|
||||
// Returns a *pongo2.Value or an error.
|
||||
func ApplyFilter(name string, value *Value, param *Value) (*Value, *Error) {
|
||||
fn, existing := filters[name]
|
||||
if !existing {
|
||||
return nil, &Error{
|
||||
Sender: "applyfilter",
|
||||
OrigError: errors.Errorf("Filter with name '%s' not found.", name),
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure param is a *Value
|
||||
if param == nil {
|
||||
param = AsValue(nil)
|
||||
}
|
||||
|
||||
return fn(value, param)
|
||||
}
|
||||
|
||||
type filterCall struct {
|
||||
token *Token
|
||||
|
||||
name string
|
||||
parameter IEvaluator
|
||||
|
||||
filterFunc FilterFunction
|
||||
}
|
||||
|
||||
func (fc *filterCall) Execute(v *Value, ctx *ExecutionContext) (*Value, *Error) {
|
||||
var param *Value
|
||||
var err *Error
|
||||
|
||||
if fc.parameter != nil {
|
||||
param, err = fc.parameter.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
param = AsValue(nil)
|
||||
}
|
||||
|
||||
filteredValue, err := fc.filterFunc(v, param)
|
||||
if err != nil {
|
||||
return nil, err.updateFromTokenIfNeeded(ctx.template, fc.token)
|
||||
}
|
||||
return filteredValue, nil
|
||||
}
|
||||
|
||||
// Filter = IDENT | IDENT ":" FilterArg | IDENT "|" Filter
|
||||
func (p *Parser) parseFilter() (*filterCall, *Error) {
|
||||
identToken := p.MatchType(TokenIdentifier)
|
||||
|
||||
// Check filter ident
|
||||
if identToken == nil {
|
||||
return nil, p.Error("Filter name must be an identifier.", nil)
|
||||
}
|
||||
|
||||
filter := &filterCall{
|
||||
token: identToken,
|
||||
name: identToken.Val,
|
||||
}
|
||||
|
||||
// Get the appropriate filter function and bind it
|
||||
filterFn, exists := filters[identToken.Val]
|
||||
if !exists {
|
||||
return nil, p.Error(fmt.Sprintf("Filter '%s' does not exist.", identToken.Val), identToken)
|
||||
}
|
||||
|
||||
filter.filterFunc = filterFn
|
||||
|
||||
// Check for filter-argument (2 tokens needed: ':' ARG)
|
||||
if p.Match(TokenSymbol, ":") != nil {
|
||||
if p.Peek(TokenSymbol, "}}") != nil {
|
||||
return nil, p.Error("Filter parameter required after ':'.", nil)
|
||||
}
|
||||
|
||||
// Get filter argument expression
|
||||
v, err := p.parseVariableOrLiteral()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filter.parameter = v
|
||||
}
|
||||
|
||||
return filter, nil
|
||||
}
|
927
vendor/github.com/flosch/pongo2/filters_builtin.go
generated
vendored
Normal file
927
vendor/github.com/flosch/pongo2/filters_builtin.go
generated
vendored
Normal file
|
@ -0,0 +1,927 @@
|
|||
package pongo2
|
||||
|
||||
/* Filters that are provided through github.com/flosch/pongo2-addons:
|
||||
------------------------------------------------------------------
|
||||
|
||||
filesizeformat
|
||||
slugify
|
||||
timesince
|
||||
timeuntil
|
||||
|
||||
Filters that won't be added:
|
||||
----------------------------
|
||||
|
||||
get_static_prefix (reason: web-framework specific)
|
||||
pprint (reason: python-specific)
|
||||
static (reason: web-framework specific)
|
||||
|
||||
Reconsideration (not implemented yet):
|
||||
--------------------------------------
|
||||
|
||||
force_escape (reason: not yet needed since this is the behaviour of pongo2's escape filter)
|
||||
safeseq (reason: same reason as `force_escape`)
|
||||
unordered_list (python-specific; not sure whether needed or not)
|
||||
dictsort (python-specific; maybe one could add a filter to sort a list of structs by a specific field name)
|
||||
dictsortreversed (see dictsort)
|
||||
*/
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
||||
RegisterFilter("escape", filterEscape)
|
||||
RegisterFilter("safe", filterSafe)
|
||||
RegisterFilter("escapejs", filterEscapejs)
|
||||
|
||||
RegisterFilter("add", filterAdd)
|
||||
RegisterFilter("addslashes", filterAddslashes)
|
||||
RegisterFilter("capfirst", filterCapfirst)
|
||||
RegisterFilter("center", filterCenter)
|
||||
RegisterFilter("cut", filterCut)
|
||||
RegisterFilter("date", filterDate)
|
||||
RegisterFilter("default", filterDefault)
|
||||
RegisterFilter("default_if_none", filterDefaultIfNone)
|
||||
RegisterFilter("divisibleby", filterDivisibleby)
|
||||
RegisterFilter("first", filterFirst)
|
||||
RegisterFilter("floatformat", filterFloatformat)
|
||||
RegisterFilter("get_digit", filterGetdigit)
|
||||
RegisterFilter("iriencode", filterIriencode)
|
||||
RegisterFilter("join", filterJoin)
|
||||
RegisterFilter("last", filterLast)
|
||||
RegisterFilter("length", filterLength)
|
||||
RegisterFilter("length_is", filterLengthis)
|
||||
RegisterFilter("linebreaks", filterLinebreaks)
|
||||
RegisterFilter("linebreaksbr", filterLinebreaksbr)
|
||||
RegisterFilter("linenumbers", filterLinenumbers)
|
||||
RegisterFilter("ljust", filterLjust)
|
||||
RegisterFilter("lower", filterLower)
|
||||
RegisterFilter("make_list", filterMakelist)
|
||||
RegisterFilter("phone2numeric", filterPhone2numeric)
|
||||
RegisterFilter("pluralize", filterPluralize)
|
||||
RegisterFilter("random", filterRandom)
|
||||
RegisterFilter("removetags", filterRemovetags)
|
||||
RegisterFilter("rjust", filterRjust)
|
||||
RegisterFilter("slice", filterSlice)
|
||||
RegisterFilter("split", filterSplit)
|
||||
RegisterFilter("stringformat", filterStringformat)
|
||||
RegisterFilter("striptags", filterStriptags)
|
||||
RegisterFilter("time", filterDate) // time uses filterDate (same golang-format)
|
||||
RegisterFilter("title", filterTitle)
|
||||
RegisterFilter("truncatechars", filterTruncatechars)
|
||||
RegisterFilter("truncatechars_html", filterTruncatecharsHTML)
|
||||
RegisterFilter("truncatewords", filterTruncatewords)
|
||||
RegisterFilter("truncatewords_html", filterTruncatewordsHTML)
|
||||
RegisterFilter("upper", filterUpper)
|
||||
RegisterFilter("urlencode", filterUrlencode)
|
||||
RegisterFilter("urlize", filterUrlize)
|
||||
RegisterFilter("urlizetrunc", filterUrlizetrunc)
|
||||
RegisterFilter("wordcount", filterWordcount)
|
||||
RegisterFilter("wordwrap", filterWordwrap)
|
||||
RegisterFilter("yesno", filterYesno)
|
||||
|
||||
RegisterFilter("float", filterFloat) // pongo-specific
|
||||
RegisterFilter("integer", filterInteger) // pongo-specific
|
||||
}
|
||||
|
||||
func filterTruncatecharsHelper(s string, newLen int) string {
|
||||
runes := []rune(s)
|
||||
if newLen < len(runes) {
|
||||
if newLen >= 3 {
|
||||
return fmt.Sprintf("%s...", string(runes[:newLen-3]))
|
||||
}
|
||||
// Not enough space for the ellipsis
|
||||
return string(runes[:newLen])
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
func filterTruncateHTMLHelper(value string, newOutput *bytes.Buffer, cond func() bool, fn func(c rune, s int, idx int) int, finalize func()) {
|
||||
vLen := len(value)
|
||||
var tagStack []string
|
||||
idx := 0
|
||||
|
||||
for idx < vLen && !cond() {
|
||||
c, s := utf8.DecodeRuneInString(value[idx:])
|
||||
if c == utf8.RuneError {
|
||||
idx += s
|
||||
continue
|
||||
}
|
||||
|
||||
if c == '<' {
|
||||
newOutput.WriteRune(c)
|
||||
idx += s // consume "<"
|
||||
|
||||
if idx+1 < vLen {
|
||||
if value[idx] == '/' {
|
||||
// Close tag
|
||||
|
||||
newOutput.WriteString("/")
|
||||
|
||||
tag := ""
|
||||
idx++ // consume "/"
|
||||
|
||||
for idx < vLen {
|
||||
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
||||
if c2 == utf8.RuneError {
|
||||
idx += size2
|
||||
continue
|
||||
}
|
||||
|
||||
// End of tag found
|
||||
if c2 == '>' {
|
||||
idx++ // consume ">"
|
||||
break
|
||||
}
|
||||
tag += string(c2)
|
||||
idx += size2
|
||||
}
|
||||
|
||||
if len(tagStack) > 0 {
|
||||
// Ideally, the close tag is TOP of tag stack
|
||||
// In malformed HTML, it must not be, so iterate through the stack and remove the tag
|
||||
for i := len(tagStack) - 1; i >= 0; i-- {
|
||||
if tagStack[i] == tag {
|
||||
// Found the tag
|
||||
tagStack[i] = tagStack[len(tagStack)-1]
|
||||
tagStack = tagStack[:len(tagStack)-1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newOutput.WriteString(tag)
|
||||
newOutput.WriteString(">")
|
||||
} else {
|
||||
// Open tag
|
||||
|
||||
tag := ""
|
||||
|
||||
params := false
|
||||
for idx < vLen {
|
||||
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
||||
if c2 == utf8.RuneError {
|
||||
idx += size2
|
||||
continue
|
||||
}
|
||||
|
||||
newOutput.WriteRune(c2)
|
||||
|
||||
// End of tag found
|
||||
if c2 == '>' {
|
||||
idx++ // consume ">"
|
||||
break
|
||||
}
|
||||
|
||||
if !params {
|
||||
if c2 == ' ' {
|
||||
params = true
|
||||
} else {
|
||||
tag += string(c2)
|
||||
}
|
||||
}
|
||||
|
||||
idx += size2
|
||||
}
|
||||
|
||||
// Add tag to stack
|
||||
tagStack = append(tagStack, tag)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
idx = fn(c, s, idx)
|
||||
}
|
||||
}
|
||||
|
||||
finalize()
|
||||
|
||||
for i := len(tagStack) - 1; i >= 0; i-- {
|
||||
tag := tagStack[i]
|
||||
// Close everything from the regular tag stack
|
||||
newOutput.WriteString(fmt.Sprintf("</%s>", tag))
|
||||
}
|
||||
}
|
||||
|
||||
func filterTruncatechars(in *Value, param *Value) (*Value, *Error) {
|
||||
s := in.String()
|
||||
newLen := param.Integer()
|
||||
return AsValue(filterTruncatecharsHelper(s, newLen)), nil
|
||||
}
|
||||
|
||||
func filterTruncatecharsHTML(in *Value, param *Value) (*Value, *Error) {
|
||||
value := in.String()
|
||||
newLen := max(param.Integer()-3, 0)
|
||||
|
||||
newOutput := bytes.NewBuffer(nil)
|
||||
|
||||
textcounter := 0
|
||||
|
||||
filterTruncateHTMLHelper(value, newOutput, func() bool {
|
||||
return textcounter >= newLen
|
||||
}, func(c rune, s int, idx int) int {
|
||||
textcounter++
|
||||
newOutput.WriteRune(c)
|
||||
|
||||
return idx + s
|
||||
}, func() {
|
||||
if textcounter >= newLen && textcounter < len(value) {
|
||||
newOutput.WriteString("...")
|
||||
}
|
||||
})
|
||||
|
||||
return AsSafeValue(newOutput.String()), nil
|
||||
}
|
||||
|
||||
func filterTruncatewords(in *Value, param *Value) (*Value, *Error) {
|
||||
words := strings.Fields(in.String())
|
||||
n := param.Integer()
|
||||
if n <= 0 {
|
||||
return AsValue(""), nil
|
||||
}
|
||||
nlen := min(len(words), n)
|
||||
out := make([]string, 0, nlen)
|
||||
for i := 0; i < nlen; i++ {
|
||||
out = append(out, words[i])
|
||||
}
|
||||
|
||||
if n < len(words) {
|
||||
out = append(out, "...")
|
||||
}
|
||||
|
||||
return AsValue(strings.Join(out, " ")), nil
|
||||
}
|
||||
|
||||
func filterTruncatewordsHTML(in *Value, param *Value) (*Value, *Error) {
|
||||
value := in.String()
|
||||
newLen := max(param.Integer(), 0)
|
||||
|
||||
newOutput := bytes.NewBuffer(nil)
|
||||
|
||||
wordcounter := 0
|
||||
|
||||
filterTruncateHTMLHelper(value, newOutput, func() bool {
|
||||
return wordcounter >= newLen
|
||||
}, func(_ rune, _ int, idx int) int {
|
||||
// Get next word
|
||||
wordFound := false
|
||||
|
||||
for idx < len(value) {
|
||||
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
||||
if c2 == utf8.RuneError {
|
||||
idx += size2
|
||||
continue
|
||||
}
|
||||
|
||||
if c2 == '<' {
|
||||
// HTML tag start, don't consume it
|
||||
return idx
|
||||
}
|
||||
|
||||
newOutput.WriteRune(c2)
|
||||
idx += size2
|
||||
|
||||
if c2 == ' ' || c2 == '.' || c2 == ',' || c2 == ';' {
|
||||
// Word ends here, stop capturing it now
|
||||
break
|
||||
} else {
|
||||
wordFound = true
|
||||
}
|
||||
}
|
||||
|
||||
if wordFound {
|
||||
wordcounter++
|
||||
}
|
||||
|
||||
return idx
|
||||
}, func() {
|
||||
if wordcounter >= newLen {
|
||||
newOutput.WriteString("...")
|
||||
}
|
||||
})
|
||||
|
||||
return AsSafeValue(newOutput.String()), nil
|
||||
}
|
||||
|
||||
func filterEscape(in *Value, param *Value) (*Value, *Error) {
|
||||
output := strings.Replace(in.String(), "&", "&", -1)
|
||||
output = strings.Replace(output, ">", ">", -1)
|
||||
output = strings.Replace(output, "<", "<", -1)
|
||||
output = strings.Replace(output, "\"", """, -1)
|
||||
output = strings.Replace(output, "'", "'", -1)
|
||||
return AsValue(output), nil
|
||||
}
|
||||
|
||||
func filterSafe(in *Value, param *Value) (*Value, *Error) {
|
||||
return in, nil // nothing to do here, just to keep track of the safe application
|
||||
}
|
||||
|
||||
func filterEscapejs(in *Value, param *Value) (*Value, *Error) {
|
||||
sin := in.String()
|
||||
|
||||
var b bytes.Buffer
|
||||
|
||||
idx := 0
|
||||
for idx < len(sin) {
|
||||
c, size := utf8.DecodeRuneInString(sin[idx:])
|
||||
if c == utf8.RuneError {
|
||||
idx += size
|
||||
continue
|
||||
}
|
||||
|
||||
if c == '\\' {
|
||||
// Escape seq?
|
||||
if idx+1 < len(sin) {
|
||||
switch sin[idx+1] {
|
||||
case 'r':
|
||||
b.WriteString(fmt.Sprintf(`\u%04X`, '\r'))
|
||||
idx += 2
|
||||
continue
|
||||
case 'n':
|
||||
b.WriteString(fmt.Sprintf(`\u%04X`, '\n'))
|
||||
idx += 2
|
||||
continue
|
||||
/*case '\'':
|
||||
b.WriteString(fmt.Sprintf(`\u%04X`, '\''))
|
||||
idx += 2
|
||||
continue
|
||||
case '"':
|
||||
b.WriteString(fmt.Sprintf(`\u%04X`, '"'))
|
||||
idx += 2
|
||||
continue*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ' ' || c == '/' {
|
||||
b.WriteRune(c)
|
||||
} else {
|
||||
b.WriteString(fmt.Sprintf(`\u%04X`, c))
|
||||
}
|
||||
|
||||
idx += size
|
||||
}
|
||||
|
||||
return AsValue(b.String()), nil
|
||||
}
|
||||
|
||||
func filterAdd(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.IsNumber() && param.IsNumber() {
|
||||
if in.IsFloat() || param.IsFloat() {
|
||||
return AsValue(in.Float() + param.Float()), nil
|
||||
}
|
||||
return AsValue(in.Integer() + param.Integer()), nil
|
||||
}
|
||||
// If in/param is not a number, we're relying on the
|
||||
// Value's String() conversion and just add them both together
|
||||
return AsValue(in.String() + param.String()), nil
|
||||
}
|
||||
|
||||
func filterAddslashes(in *Value, param *Value) (*Value, *Error) {
|
||||
output := strings.Replace(in.String(), "\\", "\\\\", -1)
|
||||
output = strings.Replace(output, "\"", "\\\"", -1)
|
||||
output = strings.Replace(output, "'", "\\'", -1)
|
||||
return AsValue(output), nil
|
||||
}
|
||||
|
||||
func filterCut(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(strings.Replace(in.String(), param.String(), "", -1)), nil
|
||||
}
|
||||
|
||||
func filterLength(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(in.Len()), nil
|
||||
}
|
||||
|
||||
func filterLengthis(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(in.Len() == param.Integer()), nil
|
||||
}
|
||||
|
||||
func filterDefault(in *Value, param *Value) (*Value, *Error) {
|
||||
if !in.IsTrue() {
|
||||
return param, nil
|
||||
}
|
||||
return in, nil
|
||||
}
|
||||
|
||||
func filterDefaultIfNone(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.IsNil() {
|
||||
return param, nil
|
||||
}
|
||||
return in, nil
|
||||
}
|
||||
|
||||
func filterDivisibleby(in *Value, param *Value) (*Value, *Error) {
|
||||
if param.Integer() == 0 {
|
||||
return AsValue(false), nil
|
||||
}
|
||||
return AsValue(in.Integer()%param.Integer() == 0), nil
|
||||
}
|
||||
|
||||
func filterFirst(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.CanSlice() && in.Len() > 0 {
|
||||
return in.Index(0), nil
|
||||
}
|
||||
return AsValue(""), nil
|
||||
}
|
||||
|
||||
func filterFloatformat(in *Value, param *Value) (*Value, *Error) {
|
||||
val := in.Float()
|
||||
|
||||
decimals := -1
|
||||
if !param.IsNil() {
|
||||
// Any argument provided?
|
||||
decimals = param.Integer()
|
||||
}
|
||||
|
||||
// if the argument is not a number (e. g. empty), the default
|
||||
// behaviour is trim the result
|
||||
trim := !param.IsNumber()
|
||||
|
||||
if decimals <= 0 {
|
||||
// argument is negative or zero, so we
|
||||
// want the output being trimmed
|
||||
decimals = -decimals
|
||||
trim = true
|
||||
}
|
||||
|
||||
if trim {
|
||||
// Remove zeroes
|
||||
if float64(int(val)) == val {
|
||||
return AsValue(in.Integer()), nil
|
||||
}
|
||||
}
|
||||
|
||||
return AsValue(strconv.FormatFloat(val, 'f', decimals, 64)), nil
|
||||
}
|
||||
|
||||
func filterGetdigit(in *Value, param *Value) (*Value, *Error) {
|
||||
i := param.Integer()
|
||||
l := len(in.String()) // do NOT use in.Len() here!
|
||||
if i <= 0 || i > l {
|
||||
return in, nil
|
||||
}
|
||||
return AsValue(in.String()[l-i] - 48), nil
|
||||
}
|
||||
|
||||
const filterIRIChars = "/#%[]=:;$&()+,!?*@'~"
|
||||
|
||||
func filterIriencode(in *Value, param *Value) (*Value, *Error) {
|
||||
var b bytes.Buffer
|
||||
|
||||
sin := in.String()
|
||||
for _, r := range sin {
|
||||
if strings.IndexRune(filterIRIChars, r) >= 0 {
|
||||
b.WriteRune(r)
|
||||
} else {
|
||||
b.WriteString(url.QueryEscape(string(r)))
|
||||
}
|
||||
}
|
||||
|
||||
return AsValue(b.String()), nil
|
||||
}
|
||||
|
||||
func filterJoin(in *Value, param *Value) (*Value, *Error) {
|
||||
if !in.CanSlice() {
|
||||
return in, nil
|
||||
}
|
||||
sep := param.String()
|
||||
sl := make([]string, 0, in.Len())
|
||||
for i := 0; i < in.Len(); i++ {
|
||||
sl = append(sl, in.Index(i).String())
|
||||
}
|
||||
return AsValue(strings.Join(sl, sep)), nil
|
||||
}
|
||||
|
||||
func filterLast(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.CanSlice() && in.Len() > 0 {
|
||||
return in.Index(in.Len() - 1), nil
|
||||
}
|
||||
return AsValue(""), nil
|
||||
}
|
||||
|
||||
func filterUpper(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(strings.ToUpper(in.String())), nil
|
||||
}
|
||||
|
||||
func filterLower(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(strings.ToLower(in.String())), nil
|
||||
}
|
||||
|
||||
func filterMakelist(in *Value, param *Value) (*Value, *Error) {
|
||||
s := in.String()
|
||||
result := make([]string, 0, len(s))
|
||||
for _, c := range s {
|
||||
result = append(result, string(c))
|
||||
}
|
||||
return AsValue(result), nil
|
||||
}
|
||||
|
||||
func filterCapfirst(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.Len() <= 0 {
|
||||
return AsValue(""), nil
|
||||
}
|
||||
t := in.String()
|
||||
r, size := utf8.DecodeRuneInString(t)
|
||||
return AsValue(strings.ToUpper(string(r)) + t[size:]), nil
|
||||
}
|
||||
|
||||
func filterCenter(in *Value, param *Value) (*Value, *Error) {
|
||||
width := param.Integer()
|
||||
slen := in.Len()
|
||||
if width <= slen {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
spaces := width - slen
|
||||
left := spaces/2 + spaces%2
|
||||
right := spaces / 2
|
||||
|
||||
return AsValue(fmt.Sprintf("%s%s%s", strings.Repeat(" ", left),
|
||||
in.String(), strings.Repeat(" ", right))), nil
|
||||
}
|
||||
|
||||
func filterDate(in *Value, param *Value) (*Value, *Error) {
|
||||
t, isTime := in.Interface().(time.Time)
|
||||
if !isTime {
|
||||
return nil, &Error{
|
||||
Sender: "filter:date",
|
||||
OrigError: errors.New("filter input argument must be of type 'time.Time'"),
|
||||
}
|
||||
}
|
||||
return AsValue(t.Format(param.String())), nil
|
||||
}
|
||||
|
||||
func filterFloat(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(in.Float()), nil
|
||||
}
|
||||
|
||||
func filterInteger(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(in.Integer()), nil
|
||||
}
|
||||
|
||||
func filterLinebreaks(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.Len() == 0 {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
|
||||
// Newline = <br />
|
||||
// Double newline = <p>...</p>
|
||||
lines := strings.Split(in.String(), "\n")
|
||||
lenlines := len(lines)
|
||||
|
||||
opened := false
|
||||
|
||||
for idx, line := range lines {
|
||||
|
||||
if !opened {
|
||||
b.WriteString("<p>")
|
||||
opened = true
|
||||
}
|
||||
|
||||
b.WriteString(line)
|
||||
|
||||
if idx < lenlines-1 && strings.TrimSpace(lines[idx]) != "" {
|
||||
// We've not reached the end
|
||||
if strings.TrimSpace(lines[idx+1]) == "" {
|
||||
// Next line is empty
|
||||
if opened {
|
||||
b.WriteString("</p>")
|
||||
opened = false
|
||||
}
|
||||
} else {
|
||||
b.WriteString("<br />")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if opened {
|
||||
b.WriteString("</p>")
|
||||
}
|
||||
|
||||
return AsValue(b.String()), nil
|
||||
}
|
||||
|
||||
func filterSplit(in *Value, param *Value) (*Value, *Error) {
|
||||
chunks := strings.Split(in.String(), param.String())
|
||||
|
||||
return AsValue(chunks), nil
|
||||
}
|
||||
|
||||
func filterLinebreaksbr(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(strings.Replace(in.String(), "\n", "<br />", -1)), nil
|
||||
}
|
||||
|
||||
func filterLinenumbers(in *Value, param *Value) (*Value, *Error) {
|
||||
lines := strings.Split(in.String(), "\n")
|
||||
output := make([]string, 0, len(lines))
|
||||
for idx, line := range lines {
|
||||
output = append(output, fmt.Sprintf("%d. %s", idx+1, line))
|
||||
}
|
||||
return AsValue(strings.Join(output, "\n")), nil
|
||||
}
|
||||
|
||||
func filterLjust(in *Value, param *Value) (*Value, *Error) {
|
||||
times := param.Integer() - in.Len()
|
||||
if times < 0 {
|
||||
times = 0
|
||||
}
|
||||
return AsValue(fmt.Sprintf("%s%s", in.String(), strings.Repeat(" ", times))), nil
|
||||
}
|
||||
|
||||
func filterUrlencode(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(url.QueryEscape(in.String())), nil
|
||||
}
|
||||
|
||||
// TODO: This regexp could do some work
|
||||
var filterUrlizeURLRegexp = regexp.MustCompile(`((((http|https)://)|www\.|((^|[ ])[0-9A-Za-z_\-]+(\.com|\.net|\.org|\.info|\.biz|\.de))))(?U:.*)([ ]+|$)`)
|
||||
var filterUrlizeEmailRegexp = regexp.MustCompile(`(\w+@\w+\.\w{2,4})`)
|
||||
|
||||
func filterUrlizeHelper(input string, autoescape bool, trunc int) (string, error) {
|
||||
var soutErr error
|
||||
sout := filterUrlizeURLRegexp.ReplaceAllStringFunc(input, func(raw_url string) string {
|
||||
var prefix string
|
||||
var suffix string
|
||||
if strings.HasPrefix(raw_url, " ") {
|
||||
prefix = " "
|
||||
}
|
||||
if strings.HasSuffix(raw_url, " ") {
|
||||
suffix = " "
|
||||
}
|
||||
|
||||
raw_url = strings.TrimSpace(raw_url)
|
||||
|
||||
t, err := ApplyFilter("iriencode", AsValue(raw_url), nil)
|
||||
if err != nil {
|
||||
soutErr = err
|
||||
return ""
|
||||
}
|
||||
url := t.String()
|
||||
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
url = fmt.Sprintf("http://%s", url)
|
||||
}
|
||||
|
||||
title := raw_url
|
||||
|
||||
if trunc > 3 && len(title) > trunc {
|
||||
title = fmt.Sprintf("%s...", title[:trunc-3])
|
||||
}
|
||||
|
||||
if autoescape {
|
||||
t, err := ApplyFilter("escape", AsValue(title), nil)
|
||||
if err != nil {
|
||||
soutErr = err
|
||||
return ""
|
||||
}
|
||||
title = t.String()
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`%s<a href="%s" rel="nofollow">%s</a>%s`, prefix, url, title, suffix)
|
||||
})
|
||||
if soutErr != nil {
|
||||
return "", soutErr
|
||||
}
|
||||
|
||||
sout = filterUrlizeEmailRegexp.ReplaceAllStringFunc(sout, func(mail string) string {
|
||||
title := mail
|
||||
|
||||
if trunc > 3 && len(title) > trunc {
|
||||
title = fmt.Sprintf("%s...", title[:trunc-3])
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`<a href="mailto:%s">%s</a>`, mail, title)
|
||||
})
|
||||
|
||||
return sout, nil
|
||||
}
|
||||
|
||||
func filterUrlize(in *Value, param *Value) (*Value, *Error) {
|
||||
autoescape := true
|
||||
if param.IsBool() {
|
||||
autoescape = param.Bool()
|
||||
}
|
||||
|
||||
s, err := filterUrlizeHelper(in.String(), autoescape, -1)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
|
||||
return AsValue(s), nil
|
||||
}
|
||||
|
||||
func filterUrlizetrunc(in *Value, param *Value) (*Value, *Error) {
|
||||
s, err := filterUrlizeHelper(in.String(), true, param.Integer())
|
||||
if err != nil {
|
||||
return nil, &Error{
|
||||
Sender: "filter:urlizetrunc",
|
||||
OrigError: errors.New("you cannot pass more than 2 arguments to filter 'pluralize'"),
|
||||
}
|
||||
}
|
||||
return AsValue(s), nil
|
||||
}
|
||||
|
||||
func filterStringformat(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(fmt.Sprintf(param.String(), in.Interface())), nil
|
||||
}
|
||||
|
||||
var reStriptags = regexp.MustCompile("<[^>]*?>")
|
||||
|
||||
func filterStriptags(in *Value, param *Value) (*Value, *Error) {
|
||||
s := in.String()
|
||||
|
||||
// Strip all tags
|
||||
s = reStriptags.ReplaceAllString(s, "")
|
||||
|
||||
return AsValue(strings.TrimSpace(s)), nil
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Phoneword
|
||||
var filterPhone2numericMap = map[string]string{
|
||||
"a": "2", "b": "2", "c": "2", "d": "3", "e": "3", "f": "3", "g": "4", "h": "4", "i": "4", "j": "5", "k": "5",
|
||||
"l": "5", "m": "6", "n": "6", "o": "6", "p": "7", "q": "7", "r": "7", "s": "7", "t": "8", "u": "8", "v": "8",
|
||||
"w": "9", "x": "9", "y": "9", "z": "9",
|
||||
}
|
||||
|
||||
func filterPhone2numeric(in *Value, param *Value) (*Value, *Error) {
|
||||
sin := in.String()
|
||||
for k, v := range filterPhone2numericMap {
|
||||
sin = strings.Replace(sin, k, v, -1)
|
||||
sin = strings.Replace(sin, strings.ToUpper(k), v, -1)
|
||||
}
|
||||
return AsValue(sin), nil
|
||||
}
|
||||
|
||||
func filterPluralize(in *Value, param *Value) (*Value, *Error) {
|
||||
if in.IsNumber() {
|
||||
// Works only on numbers
|
||||
if param.Len() > 0 {
|
||||
endings := strings.Split(param.String(), ",")
|
||||
if len(endings) > 2 {
|
||||
return nil, &Error{
|
||||
Sender: "filter:pluralize",
|
||||
OrigError: errors.New("you cannot pass more than 2 arguments to filter 'pluralize'"),
|
||||
}
|
||||
}
|
||||
if len(endings) == 1 {
|
||||
// 1 argument
|
||||
if in.Integer() != 1 {
|
||||
return AsValue(endings[0]), nil
|
||||
}
|
||||
} else {
|
||||
if in.Integer() != 1 {
|
||||
// 2 arguments
|
||||
return AsValue(endings[1]), nil
|
||||
}
|
||||
return AsValue(endings[0]), nil
|
||||
}
|
||||
} else {
|
||||
if in.Integer() != 1 {
|
||||
// return default 's'
|
||||
return AsValue("s"), nil
|
||||
}
|
||||
}
|
||||
|
||||
return AsValue(""), nil
|
||||
}
|
||||
return nil, &Error{
|
||||
Sender: "filter:pluralize",
|
||||
OrigError: errors.New("filter 'pluralize' does only work on numbers"),
|
||||
}
|
||||
}
|
||||
|
||||
func filterRandom(in *Value, param *Value) (*Value, *Error) {
|
||||
if !in.CanSlice() || in.Len() <= 0 {
|
||||
return in, nil
|
||||
}
|
||||
i := rand.Intn(in.Len())
|
||||
return in.Index(i), nil
|
||||
}
|
||||
|
||||
func filterRemovetags(in *Value, param *Value) (*Value, *Error) {
|
||||
s := in.String()
|
||||
tags := strings.Split(param.String(), ",")
|
||||
|
||||
// Strip only specific tags
|
||||
for _, tag := range tags {
|
||||
re := regexp.MustCompile(fmt.Sprintf("</?%s/?>", tag))
|
||||
s = re.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
return AsValue(strings.TrimSpace(s)), nil
|
||||
}
|
||||
|
||||
func filterRjust(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(fmt.Sprintf(fmt.Sprintf("%%%ds", param.Integer()), in.String())), nil
|
||||
}
|
||||
|
||||
func filterSlice(in *Value, param *Value) (*Value, *Error) {
|
||||
comp := strings.Split(param.String(), ":")
|
||||
if len(comp) != 2 {
|
||||
return nil, &Error{
|
||||
Sender: "filter:slice",
|
||||
OrigError: errors.New("Slice string must have the format 'from:to' [from/to can be omitted, but the ':' is required]"),
|
||||
}
|
||||
}
|
||||
|
||||
if !in.CanSlice() {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
from := AsValue(comp[0]).Integer()
|
||||
to := in.Len()
|
||||
|
||||
if from > to {
|
||||
from = to
|
||||
}
|
||||
|
||||
vto := AsValue(comp[1]).Integer()
|
||||
if vto >= from && vto <= in.Len() {
|
||||
to = vto
|
||||
}
|
||||
|
||||
return in.Slice(from, to), nil
|
||||
}
|
||||
|
||||
func filterTitle(in *Value, param *Value) (*Value, *Error) {
|
||||
if !in.IsString() {
|
||||
return AsValue(""), nil
|
||||
}
|
||||
return AsValue(strings.Title(strings.ToLower(in.String()))), nil
|
||||
}
|
||||
|
||||
func filterWordcount(in *Value, param *Value) (*Value, *Error) {
|
||||
return AsValue(len(strings.Fields(in.String()))), nil
|
||||
}
|
||||
|
||||
func filterWordwrap(in *Value, param *Value) (*Value, *Error) {
|
||||
words := strings.Fields(in.String())
|
||||
wordsLen := len(words)
|
||||
wrapAt := param.Integer()
|
||||
if wrapAt <= 0 {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
linecount := wordsLen/wrapAt + wordsLen%wrapAt
|
||||
lines := make([]string, 0, linecount)
|
||||
for i := 0; i < linecount; i++ {
|
||||
lines = append(lines, strings.Join(words[wrapAt*i:min(wrapAt*(i+1), wordsLen)], " "))
|
||||
}
|
||||
return AsValue(strings.Join(lines, "\n")), nil
|
||||
}
|
||||
|
||||
func filterYesno(in *Value, param *Value) (*Value, *Error) {
|
||||
choices := map[int]string{
|
||||
0: "yes",
|
||||
1: "no",
|
||||
2: "maybe",
|
||||
}
|
||||
paramString := param.String()
|
||||
customChoices := strings.Split(paramString, ",")
|
||||
if len(paramString) > 0 {
|
||||
if len(customChoices) > 3 {
|
||||
return nil, &Error{
|
||||
Sender: "filter:yesno",
|
||||
OrigError: errors.Errorf("You cannot pass more than 3 options to the 'yesno'-filter (got: '%s').", paramString),
|
||||
}
|
||||
}
|
||||
if len(customChoices) < 2 {
|
||||
return nil, &Error{
|
||||
Sender: "filter:yesno",
|
||||
OrigError: errors.Errorf("You must pass either no or at least 2 arguments to the 'yesno'-filter (got: '%s').", paramString),
|
||||
}
|
||||
}
|
||||
|
||||
// Map to the options now
|
||||
choices[0] = customChoices[0]
|
||||
choices[1] = customChoices[1]
|
||||
if len(customChoices) == 3 {
|
||||
choices[2] = customChoices[2]
|
||||
}
|
||||
}
|
||||
|
||||
// maybe
|
||||
if in.IsNil() {
|
||||
return AsValue(choices[2]), nil
|
||||
}
|
||||
|
||||
// yes
|
||||
if in.IsTrue() {
|
||||
return AsValue(choices[0]), nil
|
||||
}
|
||||
|
||||
// no
|
||||
return AsValue(choices[1]), nil
|
||||
}
|
15
vendor/github.com/flosch/pongo2/helpers.go
generated
vendored
Normal file
15
vendor/github.com/flosch/pongo2/helpers.go
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
package pongo2
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
425
vendor/github.com/flosch/pongo2/lexer.go
generated
vendored
Normal file
425
vendor/github.com/flosch/pongo2/lexer.go
generated
vendored
Normal file
|
@ -0,0 +1,425 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
TokenError = iota
|
||||
EOF
|
||||
|
||||
TokenHTML
|
||||
|
||||
TokenKeyword
|
||||
TokenIdentifier
|
||||
TokenString
|
||||
TokenNumber
|
||||
TokenSymbol
|
||||
)
|
||||
|
||||
var (
|
||||
tokenSpaceChars = " \n\r\t"
|
||||
tokenIdentifierChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
|
||||
tokenIdentifierCharsWithDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789"
|
||||
tokenDigits = "0123456789"
|
||||
|
||||
// Available symbols in pongo2 (within filters/tag)
|
||||
TokenSymbols = []string{
|
||||
// 3-Char symbols
|
||||
|
||||
// 2-Char symbols
|
||||
"==", ">=", "<=", "&&", "||", "{{", "}}", "{%", "%}", "!=", "<>",
|
||||
|
||||
// 1-Char symbol
|
||||
"(", ")", "+", "-", "*", "<", ">", "/", "^", ",", ".", "!", "|", ":", "=", "%",
|
||||
}
|
||||
|
||||
// Available keywords in pongo2
|
||||
TokenKeywords = []string{"in", "and", "or", "not", "true", "false", "as", "export"}
|
||||
)
|
||||
|
||||
type TokenType int
|
||||
type Token struct {
|
||||
Filename string
|
||||
Typ TokenType
|
||||
Val string
|
||||
Line int
|
||||
Col int
|
||||
}
|
||||
|
||||
type lexerStateFn func() lexerStateFn
|
||||
type lexer struct {
|
||||
name string
|
||||
input string
|
||||
start int // start pos of the item
|
||||
pos int // current pos
|
||||
width int // width of last rune
|
||||
tokens []*Token
|
||||
errored bool
|
||||
startline int
|
||||
startcol int
|
||||
line int
|
||||
col int
|
||||
|
||||
inVerbatim bool
|
||||
verbatimName string
|
||||
}
|
||||
|
||||
func (t *Token) String() string {
|
||||
val := t.Val
|
||||
if len(val) > 1000 {
|
||||
val = fmt.Sprintf("%s...%s", val[:10], val[len(val)-5:len(val)])
|
||||
}
|
||||
|
||||
typ := ""
|
||||
switch t.Typ {
|
||||
case TokenHTML:
|
||||
typ = "HTML"
|
||||
case TokenError:
|
||||
typ = "Error"
|
||||
case TokenIdentifier:
|
||||
typ = "Identifier"
|
||||
case TokenKeyword:
|
||||
typ = "Keyword"
|
||||
case TokenNumber:
|
||||
typ = "Number"
|
||||
case TokenString:
|
||||
typ = "String"
|
||||
case TokenSymbol:
|
||||
typ = "Symbol"
|
||||
default:
|
||||
typ = "Unknown"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("<Token Typ=%s (%d) Val='%s' Line=%d Col=%d>",
|
||||
typ, t.Typ, val, t.Line, t.Col)
|
||||
}
|
||||
|
||||
func lex(name string, input string) ([]*Token, *Error) {
|
||||
l := &lexer{
|
||||
name: name,
|
||||
input: input,
|
||||
tokens: make([]*Token, 0, 100),
|
||||
line: 1,
|
||||
col: 1,
|
||||
startline: 1,
|
||||
startcol: 1,
|
||||
}
|
||||
l.run()
|
||||
if l.errored {
|
||||
errtoken := l.tokens[len(l.tokens)-1]
|
||||
return nil, &Error{
|
||||
Filename: name,
|
||||
Line: errtoken.Line,
|
||||
Column: errtoken.Col,
|
||||
Sender: "lexer",
|
||||
OrigError: errors.New(errtoken.Val),
|
||||
}
|
||||
}
|
||||
return l.tokens, nil
|
||||
}
|
||||
|
||||
func (l *lexer) value() string {
|
||||
return l.input[l.start:l.pos]
|
||||
}
|
||||
|
||||
func (l *lexer) length() int {
|
||||
return l.pos - l.start
|
||||
}
|
||||
|
||||
func (l *lexer) emit(t TokenType) {
|
||||
tok := &Token{
|
||||
Filename: l.name,
|
||||
Typ: t,
|
||||
Val: l.value(),
|
||||
Line: l.startline,
|
||||
Col: l.startcol,
|
||||
}
|
||||
|
||||
if t == TokenString {
|
||||
// Escape sequence \" in strings
|
||||
tok.Val = strings.Replace(tok.Val, `\"`, `"`, -1)
|
||||
tok.Val = strings.Replace(tok.Val, `\\`, `\`, -1)
|
||||
}
|
||||
|
||||
l.tokens = append(l.tokens, tok)
|
||||
l.start = l.pos
|
||||
l.startline = l.line
|
||||
l.startcol = l.col
|
||||
}
|
||||
|
||||
func (l *lexer) next() rune {
|
||||
if l.pos >= len(l.input) {
|
||||
l.width = 0
|
||||
return EOF
|
||||
}
|
||||
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
|
||||
l.width = w
|
||||
l.pos += l.width
|
||||
l.col += l.width
|
||||
return r
|
||||
}
|
||||
|
||||
func (l *lexer) backup() {
|
||||
l.pos -= l.width
|
||||
l.col -= l.width
|
||||
}
|
||||
|
||||
func (l *lexer) peek() rune {
|
||||
r := l.next()
|
||||
l.backup()
|
||||
return r
|
||||
}
|
||||
|
||||
func (l *lexer) ignore() {
|
||||
l.start = l.pos
|
||||
l.startline = l.line
|
||||
l.startcol = l.col
|
||||
}
|
||||
|
||||
func (l *lexer) accept(what string) bool {
|
||||
if strings.IndexRune(what, l.next()) >= 0 {
|
||||
return true
|
||||
}
|
||||
l.backup()
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *lexer) acceptRun(what string) {
|
||||
for strings.IndexRune(what, l.next()) >= 0 {
|
||||
}
|
||||
l.backup()
|
||||
}
|
||||
|
||||
func (l *lexer) errorf(format string, args ...interface{}) lexerStateFn {
|
||||
t := &Token{
|
||||
Filename: l.name,
|
||||
Typ: TokenError,
|
||||
Val: fmt.Sprintf(format, args...),
|
||||
Line: l.startline,
|
||||
Col: l.startcol,
|
||||
}
|
||||
l.tokens = append(l.tokens, t)
|
||||
l.errored = true
|
||||
l.startline = l.line
|
||||
l.startcol = l.col
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lexer) eof() bool {
|
||||
return l.start >= len(l.input)-1
|
||||
}
|
||||
|
||||
func (l *lexer) run() {
|
||||
for {
|
||||
// TODO: Support verbatim tag names
|
||||
// https://docs.djangoproject.com/en/dev/ref/templates/builtins/#verbatim
|
||||
if l.inVerbatim {
|
||||
name := l.verbatimName
|
||||
if name != "" {
|
||||
name += " "
|
||||
}
|
||||
if strings.HasPrefix(l.input[l.pos:], fmt.Sprintf("{%% endverbatim %s%%}", name)) { // end verbatim
|
||||
if l.pos > l.start {
|
||||
l.emit(TokenHTML)
|
||||
}
|
||||
w := len("{% endverbatim %}")
|
||||
l.pos += w
|
||||
l.col += w
|
||||
l.ignore()
|
||||
l.inVerbatim = false
|
||||
}
|
||||
} else if strings.HasPrefix(l.input[l.pos:], "{% verbatim %}") { // tag
|
||||
if l.pos > l.start {
|
||||
l.emit(TokenHTML)
|
||||
}
|
||||
l.inVerbatim = true
|
||||
w := len("{% verbatim %}")
|
||||
l.pos += w
|
||||
l.col += w
|
||||
l.ignore()
|
||||
}
|
||||
|
||||
if !l.inVerbatim {
|
||||
// Ignore single-line comments {# ... #}
|
||||
if strings.HasPrefix(l.input[l.pos:], "{#") {
|
||||
if l.pos > l.start {
|
||||
l.emit(TokenHTML)
|
||||
}
|
||||
|
||||
l.pos += 2 // pass '{#'
|
||||
l.col += 2
|
||||
|
||||
for {
|
||||
switch l.peek() {
|
||||
case EOF:
|
||||
l.errorf("Single-line comment not closed.")
|
||||
return
|
||||
case '\n':
|
||||
l.errorf("Newline not permitted in a single-line comment.")
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(l.input[l.pos:], "#}") {
|
||||
l.pos += 2 // pass '#}'
|
||||
l.col += 2
|
||||
break
|
||||
}
|
||||
|
||||
l.next()
|
||||
}
|
||||
l.ignore() // ignore whole comment
|
||||
|
||||
// Comment skipped
|
||||
continue // next token
|
||||
}
|
||||
|
||||
if strings.HasPrefix(l.input[l.pos:], "{{") || // variable
|
||||
strings.HasPrefix(l.input[l.pos:], "{%") { // tag
|
||||
if l.pos > l.start {
|
||||
l.emit(TokenHTML)
|
||||
}
|
||||
l.tokenize()
|
||||
if l.errored {
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
switch l.peek() {
|
||||
case '\n':
|
||||
l.line++
|
||||
l.col = 0
|
||||
}
|
||||
if l.next() == EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if l.pos > l.start {
|
||||
l.emit(TokenHTML)
|
||||
}
|
||||
|
||||
if l.inVerbatim {
|
||||
l.errorf("verbatim-tag not closed, got EOF.")
|
||||
}
|
||||
}
|
||||
|
||||
func (l *lexer) tokenize() {
|
||||
for state := l.stateCode; state != nil; {
|
||||
state = state()
|
||||
}
|
||||
}
|
||||
|
||||
func (l *lexer) stateCode() lexerStateFn {
|
||||
outer_loop:
|
||||
for {
|
||||
switch {
|
||||
case l.accept(tokenSpaceChars):
|
||||
if l.value() == "\n" {
|
||||
return l.errorf("Newline not allowed within tag/variable.")
|
||||
}
|
||||
l.ignore()
|
||||
continue
|
||||
case l.accept(tokenIdentifierChars):
|
||||
return l.stateIdentifier
|
||||
case l.accept(tokenDigits):
|
||||
return l.stateNumber
|
||||
case l.accept(`"'`):
|
||||
return l.stateString
|
||||
}
|
||||
|
||||
// Check for symbol
|
||||
for _, sym := range TokenSymbols {
|
||||
if strings.HasPrefix(l.input[l.start:], sym) {
|
||||
l.pos += len(sym)
|
||||
l.col += l.length()
|
||||
l.emit(TokenSymbol)
|
||||
|
||||
if sym == "%}" || sym == "}}" {
|
||||
// Tag/variable end, return after emit
|
||||
return nil
|
||||
}
|
||||
|
||||
continue outer_loop
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
// Normal shut down
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *lexer) stateIdentifier() lexerStateFn {
|
||||
l.acceptRun(tokenIdentifierChars)
|
||||
l.acceptRun(tokenIdentifierCharsWithDigits)
|
||||
for _, kw := range TokenKeywords {
|
||||
if kw == l.value() {
|
||||
l.emit(TokenKeyword)
|
||||
return l.stateCode
|
||||
}
|
||||
}
|
||||
l.emit(TokenIdentifier)
|
||||
return l.stateCode
|
||||
}
|
||||
|
||||
func (l *lexer) stateNumber() lexerStateFn {
|
||||
l.acceptRun(tokenDigits)
|
||||
if l.accept(tokenIdentifierCharsWithDigits) {
|
||||
// This seems to be an identifier starting with a number.
|
||||
// See https://github.com/flosch/pongo2/issues/151
|
||||
return l.stateIdentifier()
|
||||
}
|
||||
/*
|
||||
Maybe context-sensitive number lexing?
|
||||
* comments.0.Text // first comment
|
||||
* usercomments.1.0 // second user, first comment
|
||||
* if (score >= 8.5) // 8.5 as a number
|
||||
|
||||
if l.peek() == '.' {
|
||||
l.accept(".")
|
||||
if !l.accept(tokenDigits) {
|
||||
return l.errorf("Malformed number.")
|
||||
}
|
||||
l.acceptRun(tokenDigits)
|
||||
}
|
||||
*/
|
||||
l.emit(TokenNumber)
|
||||
return l.stateCode
|
||||
}
|
||||
|
||||
func (l *lexer) stateString() lexerStateFn {
|
||||
quotationMark := l.value()
|
||||
l.ignore()
|
||||
l.startcol-- // we're starting the position at the first "
|
||||
for !l.accept(quotationMark) {
|
||||
switch l.next() {
|
||||
case '\\':
|
||||
// escape sequence
|
||||
switch l.peek() {
|
||||
case '"', '\\':
|
||||
l.next()
|
||||
default:
|
||||
return l.errorf("Unknown escape sequence: \\%c", l.peek())
|
||||
}
|
||||
case EOF:
|
||||
return l.errorf("Unexpected EOF, string not closed.")
|
||||
case '\n':
|
||||
return l.errorf("Newline in string is not allowed.")
|
||||
}
|
||||
}
|
||||
l.backup()
|
||||
l.emit(TokenString)
|
||||
|
||||
l.next()
|
||||
l.ignore()
|
||||
|
||||
return l.stateCode
|
||||
}
|
16
vendor/github.com/flosch/pongo2/nodes.go
generated
vendored
Normal file
16
vendor/github.com/flosch/pongo2/nodes.go
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
package pongo2
|
||||
|
||||
// The root document
|
||||
type nodeDocument struct {
|
||||
Nodes []INode
|
||||
}
|
||||
|
||||
func (doc *nodeDocument) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
for _, n := range doc.Nodes {
|
||||
err := n.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
10
vendor/github.com/flosch/pongo2/nodes_html.go
generated
vendored
Normal file
10
vendor/github.com/flosch/pongo2/nodes_html.go
generated
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
package pongo2
|
||||
|
||||
type nodeHTML struct {
|
||||
token *Token
|
||||
}
|
||||
|
||||
func (n *nodeHTML) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
writer.WriteString(n.token.Val)
|
||||
return nil
|
||||
}
|
16
vendor/github.com/flosch/pongo2/nodes_wrapper.go
generated
vendored
Normal file
16
vendor/github.com/flosch/pongo2/nodes_wrapper.go
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
package pongo2
|
||||
|
||||
type NodeWrapper struct {
|
||||
Endtag string
|
||||
nodes []INode
|
||||
}
|
||||
|
||||
func (wrapper *NodeWrapper) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
for _, n := range wrapper.nodes {
|
||||
err := n.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
309
vendor/github.com/flosch/pongo2/parser.go
generated
vendored
Normal file
309
vendor/github.com/flosch/pongo2/parser.go
generated
vendored
Normal file
|
@ -0,0 +1,309 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
type INode interface {
|
||||
Execute(*ExecutionContext, TemplateWriter) *Error
|
||||
}
|
||||
|
||||
type IEvaluator interface {
|
||||
INode
|
||||
GetPositionToken() *Token
|
||||
Evaluate(*ExecutionContext) (*Value, *Error)
|
||||
FilterApplied(name string) bool
|
||||
}
|
||||
|
||||
// The parser provides you a comprehensive and easy tool to
|
||||
// work with the template document and arguments provided by
|
||||
// the user for your custom tag.
|
||||
//
|
||||
// The parser works on a token list which will be provided by pongo2.
|
||||
// A token is a unit you can work with. Tokens are either of type identifier,
|
||||
// string, number, keyword, HTML or symbol.
|
||||
//
|
||||
// (See Token's documentation for more about tokens)
|
||||
type Parser struct {
|
||||
name string
|
||||
idx int
|
||||
tokens []*Token
|
||||
lastToken *Token
|
||||
|
||||
// if the parser parses a template document, here will be
|
||||
// a reference to it (needed to access the template through Tags)
|
||||
template *Template
|
||||
}
|
||||
|
||||
// Creates a new parser to parse tokens.
|
||||
// Used inside pongo2 to parse documents and to provide an easy-to-use
|
||||
// parser for tag authors
|
||||
func newParser(name string, tokens []*Token, template *Template) *Parser {
|
||||
p := &Parser{
|
||||
name: name,
|
||||
tokens: tokens,
|
||||
template: template,
|
||||
}
|
||||
if len(tokens) > 0 {
|
||||
p.lastToken = tokens[len(tokens)-1]
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Consume one token. It will be gone forever.
|
||||
func (p *Parser) Consume() {
|
||||
p.ConsumeN(1)
|
||||
}
|
||||
|
||||
// Consume N tokens. They will be gone forever.
|
||||
func (p *Parser) ConsumeN(count int) {
|
||||
p.idx += count
|
||||
}
|
||||
|
||||
// Returns the current token.
|
||||
func (p *Parser) Current() *Token {
|
||||
return p.Get(p.idx)
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type matches.
|
||||
// Consumes this token on success.
|
||||
func (p *Parser) MatchType(typ TokenType) *Token {
|
||||
if t := p.PeekType(typ); t != nil {
|
||||
p.Consume()
|
||||
return t
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type AND value matches.
|
||||
// Consumes this token on success.
|
||||
func (p *Parser) Match(typ TokenType, val string) *Token {
|
||||
if t := p.Peek(typ, val); t != nil {
|
||||
p.Consume()
|
||||
return t
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type AND *one* of
|
||||
// the given values matches.
|
||||
// Consumes this token on success.
|
||||
func (p *Parser) MatchOne(typ TokenType, vals ...string) *Token {
|
||||
for _, val := range vals {
|
||||
if t := p.Peek(typ, val); t != nil {
|
||||
p.Consume()
|
||||
return t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type matches.
|
||||
// It DOES NOT consume the token.
|
||||
func (p *Parser) PeekType(typ TokenType) *Token {
|
||||
return p.PeekTypeN(0, typ)
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type AND value matches.
|
||||
// It DOES NOT consume the token.
|
||||
func (p *Parser) Peek(typ TokenType, val string) *Token {
|
||||
return p.PeekN(0, typ, val)
|
||||
}
|
||||
|
||||
// Returns the CURRENT token if the given type AND *one* of
|
||||
// the given values matches.
|
||||
// It DOES NOT consume the token.
|
||||
func (p *Parser) PeekOne(typ TokenType, vals ...string) *Token {
|
||||
for _, v := range vals {
|
||||
t := p.PeekN(0, typ, v)
|
||||
if t != nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the tokens[current position + shift] token if the
|
||||
// given type AND value matches for that token.
|
||||
// DOES NOT consume the token.
|
||||
func (p *Parser) PeekN(shift int, typ TokenType, val string) *Token {
|
||||
t := p.Get(p.idx + shift)
|
||||
if t != nil {
|
||||
if t.Typ == typ && t.Val == val {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the tokens[current position + shift] token if the given type matches.
|
||||
// DOES NOT consume the token for that token.
|
||||
func (p *Parser) PeekTypeN(shift int, typ TokenType) *Token {
|
||||
t := p.Get(p.idx + shift)
|
||||
if t != nil {
|
||||
if t.Typ == typ {
|
||||
return t
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the UNCONSUMED token count.
|
||||
func (p *Parser) Remaining() int {
|
||||
return len(p.tokens) - p.idx
|
||||
}
|
||||
|
||||
// Returns the total token count.
|
||||
func (p *Parser) Count() int {
|
||||
return len(p.tokens)
|
||||
}
|
||||
|
||||
// Returns tokens[i] or NIL (if i >= len(tokens))
|
||||
func (p *Parser) Get(i int) *Token {
|
||||
if i < len(p.tokens) {
|
||||
return p.tokens[i]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns tokens[current-position + shift] or NIL
|
||||
// (if (current-position + i) >= len(tokens))
|
||||
func (p *Parser) GetR(shift int) *Token {
|
||||
i := p.idx + shift
|
||||
return p.Get(i)
|
||||
}
|
||||
|
||||
// Error produces a nice error message and returns an error-object.
|
||||
// The 'token'-argument is optional. If provided, it will take
|
||||
// the token's position information. If not provided, it will
|
||||
// automatically use the CURRENT token's position information.
|
||||
func (p *Parser) Error(msg string, token *Token) *Error {
|
||||
if token == nil {
|
||||
// Set current token
|
||||
token = p.Current()
|
||||
if token == nil {
|
||||
// Set to last token
|
||||
if len(p.tokens) > 0 {
|
||||
token = p.tokens[len(p.tokens)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
var line, col int
|
||||
if token != nil {
|
||||
line = token.Line
|
||||
col = token.Col
|
||||
}
|
||||
return &Error{
|
||||
Template: p.template,
|
||||
Filename: p.name,
|
||||
Sender: "parser",
|
||||
Line: line,
|
||||
Column: col,
|
||||
Token: token,
|
||||
OrigError: errors.New(msg),
|
||||
}
|
||||
}
|
||||
|
||||
// Wraps all nodes between starting tag and "{% endtag %}" and provides
|
||||
// one simple interface to execute the wrapped nodes.
|
||||
// It returns a parser to process provided arguments to the tag.
|
||||
func (p *Parser) WrapUntilTag(names ...string) (*NodeWrapper, *Parser, *Error) {
|
||||
wrapper := &NodeWrapper{}
|
||||
|
||||
var tagArgs []*Token
|
||||
|
||||
for p.Remaining() > 0 {
|
||||
// New tag, check whether we have to stop wrapping here
|
||||
if p.Peek(TokenSymbol, "{%") != nil {
|
||||
tagIdent := p.PeekTypeN(1, TokenIdentifier)
|
||||
|
||||
if tagIdent != nil {
|
||||
// We've found a (!) end-tag
|
||||
|
||||
found := false
|
||||
for _, n := range names {
|
||||
if tagIdent.Val == n {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// We only process the tag if we've found an end tag
|
||||
if found {
|
||||
// Okay, endtag found.
|
||||
p.ConsumeN(2) // '{%' tagname
|
||||
|
||||
for {
|
||||
if p.Match(TokenSymbol, "%}") != nil {
|
||||
// Okay, end the wrapping here
|
||||
wrapper.Endtag = tagIdent.Val
|
||||
return wrapper, newParser(p.template.name, tagArgs, p.template), nil
|
||||
}
|
||||
t := p.Current()
|
||||
p.Consume()
|
||||
if t == nil {
|
||||
return nil, nil, p.Error("Unexpected EOF.", p.lastToken)
|
||||
}
|
||||
tagArgs = append(tagArgs, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Otherwise process next element to be wrapped
|
||||
node, err := p.parseDocElement()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
wrapper.nodes = append(wrapper.nodes, node)
|
||||
}
|
||||
|
||||
return nil, nil, p.Error(fmt.Sprintf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")),
|
||||
p.lastToken)
|
||||
}
|
||||
|
||||
// Skips all nodes between starting tag and "{% endtag %}"
|
||||
func (p *Parser) SkipUntilTag(names ...string) *Error {
|
||||
for p.Remaining() > 0 {
|
||||
// New tag, check whether we have to stop wrapping here
|
||||
if p.Peek(TokenSymbol, "{%") != nil {
|
||||
tagIdent := p.PeekTypeN(1, TokenIdentifier)
|
||||
|
||||
if tagIdent != nil {
|
||||
// We've found a (!) end-tag
|
||||
|
||||
found := false
|
||||
for _, n := range names {
|
||||
if tagIdent.Val == n {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// We only process the tag if we've found an end tag
|
||||
if found {
|
||||
// Okay, endtag found.
|
||||
p.ConsumeN(2) // '{%' tagname
|
||||
|
||||
for {
|
||||
if p.Match(TokenSymbol, "%}") != nil {
|
||||
// Done skipping, exit.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t := p.Current()
|
||||
p.Consume()
|
||||
if t == nil {
|
||||
return p.Error("Unexpected EOF.", p.lastToken)
|
||||
}
|
||||
}
|
||||
|
||||
return p.Error(fmt.Sprintf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")), p.lastToken)
|
||||
}
|
54
vendor/github.com/flosch/pongo2/parser_document.go
generated
vendored
Normal file
54
vendor/github.com/flosch/pongo2/parser_document.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package pongo2
|
||||
|
||||
// Doc = { ( Filter | Tag | HTML ) }
|
||||
func (p *Parser) parseDocElement() (INode, *Error) {
|
||||
t := p.Current()
|
||||
|
||||
switch t.Typ {
|
||||
case TokenHTML:
|
||||
p.Consume() // consume HTML element
|
||||
return &nodeHTML{token: t}, nil
|
||||
case TokenSymbol:
|
||||
switch t.Val {
|
||||
case "{{":
|
||||
// parse variable
|
||||
variable, err := p.parseVariableElement()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return variable, nil
|
||||
case "{%":
|
||||
// parse tag
|
||||
tag, err := p.parseTagElement()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tag, nil
|
||||
}
|
||||
}
|
||||
return nil, p.Error("Unexpected token (only HTML/tags/filters in templates allowed)", t)
|
||||
}
|
||||
|
||||
func (tpl *Template) parse() *Error {
|
||||
tpl.parser = newParser(tpl.name, tpl.tokens, tpl)
|
||||
doc, err := tpl.parser.parseDocument()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tpl.root = doc
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseDocument() (*nodeDocument, *Error) {
|
||||
doc := &nodeDocument{}
|
||||
|
||||
for p.Remaining() > 0 {
|
||||
node, err := p.parseDocElement()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc.Nodes = append(doc.Nodes, node)
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
491
vendor/github.com/flosch/pongo2/parser_expression.go
generated
vendored
Normal file
491
vendor/github.com/flosch/pongo2/parser_expression.go
generated
vendored
Normal file
|
@ -0,0 +1,491 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Expression struct {
|
||||
// TODO: Add location token?
|
||||
expr1 IEvaluator
|
||||
expr2 IEvaluator
|
||||
opToken *Token
|
||||
}
|
||||
|
||||
type relationalExpression struct {
|
||||
// TODO: Add location token?
|
||||
expr1 IEvaluator
|
||||
expr2 IEvaluator
|
||||
opToken *Token
|
||||
}
|
||||
|
||||
type simpleExpression struct {
|
||||
negate bool
|
||||
negativeSign bool
|
||||
term1 IEvaluator
|
||||
term2 IEvaluator
|
||||
opToken *Token
|
||||
}
|
||||
|
||||
type term struct {
|
||||
// TODO: Add location token?
|
||||
factor1 IEvaluator
|
||||
factor2 IEvaluator
|
||||
opToken *Token
|
||||
}
|
||||
|
||||
type power struct {
|
||||
// TODO: Add location token?
|
||||
power1 IEvaluator
|
||||
power2 IEvaluator
|
||||
}
|
||||
|
||||
func (expr *Expression) FilterApplied(name string) bool {
|
||||
return expr.expr1.FilterApplied(name) && (expr.expr2 == nil ||
|
||||
(expr.expr2 != nil && expr.expr2.FilterApplied(name)))
|
||||
}
|
||||
|
||||
func (expr *relationalExpression) FilterApplied(name string) bool {
|
||||
return expr.expr1.FilterApplied(name) && (expr.expr2 == nil ||
|
||||
(expr.expr2 != nil && expr.expr2.FilterApplied(name)))
|
||||
}
|
||||
|
||||
func (expr *simpleExpression) FilterApplied(name string) bool {
|
||||
return expr.term1.FilterApplied(name) && (expr.term2 == nil ||
|
||||
(expr.term2 != nil && expr.term2.FilterApplied(name)))
|
||||
}
|
||||
|
||||
func (expr *term) FilterApplied(name string) bool {
|
||||
return expr.factor1.FilterApplied(name) && (expr.factor2 == nil ||
|
||||
(expr.factor2 != nil && expr.factor2.FilterApplied(name)))
|
||||
}
|
||||
|
||||
func (expr *power) FilterApplied(name string) bool {
|
||||
return expr.power1.FilterApplied(name) && (expr.power2 == nil ||
|
||||
(expr.power2 != nil && expr.power2.FilterApplied(name)))
|
||||
}
|
||||
|
||||
func (expr *Expression) GetPositionToken() *Token {
|
||||
return expr.expr1.GetPositionToken()
|
||||
}
|
||||
|
||||
func (expr *relationalExpression) GetPositionToken() *Token {
|
||||
return expr.expr1.GetPositionToken()
|
||||
}
|
||||
|
||||
func (expr *simpleExpression) GetPositionToken() *Token {
|
||||
return expr.term1.GetPositionToken()
|
||||
}
|
||||
|
||||
func (expr *term) GetPositionToken() *Token {
|
||||
return expr.factor1.GetPositionToken()
|
||||
}
|
||||
|
||||
func (expr *power) GetPositionToken() *Token {
|
||||
return expr.power1.GetPositionToken()
|
||||
}
|
||||
|
||||
func (expr *Expression) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
value, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.WriteString(value.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (expr *relationalExpression) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
value, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.WriteString(value.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (expr *simpleExpression) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
value, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.WriteString(value.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (expr *term) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
value, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.WriteString(value.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (expr *power) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
value, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.WriteString(value.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (expr *Expression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
|
||||
v1, err := expr.expr1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expr.expr2 != nil {
|
||||
v2, err := expr.expr2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch expr.opToken.Val {
|
||||
case "and", "&&":
|
||||
return AsValue(v1.IsTrue() && v2.IsTrue()), nil
|
||||
case "or", "||":
|
||||
return AsValue(v1.IsTrue() || v2.IsTrue()), nil
|
||||
default:
|
||||
return nil, ctx.Error(fmt.Sprintf("unimplemented: %s", expr.opToken.Val), expr.opToken)
|
||||
}
|
||||
} else {
|
||||
return v1, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (expr *relationalExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
|
||||
v1, err := expr.expr1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expr.expr2 != nil {
|
||||
v2, err := expr.expr2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch expr.opToken.Val {
|
||||
case "<=":
|
||||
if v1.IsFloat() || v2.IsFloat() {
|
||||
return AsValue(v1.Float() <= v2.Float()), nil
|
||||
}
|
||||
return AsValue(v1.Integer() <= v2.Integer()), nil
|
||||
case ">=":
|
||||
if v1.IsFloat() || v2.IsFloat() {
|
||||
return AsValue(v1.Float() >= v2.Float()), nil
|
||||
}
|
||||
return AsValue(v1.Integer() >= v2.Integer()), nil
|
||||
case "==":
|
||||
return AsValue(v1.EqualValueTo(v2)), nil
|
||||
case ">":
|
||||
if v1.IsFloat() || v2.IsFloat() {
|
||||
return AsValue(v1.Float() > v2.Float()), nil
|
||||
}
|
||||
return AsValue(v1.Integer() > v2.Integer()), nil
|
||||
case "<":
|
||||
if v1.IsFloat() || v2.IsFloat() {
|
||||
return AsValue(v1.Float() < v2.Float()), nil
|
||||
}
|
||||
return AsValue(v1.Integer() < v2.Integer()), nil
|
||||
case "!=", "<>":
|
||||
return AsValue(!v1.EqualValueTo(v2)), nil
|
||||
case "in":
|
||||
return AsValue(v2.Contains(v1)), nil
|
||||
default:
|
||||
return nil, ctx.Error(fmt.Sprintf("unimplemented: %s", expr.opToken.Val), expr.opToken)
|
||||
}
|
||||
} else {
|
||||
return v1, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (expr *simpleExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
|
||||
t1, err := expr.term1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := t1
|
||||
|
||||
if expr.negate {
|
||||
result = result.Negate()
|
||||
}
|
||||
|
||||
if expr.negativeSign {
|
||||
if result.IsNumber() {
|
||||
switch {
|
||||
case result.IsFloat():
|
||||
result = AsValue(-1 * result.Float())
|
||||
case result.IsInteger():
|
||||
result = AsValue(-1 * result.Integer())
|
||||
default:
|
||||
return nil, ctx.Error("Operation between a number and a non-(float/integer) is not possible", nil)
|
||||
}
|
||||
} else {
|
||||
return nil, ctx.Error("Negative sign on a non-number expression", expr.GetPositionToken())
|
||||
}
|
||||
}
|
||||
|
||||
if expr.term2 != nil {
|
||||
t2, err := expr.term2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch expr.opToken.Val {
|
||||
case "+":
|
||||
if result.IsFloat() || t2.IsFloat() {
|
||||
// Result will be a float
|
||||
return AsValue(result.Float() + t2.Float()), nil
|
||||
}
|
||||
// Result will be an integer
|
||||
return AsValue(result.Integer() + t2.Integer()), nil
|
||||
case "-":
|
||||
if result.IsFloat() || t2.IsFloat() {
|
||||
// Result will be a float
|
||||
return AsValue(result.Float() - t2.Float()), nil
|
||||
}
|
||||
// Result will be an integer
|
||||
return AsValue(result.Integer() - t2.Integer()), nil
|
||||
default:
|
||||
return nil, ctx.Error("Unimplemented", expr.GetPositionToken())
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (expr *term) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
|
||||
f1, err := expr.factor1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expr.factor2 != nil {
|
||||
f2, err := expr.factor2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch expr.opToken.Val {
|
||||
case "*":
|
||||
if f1.IsFloat() || f2.IsFloat() {
|
||||
// Result will be float
|
||||
return AsValue(f1.Float() * f2.Float()), nil
|
||||
}
|
||||
// Result will be int
|
||||
return AsValue(f1.Integer() * f2.Integer()), nil
|
||||
case "/":
|
||||
if f1.IsFloat() || f2.IsFloat() {
|
||||
// Result will be float
|
||||
return AsValue(f1.Float() / f2.Float()), nil
|
||||
}
|
||||
// Result will be int
|
||||
return AsValue(f1.Integer() / f2.Integer()), nil
|
||||
case "%":
|
||||
// Result will be int
|
||||
return AsValue(f1.Integer() % f2.Integer()), nil
|
||||
default:
|
||||
return nil, ctx.Error("unimplemented", expr.opToken)
|
||||
}
|
||||
} else {
|
||||
return f1, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (expr *power) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
|
||||
p1, err := expr.power1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if expr.power2 != nil {
|
||||
p2, err := expr.power2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsValue(math.Pow(p1.Float(), p2.Float())), nil
|
||||
}
|
||||
return p1, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseFactor() (IEvaluator, *Error) {
|
||||
if p.Match(TokenSymbol, "(") != nil {
|
||||
expr, err := p.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.Match(TokenSymbol, ")") == nil {
|
||||
return nil, p.Error("Closing bracket expected after expression", nil)
|
||||
}
|
||||
return expr, nil
|
||||
}
|
||||
|
||||
return p.parseVariableOrLiteralWithFilter()
|
||||
}
|
||||
|
||||
func (p *Parser) parsePower() (IEvaluator, *Error) {
|
||||
pw := new(power)
|
||||
|
||||
power1, err := p.parseFactor()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pw.power1 = power1
|
||||
|
||||
if p.Match(TokenSymbol, "^") != nil {
|
||||
power2, err := p.parsePower()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pw.power2 = power2
|
||||
}
|
||||
|
||||
if pw.power2 == nil {
|
||||
// Shortcut for faster evaluation
|
||||
return pw.power1, nil
|
||||
}
|
||||
|
||||
return pw, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseTerm() (IEvaluator, *Error) {
|
||||
returnTerm := new(term)
|
||||
|
||||
factor1, err := p.parsePower()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
returnTerm.factor1 = factor1
|
||||
|
||||
for p.PeekOne(TokenSymbol, "*", "/", "%") != nil {
|
||||
if returnTerm.opToken != nil {
|
||||
// Create new sub-term
|
||||
returnTerm = &term{
|
||||
factor1: returnTerm,
|
||||
}
|
||||
}
|
||||
|
||||
op := p.Current()
|
||||
p.Consume()
|
||||
|
||||
factor2, err := p.parsePower()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
returnTerm.opToken = op
|
||||
returnTerm.factor2 = factor2
|
||||
}
|
||||
|
||||
if returnTerm.opToken == nil {
|
||||
// Shortcut for faster evaluation
|
||||
return returnTerm.factor1, nil
|
||||
}
|
||||
|
||||
return returnTerm, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseSimpleExpression() (IEvaluator, *Error) {
|
||||
expr := new(simpleExpression)
|
||||
|
||||
if sign := p.MatchOne(TokenSymbol, "+", "-"); sign != nil {
|
||||
if sign.Val == "-" {
|
||||
expr.negativeSign = true
|
||||
}
|
||||
}
|
||||
|
||||
if p.Match(TokenSymbol, "!") != nil || p.Match(TokenKeyword, "not") != nil {
|
||||
expr.negate = true
|
||||
}
|
||||
|
||||
term1, err := p.parseTerm()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expr.term1 = term1
|
||||
|
||||
for p.PeekOne(TokenSymbol, "+", "-") != nil {
|
||||
if expr.opToken != nil {
|
||||
// New sub expr
|
||||
expr = &simpleExpression{
|
||||
term1: expr,
|
||||
}
|
||||
}
|
||||
|
||||
op := p.Current()
|
||||
p.Consume()
|
||||
|
||||
term2, err := p.parseTerm()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
expr.term2 = term2
|
||||
expr.opToken = op
|
||||
}
|
||||
|
||||
if expr.negate == false && expr.negativeSign == false && expr.term2 == nil {
|
||||
// Shortcut for faster evaluation
|
||||
return expr.term1, nil
|
||||
}
|
||||
|
||||
return expr, nil
|
||||
}
|
||||
|
||||
func (p *Parser) parseRelationalExpression() (IEvaluator, *Error) {
|
||||
expr1, err := p.parseSimpleExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
expr := &relationalExpression{
|
||||
expr1: expr1,
|
||||
}
|
||||
|
||||
if t := p.MatchOne(TokenSymbol, "==", "<=", ">=", "!=", "<>", ">", "<"); t != nil {
|
||||
expr2, err := p.parseRelationalExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expr.opToken = t
|
||||
expr.expr2 = expr2
|
||||
} else if t := p.MatchOne(TokenKeyword, "in"); t != nil {
|
||||
expr2, err := p.parseSimpleExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expr.opToken = t
|
||||
expr.expr2 = expr2
|
||||
}
|
||||
|
||||
if expr.expr2 == nil {
|
||||
// Shortcut for faster evaluation
|
||||
return expr.expr1, nil
|
||||
}
|
||||
|
||||
return expr, nil
|
||||
}
|
||||
|
||||
func (p *Parser) ParseExpression() (IEvaluator, *Error) {
|
||||
rexpr1, err := p.parseRelationalExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exp := &Expression{
|
||||
expr1: rexpr1,
|
||||
}
|
||||
|
||||
if p.PeekOne(TokenSymbol, "&&", "||") != nil || p.PeekOne(TokenKeyword, "and", "or") != nil {
|
||||
op := p.Current()
|
||||
p.Consume()
|
||||
expr2, err := p.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
exp.expr2 = expr2
|
||||
exp.opToken = op
|
||||
}
|
||||
|
||||
if exp.expr2 == nil {
|
||||
// Shortcut for faster evaluation
|
||||
return exp.expr1, nil
|
||||
}
|
||||
|
||||
return exp, nil
|
||||
}
|
14
vendor/github.com/flosch/pongo2/pongo2.go
generated
vendored
Normal file
14
vendor/github.com/flosch/pongo2/pongo2.go
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
package pongo2
|
||||
|
||||
// Version string
|
||||
const Version = "dev"
|
||||
|
||||
// Must panics, if a Template couldn't successfully parsed. This is how you
|
||||
// would use it:
|
||||
// var baseTemplate = pongo2.Must(pongo2.FromFile("templates/base.html"))
|
||||
func Must(tpl *Template, err error) *Template {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return tpl
|
||||
}
|
29
vendor/github.com/flosch/pongo2/pongo2_issues_test.go
generated
vendored
Normal file
29
vendor/github.com/flosch/pongo2/pongo2_issues_test.go
generated
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
package pongo2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
)
|
||||
|
||||
func TestIssue151(t *testing.T) {
|
||||
tpl, err := pongo2.FromString("{{ mydict.51232_3 }}{{ 12345_123}}{{ 995189baz }}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
str, err := tpl.Execute(pongo2.Context{
|
||||
"mydict": map[string]string{
|
||||
"51232_3": "foo",
|
||||
},
|
||||
"12345_123": "bar",
|
||||
"995189baz": "baz",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if str != "foobarbaz" {
|
||||
t.Fatalf("Expected output 'foobarbaz', but got '%s'.", str)
|
||||
}
|
||||
}
|
534
vendor/github.com/flosch/pongo2/pongo2_template_test.go
generated
vendored
Normal file
534
vendor/github.com/flosch/pongo2/pongo2_template_test.go
generated
vendored
Normal file
|
@ -0,0 +1,534 @@
|
|||
package pongo2_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
)
|
||||
|
||||
var adminList = []string{"user2"}
|
||||
|
||||
var time1 = time.Date(2014, 06, 10, 15, 30, 15, 0, time.UTC)
|
||||
var time2 = time.Date(2011, 03, 21, 8, 37, 56, 12, time.UTC)
|
||||
|
||||
type post struct {
|
||||
Text string
|
||||
Created time.Time
|
||||
}
|
||||
|
||||
type user struct {
|
||||
Name string
|
||||
Validated bool
|
||||
}
|
||||
|
||||
type comment struct {
|
||||
Author *user
|
||||
Date time.Time
|
||||
Text string
|
||||
}
|
||||
|
||||
func isAdmin(u *user) bool {
|
||||
for _, a := range adminList {
|
||||
if a == u.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *user) Is_admin() *pongo2.Value {
|
||||
return pongo2.AsValue(isAdmin(u))
|
||||
}
|
||||
|
||||
func (u *user) Is_admin2() bool {
|
||||
return isAdmin(u)
|
||||
}
|
||||
|
||||
func (p *post) String() string {
|
||||
return ":-)"
|
||||
}
|
||||
|
||||
/*
|
||||
* Start setup sandbox
|
||||
*/
|
||||
|
||||
type tagSandboxDemoTag struct {
|
||||
}
|
||||
|
||||
func (node *tagSandboxDemoTag) Execute(ctx *pongo2.ExecutionContext, writer pongo2.TemplateWriter) *pongo2.Error {
|
||||
writer.WriteString("hello")
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagSandboxDemoTagParser(doc *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
|
||||
return &tagSandboxDemoTag{}, nil
|
||||
}
|
||||
|
||||
func BannedFilterFn(in *pongo2.Value, params *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
pongo2.DefaultSet.Debug = true
|
||||
|
||||
pongo2.RegisterFilter("banned_filter", BannedFilterFn)
|
||||
pongo2.RegisterFilter("unbanned_filter", BannedFilterFn)
|
||||
pongo2.RegisterTag("banned_tag", tagSandboxDemoTagParser)
|
||||
pongo2.RegisterTag("unbanned_tag", tagSandboxDemoTagParser)
|
||||
|
||||
pongo2.DefaultSet.BanFilter("banned_filter")
|
||||
pongo2.DefaultSet.BanTag("banned_tag")
|
||||
|
||||
f, err := ioutil.TempFile("/tmp/", "pongo2_")
|
||||
if err != nil {
|
||||
panic("cannot write to /tmp/")
|
||||
}
|
||||
f.Write([]byte("Hello from pongo2"))
|
||||
pongo2.DefaultSet.Globals["temp_file"] = f.Name()
|
||||
}
|
||||
|
||||
/*
|
||||
* End setup sandbox
|
||||
*/
|
||||
|
||||
var tplContext = pongo2.Context{
|
||||
"number": 11,
|
||||
"simple": map[string]interface{}{
|
||||
"number": 42,
|
||||
"name": "john doe",
|
||||
"included_file": "INCLUDES.helper",
|
||||
"included_file_not_exists": "INCLUDES.helper.not_exists",
|
||||
"nil": nil,
|
||||
"uint": uint(8),
|
||||
"float": float64(3.1415),
|
||||
"str": "string",
|
||||
"chinese_hello_world": "你好世界",
|
||||
"bool_true": true,
|
||||
"bool_false": false,
|
||||
"newline_text": `this is a text
|
||||
with a new line in it`,
|
||||
"long_text": `This is a simple text.
|
||||
|
||||
This too, as a paragraph.
|
||||
Right?
|
||||
|
||||
Yep!`,
|
||||
"escape_js_test": `escape sequences \r\n\'\" special chars "?!=$<>`,
|
||||
"one_item_list": []int{99},
|
||||
"multiple_item_list": []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
|
||||
"unsorted_int_list": []int{192, 581, 22, 1, 249, 9999, 1828591, 8271},
|
||||
"fixed_item_list": [...]int{1, 2, 3, 4},
|
||||
"misc_list": []interface{}{"Hello", 99, 3.14, "good"},
|
||||
"escape_text": "This is \\a Test. \"Yep\". 'Yep'.",
|
||||
"xss": "<script>alert(\"uh oh\");</script>",
|
||||
"intmap": map[int]string{
|
||||
1: "one",
|
||||
5: "five",
|
||||
2: "two",
|
||||
},
|
||||
"strmap": map[string]string{
|
||||
"abc": "def",
|
||||
"bcd": "efg",
|
||||
"zab": "cde",
|
||||
"gh": "kqm",
|
||||
"ukq": "qqa",
|
||||
"aab": "aba",
|
||||
},
|
||||
"func_add": func(a, b int) int {
|
||||
return a + b
|
||||
},
|
||||
"func_add_iface": func(a, b interface{}) interface{} {
|
||||
return a.(int) + b.(int)
|
||||
},
|
||||
"func_variadic": func(msg string, args ...interface{}) string {
|
||||
return fmt.Sprintf(msg, args...)
|
||||
},
|
||||
"func_variadic_sum_int": func(args ...int) int {
|
||||
// Create a sum
|
||||
s := 0
|
||||
for _, i := range args {
|
||||
s += i
|
||||
}
|
||||
return s
|
||||
},
|
||||
"func_variadic_sum_int2": func(args ...*pongo2.Value) *pongo2.Value {
|
||||
// Create a sum
|
||||
s := 0
|
||||
for _, i := range args {
|
||||
s += i.Integer()
|
||||
}
|
||||
return pongo2.AsValue(s)
|
||||
},
|
||||
},
|
||||
"complex": map[string]interface{}{
|
||||
"is_admin": isAdmin,
|
||||
"post": post{
|
||||
Text: "<h2>Hello!</h2><p>Welcome to my new blog page. I'm using pongo2 which supports {{ variables }} and {% tags %}.</p>",
|
||||
Created: time2,
|
||||
},
|
||||
"comments": []*comment{
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user1",
|
||||
Validated: true,
|
||||
},
|
||||
Date: time1,
|
||||
Text: "\"pongo2 is nice!\"",
|
||||
},
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user2",
|
||||
Validated: true,
|
||||
},
|
||||
Date: time2,
|
||||
Text: "comment2 with <script>unsafe</script> tags in it",
|
||||
},
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user3",
|
||||
Validated: false,
|
||||
},
|
||||
Date: time1,
|
||||
Text: "<b>hello!</b> there",
|
||||
},
|
||||
},
|
||||
"comments2": []*comment{
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user1",
|
||||
Validated: true,
|
||||
},
|
||||
Date: time2,
|
||||
Text: "\"pongo2 is nice!\"",
|
||||
},
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user1",
|
||||
Validated: true,
|
||||
},
|
||||
Date: time1,
|
||||
Text: "comment2 with <script>unsafe</script> tags in it",
|
||||
},
|
||||
&comment{
|
||||
Author: &user{
|
||||
Name: "user3",
|
||||
Validated: false,
|
||||
},
|
||||
Date: time1,
|
||||
Text: "<b>hello!</b> there",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestTemplates(t *testing.T) {
|
||||
// Add a global to the default set
|
||||
pongo2.Globals["this_is_a_global_variable"] = "this is a global text"
|
||||
|
||||
matches, err := filepath.Glob("./template_tests/*.tpl")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for idx, match := range matches {
|
||||
t.Logf("[Template %3d] Testing '%s'", idx+1, match)
|
||||
tpl, err := pongo2.FromFile(match)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on FromFile('%s'): %s", match, err.Error())
|
||||
}
|
||||
testFilename := fmt.Sprintf("%s.out", match)
|
||||
testOut, rerr := ioutil.ReadFile(testFilename)
|
||||
if rerr != nil {
|
||||
t.Fatalf("Error on ReadFile('%s'): %s", testFilename, rerr.Error())
|
||||
}
|
||||
tplOut, err := tpl.ExecuteBytes(tplContext)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on Execute('%s'): %s", match, err.Error())
|
||||
}
|
||||
if bytes.Compare(testOut, tplOut) != 0 {
|
||||
t.Logf("Template (rendered) '%s': '%s'", match, tplOut)
|
||||
errFilename := filepath.Base(fmt.Sprintf("%s.error", match))
|
||||
err := ioutil.WriteFile(errFilename, []byte(tplOut), 0600)
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
t.Logf("get a complete diff with command: 'diff -ya %s %s'", testFilename, errFilename)
|
||||
t.Errorf("Failed: test_out != tpl_out for %s", match)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionErrors(t *testing.T) {
|
||||
//debug = true
|
||||
|
||||
matches, err := filepath.Glob("./template_tests/*-execution.err")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for idx, match := range matches {
|
||||
t.Logf("[Errors %3d] Testing '%s'", idx+1, match)
|
||||
|
||||
testData, err := ioutil.ReadFile(match)
|
||||
tests := strings.Split(string(testData), "\n")
|
||||
|
||||
checkFilename := fmt.Sprintf("%s.out", match)
|
||||
checkData, err := ioutil.ReadFile(checkFilename)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on ReadFile('%s'): %s", checkFilename, err.Error())
|
||||
}
|
||||
checks := strings.Split(string(checkData), "\n")
|
||||
|
||||
if len(checks) != len(tests) {
|
||||
t.Fatal("Template lines != Checks lines")
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
if strings.TrimSpace(test) == "" {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(checks[idx]) == "" {
|
||||
t.Fatalf("[%s Line %d] Check is empty (must contain an regular expression).",
|
||||
match, idx+1)
|
||||
}
|
||||
|
||||
tpl, err := pongo2.FromString(test)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on FromString('%s'): %s", test, err.Error())
|
||||
}
|
||||
|
||||
tpl, err = pongo2.FromBytes([]byte(test))
|
||||
if err != nil {
|
||||
t.Fatalf("Error on FromBytes('%s'): %s", test, err.Error())
|
||||
}
|
||||
|
||||
_, err = tpl.ExecuteBytes(tplContext)
|
||||
if err == nil {
|
||||
t.Fatalf("[%s Line %d] Expected error for (got none): %s",
|
||||
match, idx+1, tests[idx])
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(fmt.Sprintf("^%s$", checks[idx]))
|
||||
if !re.MatchString(err.Error()) {
|
||||
t.Fatalf("[%s Line %d] Error for '%s' (err = '%s') does not match the (regexp-)check: %s",
|
||||
match, idx+1, test, err.Error(), checks[idx])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompilationErrors(t *testing.T) {
|
||||
//debug = true
|
||||
|
||||
matches, err := filepath.Glob("./template_tests/*-compilation.err")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for idx, match := range matches {
|
||||
t.Logf("[Errors %3d] Testing '%s'", idx+1, match)
|
||||
|
||||
testData, err := ioutil.ReadFile(match)
|
||||
tests := strings.Split(string(testData), "\n")
|
||||
|
||||
checkFilename := fmt.Sprintf("%s.out", match)
|
||||
checkData, err := ioutil.ReadFile(checkFilename)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on ReadFile('%s'): %s", checkFilename, err.Error())
|
||||
}
|
||||
checks := strings.Split(string(checkData), "\n")
|
||||
|
||||
if len(checks) != len(tests) {
|
||||
t.Fatal("Template lines != Checks lines")
|
||||
}
|
||||
|
||||
for idx, test := range tests {
|
||||
if strings.TrimSpace(test) == "" {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(checks[idx]) == "" {
|
||||
t.Fatalf("[%s Line %d] Check is empty (must contain an regular expression).",
|
||||
match, idx+1)
|
||||
}
|
||||
|
||||
_, err = pongo2.FromString(test)
|
||||
if err == nil {
|
||||
t.Fatalf("[%s | Line %d] Expected error for (got none): %s", match, idx+1, tests[idx])
|
||||
}
|
||||
re := regexp.MustCompile(fmt.Sprintf("^%s$", checks[idx]))
|
||||
if !re.MatchString(err.Error()) {
|
||||
t.Fatalf("[%s | Line %d] Error for '%s' (err = '%s') does not match the (regexp-)check: %s",
|
||||
match, idx+1, test, err.Error(), checks[idx])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseDirectory(t *testing.T) {
|
||||
mustStr := "Hello from template_tests/base_dir_test/"
|
||||
|
||||
fs := pongo2.MustNewLocalFileSystemLoader("")
|
||||
s := pongo2.NewSet("test set with base directory", fs)
|
||||
s.Globals["base_directory"] = "template_tests/base_dir_test/"
|
||||
if err := fs.SetBaseDir(s.Globals["base_directory"].(string)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
matches, err := filepath.Glob("./template_tests/base_dir_test/subdir/*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, match := range matches {
|
||||
match = strings.Replace(match, "template_tests/base_dir_test/", "", -1)
|
||||
|
||||
tpl, err := s.FromFile(match)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := tpl.Execute(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != mustStr {
|
||||
t.Errorf("%s: out ('%s') != mustStr ('%s')", match, out, mustStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCache(b *testing.B) {
|
||||
cacheSet := pongo2.NewSet("cache set", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
for i := 0; i < b.N; i++ {
|
||||
tpl, err := cacheSet.FromCache("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheDebugOn(b *testing.B) {
|
||||
cacheDebugSet := pongo2.NewSet("cache set", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
cacheDebugSet.Debug = true
|
||||
for i := 0; i < b.N; i++ {
|
||||
tpl, err := cacheDebugSet.FromFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkExecuteComplexWithSandboxActive(b *testing.B) {
|
||||
tpl, err := pongo2.FromFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompileAndExecuteComplexWithSandboxActive(b *testing.B) {
|
||||
buf, err := ioutil.ReadFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
preloadedTpl := string(buf)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tpl, err := pongo2.FromString(preloadedTpl)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParallelExecuteComplexWithSandboxActive(b *testing.B) {
|
||||
tpl, err := pongo2.FromFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkExecuteComplexWithoutSandbox(b *testing.B) {
|
||||
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
tpl, err := s.FromFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompileAndExecuteComplexWithoutSandbox(b *testing.B) {
|
||||
buf, err := ioutil.ReadFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
preloadedTpl := string(buf)
|
||||
|
||||
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
tpl, err := s.FromString(preloadedTpl)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
err = tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkParallelExecuteComplexWithoutSandbox(b *testing.B) {
|
||||
s := pongo2.NewSet("set without sandbox", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
tpl, err := s.FromFile("template_tests/complex.tpl")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
err := tpl.ExecuteWriterUnbuffered(tplContext, ioutil.Discard)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
103
vendor/github.com/flosch/pongo2/pongo2_test.go
generated
vendored
Normal file
103
vendor/github.com/flosch/pongo2/pongo2_test.go
generated
vendored
Normal file
|
@ -0,0 +1,103 @@
|
|||
package pongo2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
// Hook up gocheck into the "go test" runner.
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type TestSuite struct {
|
||||
tpl *pongo2.Template
|
||||
}
|
||||
|
||||
var (
|
||||
_ = Suite(&TestSuite{})
|
||||
testSuite2 = pongo2.NewSet("test suite 2", pongo2.MustNewLocalFileSystemLoader(""))
|
||||
)
|
||||
|
||||
func parseTemplate(s string, c pongo2.Context) string {
|
||||
t, err := testSuite2.FromString(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
out, err := t.Execute(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func parseTemplateFn(s string, c pongo2.Context) func() {
|
||||
return func() {
|
||||
parseTemplate(s, c)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TestSuite) TestMisc(c *C) {
|
||||
// Must
|
||||
// TODO: Add better error message (see issue #18)
|
||||
c.Check(
|
||||
func() { pongo2.Must(testSuite2.FromFile("template_tests/inheritance/base2.tpl")) },
|
||||
PanicMatches,
|
||||
`\[Error \(where: fromfile\) in .*template_tests/inheritance/doesnotexist.tpl | Line 1 Col 12 near 'doesnotexist.tpl'\] open .*template_tests/inheritance/doesnotexist.tpl: no such file or directory`,
|
||||
)
|
||||
|
||||
// Context
|
||||
c.Check(parseTemplateFn("", pongo2.Context{"'illegal": nil}), PanicMatches, ".*not a valid identifier.*")
|
||||
|
||||
// Registers
|
||||
c.Check(pongo2.RegisterFilter("escape", nil).Error(), Matches, ".*is already registered")
|
||||
c.Check(pongo2.RegisterTag("for", nil).Error(), Matches, ".*is already registered")
|
||||
|
||||
// ApplyFilter
|
||||
v, err := pongo2.ApplyFilter("title", pongo2.AsValue("this is a title"), nil)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
c.Check(v.String(), Equals, "This Is A Title")
|
||||
c.Check(func() {
|
||||
_, err := pongo2.ApplyFilter("doesnotexist", nil, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}, PanicMatches, `\[Error \(where: applyfilter\)\] Filter with name 'doesnotexist' not found.`)
|
||||
}
|
||||
|
||||
func (s *TestSuite) TestImplicitExecCtx(c *C) {
|
||||
tpl, err := pongo2.FromString("{{ ImplicitExec }}")
|
||||
if err != nil {
|
||||
c.Fatalf("Error in FromString: %v", err)
|
||||
}
|
||||
|
||||
val := "a stringy thing"
|
||||
|
||||
res, err := tpl.Execute(pongo2.Context{
|
||||
"Value": val,
|
||||
"ImplicitExec": func(ctx *pongo2.ExecutionContext) string {
|
||||
return ctx.Public["Value"].(string)
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.Fatalf("Error executing template: %v", err)
|
||||
}
|
||||
|
||||
c.Check(res, Equals, val)
|
||||
|
||||
// The implicit ctx should not be persisted from call-to-call
|
||||
res, err = tpl.Execute(pongo2.Context{
|
||||
"ImplicitExec": func() string {
|
||||
return val
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.Fatalf("Error executing template: %v", err)
|
||||
}
|
||||
|
||||
c.Check(res, Equals, val)
|
||||
}
|
135
vendor/github.com/flosch/pongo2/tags.go
generated
vendored
Normal file
135
vendor/github.com/flosch/pongo2/tags.go
generated
vendored
Normal file
|
@ -0,0 +1,135 @@
|
|||
package pongo2
|
||||
|
||||
/* Incomplete:
|
||||
-----------
|
||||
|
||||
verbatim (only the "name" argument is missing for verbatim)
|
||||
|
||||
Reconsideration:
|
||||
----------------
|
||||
|
||||
debug (reason: not sure what to output yet)
|
||||
regroup / Grouping on other properties (reason: maybe too python-specific; not sure how useful this would be in Go)
|
||||
|
||||
Following built-in tags wont be added:
|
||||
--------------------------------------
|
||||
|
||||
csrf_token (reason: web-framework specific)
|
||||
load (reason: python-specific)
|
||||
url (reason: web-framework specific)
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
type INodeTag interface {
|
||||
INode
|
||||
}
|
||||
|
||||
// This is the function signature of the tag's parser you will have
|
||||
// to implement in order to create a new tag.
|
||||
//
|
||||
// 'doc' is providing access to the whole document while 'arguments'
|
||||
// is providing access to the user's arguments to the tag:
|
||||
//
|
||||
// {% your_tag_name some "arguments" 123 %}
|
||||
//
|
||||
// start_token will be the *Token with the tag's name in it (here: your_tag_name).
|
||||
//
|
||||
// Please see the Parser documentation on how to use the parser.
|
||||
// See RegisterTag()'s documentation for more information about
|
||||
// writing a tag as well.
|
||||
type TagParser func(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error)
|
||||
|
||||
type tag struct {
|
||||
name string
|
||||
parser TagParser
|
||||
}
|
||||
|
||||
var tags map[string]*tag
|
||||
|
||||
func init() {
|
||||
tags = make(map[string]*tag)
|
||||
}
|
||||
|
||||
// Registers a new tag. You usually want to call this
|
||||
// function in the tag's init() function:
|
||||
// http://golang.org/doc/effective_go.html#init
|
||||
//
|
||||
// See http://www.florian-schlachter.de/post/pongo2/ for more about
|
||||
// writing filters and tags.
|
||||
func RegisterTag(name string, parserFn TagParser) error {
|
||||
_, existing := tags[name]
|
||||
if existing {
|
||||
return errors.Errorf("tag with name '%s' is already registered", name)
|
||||
}
|
||||
tags[name] = &tag{
|
||||
name: name,
|
||||
parser: parserFn,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Replaces an already registered tag with a new implementation. Use this
|
||||
// function with caution since it allows you to change existing tag behaviour.
|
||||
func ReplaceTag(name string, parserFn TagParser) error {
|
||||
_, existing := tags[name]
|
||||
if !existing {
|
||||
return errors.Errorf("tag with name '%s' does not exist (therefore cannot be overridden)", name)
|
||||
}
|
||||
tags[name] = &tag{
|
||||
name: name,
|
||||
parser: parserFn,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tag = "{%" IDENT ARGS "%}"
|
||||
func (p *Parser) parseTagElement() (INodeTag, *Error) {
|
||||
p.Consume() // consume "{%"
|
||||
tokenName := p.MatchType(TokenIdentifier)
|
||||
|
||||
// Check for identifier
|
||||
if tokenName == nil {
|
||||
return nil, p.Error("Tag name must be an identifier.", nil)
|
||||
}
|
||||
|
||||
// Check for the existing tag
|
||||
tag, exists := tags[tokenName.Val]
|
||||
if !exists {
|
||||
// Does not exists
|
||||
return nil, p.Error(fmt.Sprintf("Tag '%s' not found (or beginning tag not provided)", tokenName.Val), tokenName)
|
||||
}
|
||||
|
||||
// Check sandbox tag restriction
|
||||
if _, isBanned := p.template.set.bannedTags[tokenName.Val]; isBanned {
|
||||
return nil, p.Error(fmt.Sprintf("Usage of tag '%s' is not allowed (sandbox restriction active).", tokenName.Val), tokenName)
|
||||
}
|
||||
|
||||
var argsToken []*Token
|
||||
for p.Peek(TokenSymbol, "%}") == nil && p.Remaining() > 0 {
|
||||
// Add token to args
|
||||
argsToken = append(argsToken, p.Current())
|
||||
p.Consume() // next token
|
||||
}
|
||||
|
||||
// EOF?
|
||||
if p.Remaining() == 0 {
|
||||
return nil, p.Error("Unexpectedly reached EOF, no tag end found.", p.lastToken)
|
||||
}
|
||||
|
||||
p.Match(TokenSymbol, "%}")
|
||||
|
||||
argParser := newParser(p.name, argsToken, p.template)
|
||||
if len(argsToken) == 0 {
|
||||
// This is done to have nice EOF error messages
|
||||
argParser.lastToken = tokenName
|
||||
}
|
||||
|
||||
p.template.level++
|
||||
defer func() { p.template.level-- }()
|
||||
return tag.parser(p, tokenName, argParser)
|
||||
}
|
52
vendor/github.com/flosch/pongo2/tags_autoescape.go
generated
vendored
Normal file
52
vendor/github.com/flosch/pongo2/tags_autoescape.go
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
package pongo2
|
||||
|
||||
type tagAutoescapeNode struct {
|
||||
wrapper *NodeWrapper
|
||||
autoescape bool
|
||||
}
|
||||
|
||||
func (node *tagAutoescapeNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
old := ctx.Autoescape
|
||||
ctx.Autoescape = node.autoescape
|
||||
|
||||
err := node.wrapper.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx.Autoescape = old
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagAutoescapeParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
autoescapeNode := &tagAutoescapeNode{}
|
||||
|
||||
wrapper, _, err := doc.WrapUntilTag("endautoescape")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
autoescapeNode.wrapper = wrapper
|
||||
|
||||
modeToken := arguments.MatchType(TokenIdentifier)
|
||||
if modeToken == nil {
|
||||
return nil, arguments.Error("A mode is required for autoescape-tag.", nil)
|
||||
}
|
||||
if modeToken.Val == "on" {
|
||||
autoescapeNode.autoescape = true
|
||||
} else if modeToken.Val == "off" {
|
||||
autoescapeNode.autoescape = false
|
||||
} else {
|
||||
return nil, arguments.Error("Only 'on' or 'off' is valid as an autoescape-mode.", nil)
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed autoescape-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return autoescapeNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("autoescape", tagAutoescapeParser)
|
||||
}
|
129
vendor/github.com/flosch/pongo2/tags_block.go
generated
vendored
Normal file
129
vendor/github.com/flosch/pongo2/tags_block.go
generated
vendored
Normal file
|
@ -0,0 +1,129 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type tagBlockNode struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (node *tagBlockNode) getBlockWrappers(tpl *Template) []*NodeWrapper {
|
||||
nodeWrappers := make([]*NodeWrapper, 0)
|
||||
var t *NodeWrapper
|
||||
|
||||
for tpl != nil {
|
||||
t = tpl.blocks[node.name]
|
||||
if t != nil {
|
||||
nodeWrappers = append(nodeWrappers, t)
|
||||
}
|
||||
tpl = tpl.child
|
||||
}
|
||||
|
||||
return nodeWrappers
|
||||
}
|
||||
|
||||
func (node *tagBlockNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
tpl := ctx.template
|
||||
if tpl == nil {
|
||||
panic("internal error: tpl == nil")
|
||||
}
|
||||
|
||||
// Determine the block to execute
|
||||
blockWrappers := node.getBlockWrappers(tpl)
|
||||
lenBlockWrappers := len(blockWrappers)
|
||||
|
||||
if lenBlockWrappers == 0 {
|
||||
return ctx.Error("internal error: len(block_wrappers) == 0 in tagBlockNode.Execute()", nil)
|
||||
}
|
||||
|
||||
blockWrapper := blockWrappers[lenBlockWrappers-1]
|
||||
ctx.Private["block"] = tagBlockInformation{
|
||||
ctx: ctx,
|
||||
wrappers: blockWrappers[0 : lenBlockWrappers-1],
|
||||
}
|
||||
err := blockWrapper.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type tagBlockInformation struct {
|
||||
ctx *ExecutionContext
|
||||
wrappers []*NodeWrapper
|
||||
}
|
||||
|
||||
func (t tagBlockInformation) Super() string {
|
||||
lenWrappers := len(t.wrappers)
|
||||
|
||||
if lenWrappers == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
superCtx := NewChildExecutionContext(t.ctx)
|
||||
superCtx.Private["block"] = tagBlockInformation{
|
||||
ctx: t.ctx,
|
||||
wrappers: t.wrappers[0 : lenWrappers-1],
|
||||
}
|
||||
|
||||
blockWrapper := t.wrappers[lenWrappers-1]
|
||||
buf := bytes.NewBufferString("")
|
||||
err := blockWrapper.Execute(superCtx, &templateWriter{buf})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func tagBlockParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
if arguments.Count() == 0 {
|
||||
return nil, arguments.Error("Tag 'block' requires an identifier.", nil)
|
||||
}
|
||||
|
||||
nameToken := arguments.MatchType(TokenIdentifier)
|
||||
if nameToken == nil {
|
||||
return nil, arguments.Error("First argument for tag 'block' must be an identifier.", nil)
|
||||
}
|
||||
|
||||
if arguments.Remaining() != 0 {
|
||||
return nil, arguments.Error("Tag 'block' takes exactly 1 argument (an identifier).", nil)
|
||||
}
|
||||
|
||||
wrapper, endtagargs, err := doc.WrapUntilTag("endblock")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if endtagargs.Remaining() > 0 {
|
||||
endtagnameToken := endtagargs.MatchType(TokenIdentifier)
|
||||
if endtagnameToken != nil {
|
||||
if endtagnameToken.Val != nameToken.Val {
|
||||
return nil, endtagargs.Error(fmt.Sprintf("Name for 'endblock' must equal to 'block'-tag's name ('%s' != '%s').",
|
||||
nameToken.Val, endtagnameToken.Val), nil)
|
||||
}
|
||||
}
|
||||
|
||||
if endtagnameToken == nil || endtagargs.Remaining() > 0 {
|
||||
return nil, endtagargs.Error("Either no or only one argument (identifier) allowed for 'endblock'.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
tpl := doc.template
|
||||
if tpl == nil {
|
||||
panic("internal error: tpl == nil")
|
||||
}
|
||||
_, hasBlock := tpl.blocks[nameToken.Val]
|
||||
if !hasBlock {
|
||||
tpl.blocks[nameToken.Val] = wrapper
|
||||
} else {
|
||||
return nil, arguments.Error(fmt.Sprintf("Block named '%s' already defined", nameToken.Val), nil)
|
||||
}
|
||||
|
||||
return &tagBlockNode{name: nameToken.Val}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("block", tagBlockParser)
|
||||
}
|
27
vendor/github.com/flosch/pongo2/tags_comment.go
generated
vendored
Normal file
27
vendor/github.com/flosch/pongo2/tags_comment.go
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
package pongo2
|
||||
|
||||
type tagCommentNode struct{}
|
||||
|
||||
func (node *tagCommentNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagCommentParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
commentNode := &tagCommentNode{}
|
||||
|
||||
// TODO: Process the endtag's arguments (see django 'comment'-tag documentation)
|
||||
err := doc.SkipUntilTag("endcomment")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if arguments.Count() != 0 {
|
||||
return nil, arguments.Error("Tag 'comment' does not take any argument.", nil)
|
||||
}
|
||||
|
||||
return commentNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("comment", tagCommentParser)
|
||||
}
|
106
vendor/github.com/flosch/pongo2/tags_cycle.go
generated
vendored
Normal file
106
vendor/github.com/flosch/pongo2/tags_cycle.go
generated
vendored
Normal file
|
@ -0,0 +1,106 @@
|
|||
package pongo2
|
||||
|
||||
type tagCycleValue struct {
|
||||
node *tagCycleNode
|
||||
value *Value
|
||||
}
|
||||
|
||||
type tagCycleNode struct {
|
||||
position *Token
|
||||
args []IEvaluator
|
||||
idx int
|
||||
asName string
|
||||
silent bool
|
||||
}
|
||||
|
||||
func (cv *tagCycleValue) String() string {
|
||||
return cv.value.String()
|
||||
}
|
||||
|
||||
func (node *tagCycleNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
item := node.args[node.idx%len(node.args)]
|
||||
node.idx++
|
||||
|
||||
val, err := item.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if t, ok := val.Interface().(*tagCycleValue); ok {
|
||||
// {% cycle "test1" "test2"
|
||||
// {% cycle cycleitem %}
|
||||
|
||||
// Update the cycle value with next value
|
||||
item := t.node.args[t.node.idx%len(t.node.args)]
|
||||
t.node.idx++
|
||||
|
||||
val, err := item.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.value = val
|
||||
|
||||
if !t.node.silent {
|
||||
writer.WriteString(val.String())
|
||||
}
|
||||
} else {
|
||||
// Regular call
|
||||
|
||||
cycleValue := &tagCycleValue{
|
||||
node: node,
|
||||
value: val,
|
||||
}
|
||||
|
||||
if node.asName != "" {
|
||||
ctx.Private[node.asName] = cycleValue
|
||||
}
|
||||
if !node.silent {
|
||||
writer.WriteString(val.String())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HINT: We're not supporting the old comma-separated list of expressions argument-style
|
||||
func tagCycleParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
cycleNode := &tagCycleNode{
|
||||
position: start,
|
||||
}
|
||||
|
||||
for arguments.Remaining() > 0 {
|
||||
node, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cycleNode.args = append(cycleNode.args, node)
|
||||
|
||||
if arguments.MatchOne(TokenKeyword, "as") != nil {
|
||||
// as
|
||||
|
||||
nameToken := arguments.MatchType(TokenIdentifier)
|
||||
if nameToken == nil {
|
||||
return nil, arguments.Error("Name (identifier) expected after 'as'.", nil)
|
||||
}
|
||||
cycleNode.asName = nameToken.Val
|
||||
|
||||
if arguments.MatchOne(TokenIdentifier, "silent") != nil {
|
||||
cycleNode.silent = true
|
||||
}
|
||||
|
||||
// Now we're finished
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed cycle-tag.", nil)
|
||||
}
|
||||
|
||||
return cycleNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("cycle", tagCycleParser)
|
||||
}
|
52
vendor/github.com/flosch/pongo2/tags_extends.go
generated
vendored
Normal file
52
vendor/github.com/flosch/pongo2/tags_extends.go
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
package pongo2
|
||||
|
||||
type tagExtendsNode struct {
|
||||
filename string
|
||||
}
|
||||
|
||||
func (node *tagExtendsNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagExtendsParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
extendsNode := &tagExtendsNode{}
|
||||
|
||||
if doc.template.level > 1 {
|
||||
return nil, arguments.Error("The 'extends' tag can only defined on root level.", start)
|
||||
}
|
||||
|
||||
if doc.template.parent != nil {
|
||||
// Already one parent
|
||||
return nil, arguments.Error("This template has already one parent.", start)
|
||||
}
|
||||
|
||||
if filenameToken := arguments.MatchType(TokenString); filenameToken != nil {
|
||||
// prepared, static template
|
||||
|
||||
// Get parent's filename
|
||||
parentFilename := doc.template.set.resolveFilename(doc.template, filenameToken.Val)
|
||||
|
||||
// Parse the parent
|
||||
parentTemplate, err := doc.template.set.FromFile(parentFilename)
|
||||
if err != nil {
|
||||
return nil, err.(*Error)
|
||||
}
|
||||
|
||||
// Keep track of things
|
||||
parentTemplate.child = doc.template
|
||||
doc.template.parent = parentTemplate
|
||||
extendsNode.filename = parentFilename
|
||||
} else {
|
||||
return nil, arguments.Error("Tag 'extends' requires a template filename as string.", nil)
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Tag 'extends' does only take 1 argument.", nil)
|
||||
}
|
||||
|
||||
return extendsNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("extends", tagExtendsParser)
|
||||
}
|
95
vendor/github.com/flosch/pongo2/tags_filter.go
generated
vendored
Normal file
95
vendor/github.com/flosch/pongo2/tags_filter.go
generated
vendored
Normal file
|
@ -0,0 +1,95 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type nodeFilterCall struct {
|
||||
name string
|
||||
paramExpr IEvaluator
|
||||
}
|
||||
|
||||
type tagFilterNode struct {
|
||||
position *Token
|
||||
bodyWrapper *NodeWrapper
|
||||
filterChain []*nodeFilterCall
|
||||
}
|
||||
|
||||
func (node *tagFilterNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
temp := bytes.NewBuffer(make([]byte, 0, 1024)) // 1 KiB size
|
||||
|
||||
err := node.bodyWrapper.Execute(ctx, temp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value := AsValue(temp.String())
|
||||
|
||||
for _, call := range node.filterChain {
|
||||
var param *Value
|
||||
if call.paramExpr != nil {
|
||||
param, err = call.paramExpr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
param = AsValue(nil)
|
||||
}
|
||||
value, err = ApplyFilter(call.name, value, param)
|
||||
if err != nil {
|
||||
return ctx.Error(err.Error(), node.position)
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteString(value.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagFilterParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
filterNode := &tagFilterNode{
|
||||
position: start,
|
||||
}
|
||||
|
||||
wrapper, _, err := doc.WrapUntilTag("endfilter")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filterNode.bodyWrapper = wrapper
|
||||
|
||||
for arguments.Remaining() > 0 {
|
||||
filterCall := &nodeFilterCall{}
|
||||
|
||||
nameToken := arguments.MatchType(TokenIdentifier)
|
||||
if nameToken == nil {
|
||||
return nil, arguments.Error("Expected a filter name (identifier).", nil)
|
||||
}
|
||||
filterCall.name = nameToken.Val
|
||||
|
||||
if arguments.MatchOne(TokenSymbol, ":") != nil {
|
||||
// Filter parameter
|
||||
// NOTICE: we can't use ParseExpression() here, because it would parse the next filter "|..." as well in the argument list
|
||||
expr, err := arguments.parseVariableOrLiteral()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filterCall.paramExpr = expr
|
||||
}
|
||||
|
||||
filterNode.filterChain = append(filterNode.filterChain, filterCall)
|
||||
|
||||
if arguments.MatchOne(TokenSymbol, "|") == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed filter-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return filterNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("filter", tagFilterParser)
|
||||
}
|
49
vendor/github.com/flosch/pongo2/tags_firstof.go
generated
vendored
Normal file
49
vendor/github.com/flosch/pongo2/tags_firstof.go
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
package pongo2
|
||||
|
||||
type tagFirstofNode struct {
|
||||
position *Token
|
||||
args []IEvaluator
|
||||
}
|
||||
|
||||
func (node *tagFirstofNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
for _, arg := range node.args {
|
||||
val, err := arg.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if val.IsTrue() {
|
||||
if ctx.Autoescape && !arg.FilterApplied("safe") {
|
||||
val, err = ApplyFilter("escape", val, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteString(val.String())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagFirstofParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
firstofNode := &tagFirstofNode{
|
||||
position: start,
|
||||
}
|
||||
|
||||
for arguments.Remaining() > 0 {
|
||||
node, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
firstofNode.args = append(firstofNode.args, node)
|
||||
}
|
||||
|
||||
return firstofNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("firstof", tagFirstofParser)
|
||||
}
|
159
vendor/github.com/flosch/pongo2/tags_for.go
generated
vendored
Normal file
159
vendor/github.com/flosch/pongo2/tags_for.go
generated
vendored
Normal file
|
@ -0,0 +1,159 @@
|
|||
package pongo2
|
||||
|
||||
type tagForNode struct {
|
||||
key string
|
||||
value string // only for maps: for key, value in map
|
||||
objectEvaluator IEvaluator
|
||||
reversed bool
|
||||
sorted bool
|
||||
|
||||
bodyWrapper *NodeWrapper
|
||||
emptyWrapper *NodeWrapper
|
||||
}
|
||||
|
||||
type tagForLoopInformation struct {
|
||||
Counter int
|
||||
Counter0 int
|
||||
Revcounter int
|
||||
Revcounter0 int
|
||||
First bool
|
||||
Last bool
|
||||
Parentloop *tagForLoopInformation
|
||||
}
|
||||
|
||||
func (node *tagForNode) Execute(ctx *ExecutionContext, writer TemplateWriter) (forError *Error) {
|
||||
// Backup forloop (as parentloop in public context), key-name and value-name
|
||||
forCtx := NewChildExecutionContext(ctx)
|
||||
parentloop := forCtx.Private["forloop"]
|
||||
|
||||
// Create loop struct
|
||||
loopInfo := &tagForLoopInformation{
|
||||
First: true,
|
||||
}
|
||||
|
||||
// Is it a loop in a loop?
|
||||
if parentloop != nil {
|
||||
loopInfo.Parentloop = parentloop.(*tagForLoopInformation)
|
||||
}
|
||||
|
||||
// Register loopInfo in public context
|
||||
forCtx.Private["forloop"] = loopInfo
|
||||
|
||||
obj, err := node.objectEvaluator.Evaluate(forCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
obj.IterateOrder(func(idx, count int, key, value *Value) bool {
|
||||
// There's something to iterate over (correct type and at least 1 item)
|
||||
|
||||
// Update loop infos and public context
|
||||
forCtx.Private[node.key] = key
|
||||
if value != nil {
|
||||
forCtx.Private[node.value] = value
|
||||
}
|
||||
loopInfo.Counter = idx + 1
|
||||
loopInfo.Counter0 = idx
|
||||
if idx == 1 {
|
||||
loopInfo.First = false
|
||||
}
|
||||
if idx+1 == count {
|
||||
loopInfo.Last = true
|
||||
}
|
||||
loopInfo.Revcounter = count - idx // TODO: Not sure about this, have to look it up
|
||||
loopInfo.Revcounter0 = count - (idx + 1) // TODO: Not sure about this, have to look it up
|
||||
|
||||
// Render elements with updated context
|
||||
err := node.bodyWrapper.Execute(forCtx, writer)
|
||||
if err != nil {
|
||||
forError = err
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, func() {
|
||||
// Nothing to iterate over (maybe wrong type or no items)
|
||||
if node.emptyWrapper != nil {
|
||||
err := node.emptyWrapper.Execute(forCtx, writer)
|
||||
if err != nil {
|
||||
forError = err
|
||||
}
|
||||
}
|
||||
}, node.reversed, node.sorted)
|
||||
|
||||
return forError
|
||||
}
|
||||
|
||||
func tagForParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
forNode := &tagForNode{}
|
||||
|
||||
// Arguments parsing
|
||||
var valueToken *Token
|
||||
keyToken := arguments.MatchType(TokenIdentifier)
|
||||
if keyToken == nil {
|
||||
return nil, arguments.Error("Expected an key identifier as first argument for 'for'-tag", nil)
|
||||
}
|
||||
|
||||
if arguments.Match(TokenSymbol, ",") != nil {
|
||||
// Value name is provided
|
||||
valueToken = arguments.MatchType(TokenIdentifier)
|
||||
if valueToken == nil {
|
||||
return nil, arguments.Error("Value name must be an identifier.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
if arguments.Match(TokenKeyword, "in") == nil {
|
||||
return nil, arguments.Error("Expected keyword 'in'.", nil)
|
||||
}
|
||||
|
||||
objectEvaluator, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
forNode.objectEvaluator = objectEvaluator
|
||||
forNode.key = keyToken.Val
|
||||
if valueToken != nil {
|
||||
forNode.value = valueToken.Val
|
||||
}
|
||||
|
||||
if arguments.MatchOne(TokenIdentifier, "reversed") != nil {
|
||||
forNode.reversed = true
|
||||
}
|
||||
|
||||
if arguments.MatchOne(TokenIdentifier, "sorted") != nil {
|
||||
forNode.sorted = true
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed for-loop arguments.", nil)
|
||||
}
|
||||
|
||||
// Body wrapping
|
||||
wrapper, endargs, err := doc.WrapUntilTag("empty", "endfor")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
forNode.bodyWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
|
||||
if wrapper.Endtag == "empty" {
|
||||
// if there's an else in the if-statement, we need the else-Block as well
|
||||
wrapper, endargs, err = doc.WrapUntilTag("endfor")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
forNode.emptyWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return forNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("for", tagForParser)
|
||||
}
|
76
vendor/github.com/flosch/pongo2/tags_if.go
generated
vendored
Normal file
76
vendor/github.com/flosch/pongo2/tags_if.go
generated
vendored
Normal file
|
@ -0,0 +1,76 @@
|
|||
package pongo2
|
||||
|
||||
type tagIfNode struct {
|
||||
conditions []IEvaluator
|
||||
wrappers []*NodeWrapper
|
||||
}
|
||||
|
||||
func (node *tagIfNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
for i, condition := range node.conditions {
|
||||
result, err := condition.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.IsTrue() {
|
||||
return node.wrappers[i].Execute(ctx, writer)
|
||||
}
|
||||
// Last condition?
|
||||
if len(node.conditions) == i+1 && len(node.wrappers) > i+1 {
|
||||
return node.wrappers[i+1].Execute(ctx, writer)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagIfParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
ifNode := &tagIfNode{}
|
||||
|
||||
// Parse first and main IF condition
|
||||
condition, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifNode.conditions = append(ifNode.conditions, condition)
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("If-condition is malformed.", nil)
|
||||
}
|
||||
|
||||
// Check the rest
|
||||
for {
|
||||
wrapper, tagArgs, err := doc.WrapUntilTag("elif", "else", "endif")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifNode.wrappers = append(ifNode.wrappers, wrapper)
|
||||
|
||||
if wrapper.Endtag == "elif" {
|
||||
// elif can take a condition
|
||||
condition, err = tagArgs.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifNode.conditions = append(ifNode.conditions, condition)
|
||||
|
||||
if tagArgs.Remaining() > 0 {
|
||||
return nil, tagArgs.Error("Elif-condition is malformed.", nil)
|
||||
}
|
||||
} else {
|
||||
if tagArgs.Count() > 0 {
|
||||
// else/endif can't take any conditions
|
||||
return nil, tagArgs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
if wrapper.Endtag == "endif" {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ifNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("if", tagIfParser)
|
||||
}
|
116
vendor/github.com/flosch/pongo2/tags_ifchanged.go
generated
vendored
Normal file
116
vendor/github.com/flosch/pongo2/tags_ifchanged.go
generated
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type tagIfchangedNode struct {
|
||||
watchedExpr []IEvaluator
|
||||
lastValues []*Value
|
||||
lastContent []byte
|
||||
thenWrapper *NodeWrapper
|
||||
elseWrapper *NodeWrapper
|
||||
}
|
||||
|
||||
func (node *tagIfchangedNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
if len(node.watchedExpr) == 0 {
|
||||
// Check against own rendered body
|
||||
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 1024)) // 1 KiB
|
||||
err := node.thenWrapper.Execute(ctx, buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bufBytes := buf.Bytes()
|
||||
if !bytes.Equal(node.lastContent, bufBytes) {
|
||||
// Rendered content changed, output it
|
||||
writer.Write(bufBytes)
|
||||
node.lastContent = bufBytes
|
||||
}
|
||||
} else {
|
||||
nowValues := make([]*Value, 0, len(node.watchedExpr))
|
||||
for _, expr := range node.watchedExpr {
|
||||
val, err := expr.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nowValues = append(nowValues, val)
|
||||
}
|
||||
|
||||
// Compare old to new values now
|
||||
changed := len(node.lastValues) == 0
|
||||
|
||||
for idx, oldVal := range node.lastValues {
|
||||
if !oldVal.EqualValueTo(nowValues[idx]) {
|
||||
changed = true
|
||||
break // we can stop here because ONE value changed
|
||||
}
|
||||
}
|
||||
|
||||
node.lastValues = nowValues
|
||||
|
||||
if changed {
|
||||
// Render thenWrapper
|
||||
err := node.thenWrapper.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Render elseWrapper
|
||||
err := node.elseWrapper.Execute(ctx, writer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagIfchangedParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
ifchangedNode := &tagIfchangedNode{}
|
||||
|
||||
for arguments.Remaining() > 0 {
|
||||
// Parse condition
|
||||
expr, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifchangedNode.watchedExpr = append(ifchangedNode.watchedExpr, expr)
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Ifchanged-arguments are malformed.", nil)
|
||||
}
|
||||
|
||||
// Wrap then/else-blocks
|
||||
wrapper, endargs, err := doc.WrapUntilTag("else", "endifchanged")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifchangedNode.thenWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
|
||||
if wrapper.Endtag == "else" {
|
||||
// if there's an else in the if-statement, we need the else-Block as well
|
||||
wrapper, endargs, err = doc.WrapUntilTag("endifchanged")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifchangedNode.elseWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return ifchangedNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("ifchanged", tagIfchangedParser)
|
||||
}
|
78
vendor/github.com/flosch/pongo2/tags_ifequal.go
generated
vendored
Normal file
78
vendor/github.com/flosch/pongo2/tags_ifequal.go
generated
vendored
Normal file
|
@ -0,0 +1,78 @@
|
|||
package pongo2
|
||||
|
||||
type tagIfEqualNode struct {
|
||||
var1, var2 IEvaluator
|
||||
thenWrapper *NodeWrapper
|
||||
elseWrapper *NodeWrapper
|
||||
}
|
||||
|
||||
func (node *tagIfEqualNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
r1, err := node.var1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r2, err := node.var2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := r1.EqualValueTo(r2)
|
||||
|
||||
if result {
|
||||
return node.thenWrapper.Execute(ctx, writer)
|
||||
}
|
||||
if node.elseWrapper != nil {
|
||||
return node.elseWrapper.Execute(ctx, writer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagIfEqualParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
ifequalNode := &tagIfEqualNode{}
|
||||
|
||||
// Parse two expressions
|
||||
var1, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var2, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifequalNode.var1 = var1
|
||||
ifequalNode.var2 = var2
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("ifequal only takes 2 arguments.", nil)
|
||||
}
|
||||
|
||||
// Wrap then/else-blocks
|
||||
wrapper, endargs, err := doc.WrapUntilTag("else", "endifequal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifequalNode.thenWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
|
||||
if wrapper.Endtag == "else" {
|
||||
// if there's an else in the if-statement, we need the else-Block as well
|
||||
wrapper, endargs, err = doc.WrapUntilTag("endifequal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifequalNode.elseWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return ifequalNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("ifequal", tagIfEqualParser)
|
||||
}
|
78
vendor/github.com/flosch/pongo2/tags_ifnotequal.go
generated
vendored
Normal file
78
vendor/github.com/flosch/pongo2/tags_ifnotequal.go
generated
vendored
Normal file
|
@ -0,0 +1,78 @@
|
|||
package pongo2
|
||||
|
||||
type tagIfNotEqualNode struct {
|
||||
var1, var2 IEvaluator
|
||||
thenWrapper *NodeWrapper
|
||||
elseWrapper *NodeWrapper
|
||||
}
|
||||
|
||||
func (node *tagIfNotEqualNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
r1, err := node.var1.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r2, err := node.var2.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := !r1.EqualValueTo(r2)
|
||||
|
||||
if result {
|
||||
return node.thenWrapper.Execute(ctx, writer)
|
||||
}
|
||||
if node.elseWrapper != nil {
|
||||
return node.elseWrapper.Execute(ctx, writer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagIfNotEqualParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
ifnotequalNode := &tagIfNotEqualNode{}
|
||||
|
||||
// Parse two expressions
|
||||
var1, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var2, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifnotequalNode.var1 = var1
|
||||
ifnotequalNode.var2 = var2
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("ifequal only takes 2 arguments.", nil)
|
||||
}
|
||||
|
||||
// Wrap then/else-blocks
|
||||
wrapper, endargs, err := doc.WrapUntilTag("else", "endifnotequal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifnotequalNode.thenWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
|
||||
if wrapper.Endtag == "else" {
|
||||
// if there's an else in the if-statement, we need the else-Block as well
|
||||
wrapper, endargs, err = doc.WrapUntilTag("endifnotequal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ifnotequalNode.elseWrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return ifnotequalNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("ifnotequal", tagIfNotEqualParser)
|
||||
}
|
84
vendor/github.com/flosch/pongo2/tags_import.go
generated
vendored
Normal file
84
vendor/github.com/flosch/pongo2/tags_import.go
generated
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type tagImportNode struct {
|
||||
position *Token
|
||||
filename string
|
||||
macros map[string]*tagMacroNode // alias/name -> macro instance
|
||||
}
|
||||
|
||||
func (node *tagImportNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
for name, macro := range node.macros {
|
||||
func(name string, macro *tagMacroNode) {
|
||||
ctx.Private[name] = func(args ...*Value) *Value {
|
||||
return macro.call(ctx, args...)
|
||||
}
|
||||
}(name, macro)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagImportParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
importNode := &tagImportNode{
|
||||
position: start,
|
||||
macros: make(map[string]*tagMacroNode),
|
||||
}
|
||||
|
||||
filenameToken := arguments.MatchType(TokenString)
|
||||
if filenameToken == nil {
|
||||
return nil, arguments.Error("Import-tag needs a filename as string.", nil)
|
||||
}
|
||||
|
||||
importNode.filename = doc.template.set.resolveFilename(doc.template, filenameToken.Val)
|
||||
|
||||
if arguments.Remaining() == 0 {
|
||||
return nil, arguments.Error("You must at least specify one macro to import.", nil)
|
||||
}
|
||||
|
||||
// Compile the given template
|
||||
tpl, err := doc.template.set.FromFile(importNode.filename)
|
||||
if err != nil {
|
||||
return nil, err.(*Error).updateFromTokenIfNeeded(doc.template, start)
|
||||
}
|
||||
|
||||
for arguments.Remaining() > 0 {
|
||||
macroNameToken := arguments.MatchType(TokenIdentifier)
|
||||
if macroNameToken == nil {
|
||||
return nil, arguments.Error("Expected macro name (identifier).", nil)
|
||||
}
|
||||
|
||||
asName := macroNameToken.Val
|
||||
if arguments.Match(TokenKeyword, "as") != nil {
|
||||
aliasToken := arguments.MatchType(TokenIdentifier)
|
||||
if aliasToken == nil {
|
||||
return nil, arguments.Error("Expected macro alias name (identifier).", nil)
|
||||
}
|
||||
asName = aliasToken.Val
|
||||
}
|
||||
|
||||
macroInstance, has := tpl.exportedMacros[macroNameToken.Val]
|
||||
if !has {
|
||||
return nil, arguments.Error(fmt.Sprintf("Macro '%s' not found (or not exported) in '%s'.", macroNameToken.Val,
|
||||
importNode.filename), macroNameToken)
|
||||
}
|
||||
|
||||
importNode.macros[asName] = macroInstance
|
||||
|
||||
if arguments.Remaining() == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
if arguments.Match(TokenSymbol, ",") == nil {
|
||||
return nil, arguments.Error("Expected ','.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
return importNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("import", tagImportParser)
|
||||
}
|
146
vendor/github.com/flosch/pongo2/tags_include.go
generated
vendored
Normal file
146
vendor/github.com/flosch/pongo2/tags_include.go
generated
vendored
Normal file
|
@ -0,0 +1,146 @@
|
|||
package pongo2
|
||||
|
||||
type tagIncludeNode struct {
|
||||
tpl *Template
|
||||
filenameEvaluator IEvaluator
|
||||
lazy bool
|
||||
only bool
|
||||
filename string
|
||||
withPairs map[string]IEvaluator
|
||||
ifExists bool
|
||||
}
|
||||
|
||||
func (node *tagIncludeNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
// Building the context for the template
|
||||
includeCtx := make(Context)
|
||||
|
||||
// Fill the context with all data from the parent
|
||||
if !node.only {
|
||||
includeCtx.Update(ctx.Public)
|
||||
includeCtx.Update(ctx.Private)
|
||||
}
|
||||
|
||||
// Put all custom with-pairs into the context
|
||||
for key, value := range node.withPairs {
|
||||
val, err := value.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
includeCtx[key] = val
|
||||
}
|
||||
|
||||
// Execute the template
|
||||
if node.lazy {
|
||||
// Evaluate the filename
|
||||
filename, err := node.filenameEvaluator.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if filename.String() == "" {
|
||||
return ctx.Error("Filename for 'include'-tag evaluated to an empty string.", nil)
|
||||
}
|
||||
|
||||
// Get include-filename
|
||||
includedFilename := ctx.template.set.resolveFilename(ctx.template, filename.String())
|
||||
|
||||
includedTpl, err2 := ctx.template.set.FromFile(includedFilename)
|
||||
if err2 != nil {
|
||||
// if this is ReadFile error, and "if_exists" flag is enabled
|
||||
if node.ifExists && err2.(*Error).Sender == "fromfile" {
|
||||
return nil
|
||||
}
|
||||
return err2.(*Error)
|
||||
}
|
||||
err2 = includedTpl.ExecuteWriter(includeCtx, writer)
|
||||
if err2 != nil {
|
||||
return err2.(*Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Template is already parsed with static filename
|
||||
err := node.tpl.ExecuteWriter(includeCtx, writer)
|
||||
if err != nil {
|
||||
return err.(*Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type tagIncludeEmptyNode struct{}
|
||||
|
||||
func (node *tagIncludeEmptyNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagIncludeParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
includeNode := &tagIncludeNode{
|
||||
withPairs: make(map[string]IEvaluator),
|
||||
}
|
||||
|
||||
if filenameToken := arguments.MatchType(TokenString); filenameToken != nil {
|
||||
// prepared, static template
|
||||
|
||||
// "if_exists" flag
|
||||
ifExists := arguments.Match(TokenIdentifier, "if_exists") != nil
|
||||
|
||||
// Get include-filename
|
||||
includedFilename := doc.template.set.resolveFilename(doc.template, filenameToken.Val)
|
||||
|
||||
// Parse the parent
|
||||
includeNode.filename = includedFilename
|
||||
includedTpl, err := doc.template.set.FromFile(includedFilename)
|
||||
if err != nil {
|
||||
// if this is ReadFile error, and "if_exists" token presents we should create and empty node
|
||||
if err.(*Error).Sender == "fromfile" && ifExists {
|
||||
return &tagIncludeEmptyNode{}, nil
|
||||
}
|
||||
return nil, err.(*Error).updateFromTokenIfNeeded(doc.template, filenameToken)
|
||||
}
|
||||
includeNode.tpl = includedTpl
|
||||
} else {
|
||||
// No String, then the user wants to use lazy-evaluation (slower, but possible)
|
||||
filenameEvaluator, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err.updateFromTokenIfNeeded(doc.template, filenameToken)
|
||||
}
|
||||
includeNode.filenameEvaluator = filenameEvaluator
|
||||
includeNode.lazy = true
|
||||
includeNode.ifExists = arguments.Match(TokenIdentifier, "if_exists") != nil // "if_exists" flag
|
||||
}
|
||||
|
||||
// After having parsed the filename we're gonna parse the with+only options
|
||||
if arguments.Match(TokenIdentifier, "with") != nil {
|
||||
for arguments.Remaining() > 0 {
|
||||
// We have at least one key=expr pair (because of starting "with")
|
||||
keyToken := arguments.MatchType(TokenIdentifier)
|
||||
if keyToken == nil {
|
||||
return nil, arguments.Error("Expected an identifier", nil)
|
||||
}
|
||||
if arguments.Match(TokenSymbol, "=") == nil {
|
||||
return nil, arguments.Error("Expected '='.", nil)
|
||||
}
|
||||
valueExpr, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err.updateFromTokenIfNeeded(doc.template, keyToken)
|
||||
}
|
||||
|
||||
includeNode.withPairs[keyToken.Val] = valueExpr
|
||||
|
||||
// Only?
|
||||
if arguments.Match(TokenIdentifier, "only") != nil {
|
||||
includeNode.only = true
|
||||
break // stop parsing arguments because it's the last option
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed 'include'-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return includeNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("include", tagIncludeParser)
|
||||
}
|
133
vendor/github.com/flosch/pongo2/tags_lorem.go
generated
vendored
Normal file
133
vendor/github.com/flosch/pongo2/tags_lorem.go
generated
vendored
Normal file
|
@ -0,0 +1,133 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
tagLoremParagraphs = strings.Split(tagLoremText, "\n")
|
||||
tagLoremWords = strings.Fields(tagLoremText)
|
||||
)
|
||||
|
||||
type tagLoremNode struct {
|
||||
position *Token
|
||||
count int // number of paragraphs
|
||||
method string // w = words, p = HTML paragraphs, b = plain-text (default is b)
|
||||
random bool // does not use the default paragraph "Lorem ipsum dolor sit amet, ..."
|
||||
}
|
||||
|
||||
func (node *tagLoremNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
switch node.method {
|
||||
case "b":
|
||||
if node.random {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString("\n")
|
||||
}
|
||||
par := tagLoremParagraphs[rand.Intn(len(tagLoremParagraphs))]
|
||||
writer.WriteString(par)
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString("\n")
|
||||
}
|
||||
par := tagLoremParagraphs[i%len(tagLoremParagraphs)]
|
||||
writer.WriteString(par)
|
||||
}
|
||||
}
|
||||
case "w":
|
||||
if node.random {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString(" ")
|
||||
}
|
||||
word := tagLoremWords[rand.Intn(len(tagLoremWords))]
|
||||
writer.WriteString(word)
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString(" ")
|
||||
}
|
||||
word := tagLoremWords[i%len(tagLoremWords)]
|
||||
writer.WriteString(word)
|
||||
}
|
||||
}
|
||||
case "p":
|
||||
if node.random {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString("\n")
|
||||
}
|
||||
writer.WriteString("<p>")
|
||||
par := tagLoremParagraphs[rand.Intn(len(tagLoremParagraphs))]
|
||||
writer.WriteString(par)
|
||||
writer.WriteString("</p>")
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < node.count; i++ {
|
||||
if i > 0 {
|
||||
writer.WriteString("\n")
|
||||
}
|
||||
writer.WriteString("<p>")
|
||||
par := tagLoremParagraphs[i%len(tagLoremParagraphs)]
|
||||
writer.WriteString(par)
|
||||
writer.WriteString("</p>")
|
||||
|
||||
}
|
||||
}
|
||||
default:
|
||||
return ctx.OrigError(errors.Errorf("unsupported method: %s", node.method), nil)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagLoremParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
loremNode := &tagLoremNode{
|
||||
position: start,
|
||||
count: 1,
|
||||
method: "b",
|
||||
}
|
||||
|
||||
if countToken := arguments.MatchType(TokenNumber); countToken != nil {
|
||||
loremNode.count = AsValue(countToken.Val).Integer()
|
||||
}
|
||||
|
||||
if methodToken := arguments.MatchType(TokenIdentifier); methodToken != nil {
|
||||
if methodToken.Val != "w" && methodToken.Val != "p" && methodToken.Val != "b" {
|
||||
return nil, arguments.Error("lorem-method must be either 'w', 'p' or 'b'.", nil)
|
||||
}
|
||||
|
||||
loremNode.method = methodToken.Val
|
||||
}
|
||||
|
||||
if arguments.MatchOne(TokenIdentifier, "random") != nil {
|
||||
loremNode.random = true
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed lorem-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return loremNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
||||
RegisterTag("lorem", tagLoremParser)
|
||||
}
|
||||
|
||||
const tagLoremText = `Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
|
||||
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
|
||||
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
|
||||
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.
|
||||
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.
|
||||
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.`
|
149
vendor/github.com/flosch/pongo2/tags_macro.go
generated
vendored
Normal file
149
vendor/github.com/flosch/pongo2/tags_macro.go
generated
vendored
Normal file
|
@ -0,0 +1,149 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type tagMacroNode struct {
|
||||
position *Token
|
||||
name string
|
||||
argsOrder []string
|
||||
args map[string]IEvaluator
|
||||
exported bool
|
||||
|
||||
wrapper *NodeWrapper
|
||||
}
|
||||
|
||||
func (node *tagMacroNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
ctx.Private[node.name] = func(args ...*Value) *Value {
|
||||
return node.call(ctx, args...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (node *tagMacroNode) call(ctx *ExecutionContext, args ...*Value) *Value {
|
||||
argsCtx := make(Context)
|
||||
|
||||
for k, v := range node.args {
|
||||
if v == nil {
|
||||
// User did not provided a default value
|
||||
argsCtx[k] = nil
|
||||
} else {
|
||||
// Evaluate the default value
|
||||
valueExpr, err := v.Evaluate(ctx)
|
||||
if err != nil {
|
||||
ctx.Logf(err.Error())
|
||||
return AsSafeValue(err.Error())
|
||||
}
|
||||
|
||||
argsCtx[k] = valueExpr
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) > len(node.argsOrder) {
|
||||
// Too many arguments, we're ignoring them and just logging into debug mode.
|
||||
err := ctx.Error(fmt.Sprintf("Macro '%s' called with too many arguments (%d instead of %d).",
|
||||
node.name, len(args), len(node.argsOrder)), nil).updateFromTokenIfNeeded(ctx.template, node.position)
|
||||
|
||||
ctx.Logf(err.Error()) // TODO: This is a workaround, because the error is not returned yet to the Execution()-methods
|
||||
return AsSafeValue(err.Error())
|
||||
}
|
||||
|
||||
// Make a context for the macro execution
|
||||
macroCtx := NewChildExecutionContext(ctx)
|
||||
|
||||
// Register all arguments in the private context
|
||||
macroCtx.Private.Update(argsCtx)
|
||||
|
||||
for idx, argValue := range args {
|
||||
macroCtx.Private[node.argsOrder[idx]] = argValue.Interface()
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
err := node.wrapper.Execute(macroCtx, &b)
|
||||
if err != nil {
|
||||
return AsSafeValue(err.updateFromTokenIfNeeded(ctx.template, node.position).Error())
|
||||
}
|
||||
|
||||
return AsSafeValue(b.String())
|
||||
}
|
||||
|
||||
func tagMacroParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
macroNode := &tagMacroNode{
|
||||
position: start,
|
||||
args: make(map[string]IEvaluator),
|
||||
}
|
||||
|
||||
nameToken := arguments.MatchType(TokenIdentifier)
|
||||
if nameToken == nil {
|
||||
return nil, arguments.Error("Macro-tag needs at least an identifier as name.", nil)
|
||||
}
|
||||
macroNode.name = nameToken.Val
|
||||
|
||||
if arguments.MatchOne(TokenSymbol, "(") == nil {
|
||||
return nil, arguments.Error("Expected '('.", nil)
|
||||
}
|
||||
|
||||
for arguments.Match(TokenSymbol, ")") == nil {
|
||||
argNameToken := arguments.MatchType(TokenIdentifier)
|
||||
if argNameToken == nil {
|
||||
return nil, arguments.Error("Expected argument name as identifier.", nil)
|
||||
}
|
||||
macroNode.argsOrder = append(macroNode.argsOrder, argNameToken.Val)
|
||||
|
||||
if arguments.Match(TokenSymbol, "=") != nil {
|
||||
// Default expression follows
|
||||
argDefaultExpr, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
macroNode.args[argNameToken.Val] = argDefaultExpr
|
||||
} else {
|
||||
// No default expression
|
||||
macroNode.args[argNameToken.Val] = nil
|
||||
}
|
||||
|
||||
if arguments.Match(TokenSymbol, ")") != nil {
|
||||
break
|
||||
}
|
||||
if arguments.Match(TokenSymbol, ",") == nil {
|
||||
return nil, arguments.Error("Expected ',' or ')'.", nil)
|
||||
}
|
||||
}
|
||||
|
||||
if arguments.Match(TokenKeyword, "export") != nil {
|
||||
macroNode.exported = true
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed macro-tag.", nil)
|
||||
}
|
||||
|
||||
// Body wrapping
|
||||
wrapper, endargs, err := doc.WrapUntilTag("endmacro")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
macroNode.wrapper = wrapper
|
||||
|
||||
if endargs.Count() > 0 {
|
||||
return nil, endargs.Error("Arguments not allowed here.", nil)
|
||||
}
|
||||
|
||||
if macroNode.exported {
|
||||
// Now register the macro if it wants to be exported
|
||||
_, has := doc.template.exportedMacros[macroNode.name]
|
||||
if has {
|
||||
return nil, doc.Error(fmt.Sprintf("another macro with name '%s' already exported", macroNode.name), start)
|
||||
}
|
||||
doc.template.exportedMacros[macroNode.name] = macroNode
|
||||
}
|
||||
|
||||
return macroNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("macro", tagMacroParser)
|
||||
}
|
50
vendor/github.com/flosch/pongo2/tags_now.go
generated
vendored
Normal file
50
vendor/github.com/flosch/pongo2/tags_now.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type tagNowNode struct {
|
||||
position *Token
|
||||
format string
|
||||
fake bool
|
||||
}
|
||||
|
||||
func (node *tagNowNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
var t time.Time
|
||||
if node.fake {
|
||||
t = time.Date(2014, time.February, 05, 18, 31, 45, 00, time.UTC)
|
||||
} else {
|
||||
t = time.Now()
|
||||
}
|
||||
|
||||
writer.WriteString(t.Format(node.format))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagNowParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
nowNode := &tagNowNode{
|
||||
position: start,
|
||||
}
|
||||
|
||||
formatToken := arguments.MatchType(TokenString)
|
||||
if formatToken == nil {
|
||||
return nil, arguments.Error("Expected a format string.", nil)
|
||||
}
|
||||
nowNode.format = formatToken.Val
|
||||
|
||||
if arguments.MatchOne(TokenIdentifier, "fake") != nil {
|
||||
nowNode.fake = true
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed now-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return nowNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("now", tagNowParser)
|
||||
}
|
50
vendor/github.com/flosch/pongo2/tags_set.go
generated
vendored
Normal file
50
vendor/github.com/flosch/pongo2/tags_set.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
package pongo2
|
||||
|
||||
type tagSetNode struct {
|
||||
name string
|
||||
expression IEvaluator
|
||||
}
|
||||
|
||||
func (node *tagSetNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
// Evaluate expression
|
||||
value, err := node.expression.Evaluate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx.Private[node.name] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagSetParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
node := &tagSetNode{}
|
||||
|
||||
// Parse variable name
|
||||
typeToken := arguments.MatchType(TokenIdentifier)
|
||||
if typeToken == nil {
|
||||
return nil, arguments.Error("Expected an identifier.", nil)
|
||||
}
|
||||
node.name = typeToken.Val
|
||||
|
||||
if arguments.Match(TokenSymbol, "=") == nil {
|
||||
return nil, arguments.Error("Expected '='.", nil)
|
||||
}
|
||||
|
||||
// Variable expression
|
||||
keyExpression, err := arguments.ParseExpression()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node.expression = keyExpression
|
||||
|
||||
// Remaining arguments
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed 'set'-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("set", tagSetParser)
|
||||
}
|
54
vendor/github.com/flosch/pongo2/tags_spaceless.go
generated
vendored
Normal file
54
vendor/github.com/flosch/pongo2/tags_spaceless.go
generated
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type tagSpacelessNode struct {
|
||||
wrapper *NodeWrapper
|
||||
}
|
||||
|
||||
var tagSpacelessRegexp = regexp.MustCompile(`(?U:(<.*>))([\t\n\v\f\r ]+)(?U:(<.*>))`)
|
||||
|
||||
func (node *tagSpacelessNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
b := bytes.NewBuffer(make([]byte, 0, 1024)) // 1 KiB
|
||||
|
||||
err := node.wrapper.Execute(ctx, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := b.String()
|
||||
// Repeat this recursively
|
||||
changed := true
|
||||
for changed {
|
||||
s2 := tagSpacelessRegexp.ReplaceAllString(s, "$1$3")
|
||||
changed = s != s2
|
||||
s = s2
|
||||
}
|
||||
|
||||
writer.WriteString(s)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagSpacelessParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
spacelessNode := &tagSpacelessNode{}
|
||||
|
||||
wrapper, _, err := doc.WrapUntilTag("endspaceless")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
spacelessNode.wrapper = wrapper
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed spaceless-tag arguments.", nil)
|
||||
}
|
||||
|
||||
return spacelessNode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("spaceless", tagSpacelessParser)
|
||||
}
|
68
vendor/github.com/flosch/pongo2/tags_ssi.go
generated
vendored
Normal file
68
vendor/github.com/flosch/pongo2/tags_ssi.go
generated
vendored
Normal file
|
@ -0,0 +1,68 @@
|
|||
package pongo2
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type tagSSINode struct {
|
||||
filename string
|
||||
content string
|
||||
template *Template
|
||||
}
|
||||
|
||||
func (node *tagSSINode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
|
||||
if node.template != nil {
|
||||
// Execute the template within the current context
|
||||
includeCtx := make(Context)
|
||||
includeCtx.Update(ctx.Public)
|
||||
includeCtx.Update(ctx.Private)
|
||||
|
||||
err := node.template.execute(includeCtx, writer)
|
||||
if err != nil {
|
||||
return err.(*Error)
|
||||
}
|
||||
} else {
|
||||
// Just print out the content
|
||||
writer.WriteString(node.content)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tagSSIParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
|
||||
SSINode := &tagSSINode{}
|
||||
|
||||
if fileToken := arguments.MatchType(TokenString); fileToken != nil {
|
||||
SSINode.filename = fileToken.Val
|
||||
|
||||
if arguments.Match(TokenIdentifier, "parsed") != nil {
|
||||
// parsed
|
||||
temporaryTpl, err := doc.template.set.FromFile(doc.template.set.resolveFilename(doc.template, fileToken.Val))
|
||||
if err != nil {
|
||||
return nil, err.(*Error).updateFromTokenIfNeeded(doc.template, fileToken)
|
||||
}
|
||||
SSINode.template = temporaryTpl
|
||||
} else {
|
||||
// plaintext
|
||||
buf, err := ioutil.ReadFile(doc.template.set.resolveFilename(doc.template, fileToken.Val))
|
||||
if err != nil {
|
||||
return nil, (&Error{
|
||||
Sender: "tag:ssi",
|
||||
OrigError: err,
|
||||
}).updateFromTokenIfNeeded(doc.template, fileToken)
|
||||
}
|
||||
SSINode.content = string(buf)
|
||||
}
|
||||
} else {
|
||||
return nil, arguments.Error("First argument must be a string.", nil)
|
||||
}
|
||||
|
||||
if arguments.Remaining() > 0 {
|
||||
return nil, arguments.Error("Malformed SSI-tag argument.", nil)
|
||||
}
|
||||
|
||||
return SSINode, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterTag("ssi", tagSSIParser)
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue