Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
4b81234
Initial fork
Draal May 15, 2020
fe5cffc
Add ability to find variables
Draal May 15, 2020
2151af5
Merge pull request #1 from autopilot3/find-variables
Draal May 17, 2020
e3fc505
Fix `{{ people.custom.company | default: 'there' }}` variables
Draal Jun 26, 2020
66db265
Merge pull request #2 from autopilot3/fix-deeper-than2-variables
Draal Jun 26, 2020
d23f1ce
new filter for subject
HenryHK Oct 15, 2020
808c219
go mod tidy
HenryHK Oct 15, 2020
4bfaaaf
Merge pull request #3 from autopilot3/new-filter
HenryHK Oct 15, 2020
ec1875a
add phone default support
HenryHK Oct 15, 2020
7d5fa29
Merge pull request #4 from autopilot3/fix/phone-default
HenryHK Oct 15, 2020
1c32e6b
custom tags
HenryHK Oct 15, 2020
e40a748
crm display types const to avoid cyclic import
HenryHK Oct 15, 2020
aded6c2
Merge pull request #5 from autopilot3/custom-tags
HenryHK Oct 15, 2020
461df22
fix bad naming
mmoustai Jan 13, 2021
1489914
fix type
mmoustai Jan 13, 2021
ae307ff
Merge pull request #6 from autopilot3/fix/naming
mmoustai Jan 13, 2021
11c4dae
add activity id
HenryHK Mar 17, 2021
23c0b05
fix multiplier for decimal/currency
HenryHK May 27, 2021
4bc049a
Merge pull request #7 from autopilot3/fix/decimal-currency-multiplier
HenryHK May 27, 2021
05b6977
fix decimal filter default value not working
HenryHK Jul 6, 2021
da80a6f
add log
HenryHK Jul 6, 2021
d4cfe9a
Merge pull request #8 from autopilot3/fix/decimal-default
HenryHK Jul 6, 2021
4819f27
Empty bool value
mmoustai Aug 30, 2021
d88d60a
Use string value
mmoustai Aug 30, 2021
2147cdc
fix missing leading 0 for month/day
mmoustai Aug 30, 2021
ce46d96
Merge pull request #9 from autopilot3/fix/empty-bool
mmoustai Aug 30, 2021
7a67cc3
Add buildkite
mmoustai Aug 30, 2021
efa8533
introduce thousand delimiter for decimal by default
HenryHK Nov 22, 2021
9a3bc46
Merge pull request #10 from autopilot3/fix/decimal-thousand-delimiter
HenryHK Nov 23, 2021
5a9727e
number with delimiter by language
HenryHK Jan 5, 2022
499265c
Merge pull request #11 from autopilot3/fix/decimal-thousand-delimiter
HenryHK Jan 5, 2022
4ddc61f
ad setContains/setContainsAll
HenryHK Feb 15, 2022
1487e0a
Merge pull request #12 from autopilot3/feat/setContains
HenryHK Feb 15, 2022
8d6225f
feat(liquid): add hability to handle array
sfroment Feb 22, 2022
08b2498
fix: tu
sfroment Feb 23, 2022
784dcce
chore: better than changing yacc
sfroment Feb 23, 2022
0119a7a
chore: update go.mod
sfroment Feb 23, 2022
2ddb152
chore: unexport ArrayValue
sfroment Feb 23, 2022
8840b8d
chore: review
sfroment Mar 2, 2022
0fe1235
Merge pull request #14 from autopilot3/dev/sfroment/add-array-handling
sfroment Mar 2, 2022
cfb63fb
remove unused var
mmoustai Mar 11, 2022
62a21a0
Merge pull request #15 from autopilot3/feat/remove-unused-var
mmoustai Mar 11, 2022
262af88
now we have proper array types in liquid so we need to adapt to it
HenryHK Apr 4, 2022
f303c64
Merge pull request #16 from autopilot3/fix/setContains-backwards-comp…
HenryHK Apr 4, 2022
a6768ed
add raw phone filter
HenryHK Apr 6, 2022
bf4fb98
Merge pull request #17 from autopilot3/fix/raw-phone
HenryHK Apr 6, 2022
5246c8f
Fix capitalise for non Latin characters, optimize slice
Aug 7, 2022
f9a25b6
Merge pull request #18 from autopilot3/fix-liquid-cap
Draal Aug 8, 2022
af26c4a
Update go mod
Aug 18, 2022
5e2c6b0
fix go mod
Aug 18, 2022
eacc0db
we want to check every condition in condition expressions for FindVar…
HenryHK Sep 2, 2022
b3bfe8e
lint
HenryHK Sep 2, 2022
1640aec
Merge pull request #19 from autopilot3/fix/if-condition-find-vars
HenryHK Sep 2, 2022
fca3c3f
add ability to only render allowed merge tags
HenryHK Sep 8, 2022
94ee971
Merge pull request #20 from autopilot3/feat/only-render-allowed-tags
HenryHK Sep 8, 2022
203ed38
MD5 and SHA1 filters
xitonix Sep 9, 2022
39122cd
SHA256 Filter
xitonix Sep 11, 2022
2b550c1
HMAC MD5
xitonix Sep 11, 2022
ef283b0
HMAC SHA1 & SHA256
xitonix Sep 11, 2022
9fc30e6
Do not hash string inputs
xitonix Sep 11, 2022
74ed9c8
at_least & at_most filters
xitonix Sep 11, 2022
5ccc042
Merge pull request #21 from autopilot3/feat/md5-sha1-filters
xitonix Sep 12, 2022
1ddc8b1
add support for gtypes date type
HenryHK Sep 19, 2022
206fc14
Merge pull request #22 from autopilot3/fix/support-gtypes-date
HenryHK Sep 19, 2022
69e0b28
fix nil used on date filter
HenryHK Sep 20, 2022
8a1aa7d
Merge pull request #23 from autopilot3/fix/date-filter-on-nil
HenryHK Sep 20, 2022
2d71610
use contains to properly check if source contains allowed tags
HenryHK Sep 21, 2022
a396b07
Merge pull request #24 from autopilot3/fix/allowed-tags-contains-check
HenryHK Sep 21, 2022
0248e88
allow tags with default for better templates screenshot rendering
HenryHK Nov 9, 2022
7822ad3
avoid double check
HenryHK Nov 9, 2022
0cd651a
WIP
HenryHK Nov 9, 2022
09ea7ac
if we enter allowed tags mode, always render the first branch in cont…
HenryHK Nov 16, 2022
3624a4f
render control flow as it is
HenryHK Nov 16, 2022
9aaca9c
Merge pull request #25 from autopilot3/fix/allowed-tags-with-default
HenryHK Nov 16, 2022
847fcc2
time in tz filter
HenryHK Dec 12, 2022
e619b8e
Merge pull request #26 from autopilot3/time-in-tz
HenryHK Dec 12, 2022
b6d10d3
add date filter to support number type
HenryHK Dec 14, 2022
4c1e3c2
Merge pull request #27 from autopilot3/date-number-type
HenryHK Dec 14, 2022
021f417
new dateformat available for date/time filter
HenryHK Dec 19, 2022
5b6df92
Merge pull request #28 from autopilot3/fix/new-date-format
HenryHK Dec 19, 2022
f03e31b
fix: liquid proposal 2
sfroment Mar 2, 2023
6e4d493
Merge pull request #30 from autopilot3/dev/sfroment/proposal-2
sfroment Mar 2, 2023
53137de
add decimalWithDelimiter filter supporting localisation separator
HenryHK May 22, 2023
9ce47a5
chore: go mod
sfroment Jun 7, 2023
05cd76b
Merge pull request #33 from autopilot3/dev/sfromnet/go-mod
sfroment Jun 7, 2023
4a040f6
chore: go mod
sfroment Jun 8, 2023
24495b0
Merge pull request #34 from autopilot3/dev/sfroment/go-mod-123123123
sfroment Jun 8, 2023
e918b48
chore: go mod
sfroment Jun 8, 2023
1ebeda5
Merge pull request #35 from autopilot3/dev/sfroment/go-mod-123123123
sfroment Jun 8, 2023
b1a99f1
chore: Remove autopilot from go.sum
sfroment Jun 14, 2023
8cf9c65
chore: Remove autopilot from go.sum & go.mod
sfroment Jun 14, 2023
5257412
add dummy trackURL filter to bypass liquid syntax check
HenryHK Jul 24, 2023
5cbf031
fix mod
HenryHK Jul 24, 2023
3bddbfd
add $$TRACK_ME to help identify url want to be tracked
HenryHK Jul 24, 2023
50e84f5
lint
HenryHK Jul 24, 2023
5b9626d
Merge pull request #38 from autopilot3/feat/dummy-trackURL-filter
HenryHK Jul 24, 2023
30b2413
unescape string before track URL before bee could escape the append/o…
HenryHK Aug 21, 2023
c2fa82e
Merge pull request #39 from autopilot3/fix/unescape-html-before-trackURL
HenryHK Aug 21, 2023
44f219e
Merge pull request #32 from autopilot3/decimal-with-delimiter-for-loc
HenryHK Oct 16, 2023
e46e2b0
retire ioutil
HenryHK Oct 17, 2023
bf76df2
Merge pull request #40 from autopilot3/fix/staticheck-lint
HenryHK Oct 17, 2023
78aaa50
date time filter supports time too
HenryHK Feb 4, 2024
1de69f6
add dateFormatOrDefault test
HenryHK Feb 4, 2024
004098d
Merge pull request #41 from autopilot3/fix/dateTime-filter-supports-time
HenryHK Feb 4, 2024
bb1da1f
Fix find vars inside control flow blocks
Mar 24, 2024
e1eb639
Merge pull request #42 from autopilot3/read-vars-inside-if-blocks
Draal Mar 24, 2024
9b5f70c
Fix case control flow find vars
Mar 25, 2024
aa51347
Merge pull request #43 from autopilot3/fix-case-find-vars
Draal Mar 25, 2024
422dcbd
Implement loop vars
Mar 26, 2024
832e15d
extend compare to auto detect if the comparer is time and do comparis…
HenryHK Apr 3, 2024
8552c11
Merge pull request #45 from autopilot3/fix/extend-comparison-for-time
HenryHK Apr 3, 2024
83f6833
further extend time comparison to support either comparer is of time.…
HenryHK Apr 3, 2024
00effd3
Merge pull request #46 from autopilot3/fix/extend-comparison-for-time
HenryHK Apr 3, 2024
c16826d
Merge pull request #44 from autopilot3/implement-loop-vars
HenryHK Apr 3, 2024
2985580
support phone type equal compare
HenryHK Apr 23, 2024
5327e06
Merge pull request #47 from autopilot3/fix/phone-number-equal
HenryHK Apr 24, 2024
ea281ec
add check for phone type
HenryHK Aug 19, 2024
cb5d300
fix mod for vulnerabilities
HenryHK Aug 19, 2024
9cfce75
Merge pull request #48 from autopilot3/fix/add-logs-check-phone-cast
HenryHK Aug 19, 2024
1dc6769
add to_number filter
HenryHK Nov 6, 2024
4d89ad1
Merge pull request #49 from autopilot3/fix/add-to-i-filter
HenryHK Nov 6, 2024
9d29ed3
add protection and logs for variable not as VariableBind type
HenryHK Dec 22, 2024
5e0f3d6
Merge pull request #50 from autopilot3/fix/log-variable-bind-type
HenryHK Dec 22, 2024
7f5c576
only log phone info if it's not nil and failed to convert
HenryHK Mar 17, 2025
d710360
remove log
HenryHK Mar 17, 2025
a4bcc7f
Merge pull request #51 from autopilot3/fix/only-log-phone-if-not-nil
HenryHK Mar 17, 2025
2f95634
improve nil value comparison if nil values involved
HenryHK Mar 17, 2025
ba0664d
add examples for improved comparison
HenryHK Mar 17, 2025
67b9e1b
Merge pull request #52 from autopilot3/fix/improve-nil-value-comparison
HenryHK Mar 17, 2025
d28f0d5
add startsWith/endsWith
HenryHK Mar 31, 2025
8f1e044
Merge pull request #53 from autopilot3/fix/add-start-end-with
HenryHK Mar 31, 2025
53e3558
fix slice of interface cast
HenryHK Apr 12, 2025
204768c
fix lint
HenryHK Apr 13, 2025
aa0d052
Merge pull request #54 from autopilot3/fix/slice-of-interface-cast
HenryHK Apr 13, 2025
366f428
add check for divider
HenryHK May 2, 2025
35945fd
Merge pull request #55 from autopilot3/fix/check-divider
HenryHK May 2, 2025
b54bea0
use same var for code consistency
HenryHK May 2, 2025
5c1cfed
add test
HenryHK May 2, 2025
a00cf4a
Merge pull request #56 from autopilot3/fix/check-divider
HenryHK May 2, 2025
51fb259
remove include tag to prevent filesystem access
HenryHK May 5, 2025
93dccf6
remove RenderFile implementation
HenryHK May 5, 2025
c00ab95
Merge pull request #57 from autopilot3/fix/remove-include
HenryHK May 6, 2025
43cb2e5
update dep
HenryHK May 7, 2025
0dd555c
Merge pull request #58 from autopilot3/fix/update-dep
HenryHK May 7, 2025
3fcaff7
add protection and debug log on VariableBind cast error case
HenryHK Jun 19, 2025
f3526c5
Merge pull request #59 from autopilot3/fix/protection-and-debug-log
HenryHK Jun 19, 2025
78ba949
Pass context to logs
Sep 2, 2025
55f7259
Merge pull request #60 from autopilot3/pass-ctx
Draal Sep 2, 2025
22dc0da
Var can be nil if it's assigned value
Sep 5, 2025
6d59d08
Merge pull request #61 from autopilot3/fix-assign-case
Draal Sep 7, 2025
31057e4
Fix using assigned vars in loops
Oct 16, 2025
4b0b563
Merge pull request #62 from autopilot3/fix-assign-var-loops
Draal Oct 16, 2025
f29275c
Add adv currency formatting
Nov 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
env:
DEPENDENCY_NAME: github.com/autopilot3/liquid
VERSION: ${VERSION:-master}

steps:
- label: ":buildkite: Generate Build Steps"
command: bksg -c golang-module-update -v "$VERSION"
retry:
automatic: true
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Checklist

- [ ] I have searched the [issue list](https://github.com/osteele/liquid/issues)
- [ ] I have searched the [issue list](https://github.com/autopilot3/liquid/issues)
- [ ] I have tested my example against Shopify Liquid. (This isn't necessary if the actual behavior is a panic, or an error for which `IsTemplateError` returns false.)

## Expected Behavior
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
*.output
*.out
/liquid
*.test
.idea
688 changes: 344 additions & 344 deletions CHANGELOG.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Here's some ways to help:

* Select an item from the [issues list](https://github.com/osteele/liquid/issues)
* Select an item from the [issues list](https://github.com/autopilot3/liquid/issues)
* Search the sources for FIXME and TODO comments.
* Improve the [code coverage](https://coveralls.io/github/osteele/liquid?branch=master).

Review the [pull request template](https://github.com/osteele/liquid/blob/master/.github/PULL_REQUEST_TEMPLATE.md) before you get too far along on coding.
Review the [pull request template](https://github.com/autopilot3/liquid/blob/master/.github/PULL_REQUEST_TEMPLATE.md) before you get too far along on coding.

A note on lint: `nolint: gocyclo` has been used to disable cyclomatic complexity checks on generated functions, hand-written parsers, and some of the generic interpreter functions. IMO this check isn't appropriate for those classes of functions. This isn't a license to disable cyclomatic complexity checks or lint in general.

Expand Down Expand Up @@ -40,7 +40,7 @@ make lint

```bash
godoc -http=:6060
open http://localhost:6060/pkg/github.com/osteele/liquid/
open http://localhost:6060/pkg/github.com/autopilot3/liquid/
```

### Work on the Expression Parser and Lexer
Expand Down
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SOURCEDIR=.
SOURCES := $(shell find $(SOURCEDIR) -name '*.go')

LIB = liquid
PACKAGE = github.com/osteele/liquid
PACKAGE = github.com/autopilot3/liquid
LDFLAGS=

.DEFAULT_GOAL: ci
Expand All @@ -24,17 +24,15 @@ imports: ## list imports
@go list -f '{{join .Imports "\n"}}' ./... | grep -v `go list -f '{{.ImportPath}}'` | grep '\.' | sort | uniq

lint: ## lint the package
gometalinter ./... --tests --deadline=5m --include=gofmt --exclude expressions/scanner.go --exclude y.go --exclude '.*_string.go' --disable=gotype --disable=interfacer
golangci-lint run
@echo lint passed

pre-commit: lint test ## lint and test the package

setup: ## install dependencies and development tools
go get golang.org/x/tools/cmd/stringer
go install golang.org/x/tools/cmd/stringer
go install golang.org/x/tools/cmd/goyacc
go get -t ./...
go get github.com/alecthomas/gometalinter
gometalinter --install

test: ## test the package
go test ./...
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ It was developed for use in the [Gojekyll](https://github.com/osteele/gojekyll)

`go get gopkg.in/osteele/liquid.v1` # latest snapshot

`go get -u github.com/osteele/liquid` # development version
`go get -u github.com/autopilot3/liquid` # development version

## Usage

Expand Down Expand Up @@ -83,7 +83,7 @@ Drops have a different design from the Shopify (Ruby) implementation.
A Ruby drop sets `liquid_attributes` to a list of attributes that are exposed to Liquid.
A Go drop implements `ToLiquid() interface{}`, that returns a proxy object.
Conventionally, the proxy is a `map` or `struct` that defines the exposed properties.
See <http://godoc.org/github.com/osteele/liquid#Drop> for additional information.
See <http://godoc.org/github.com/autopilot3/liquid#Drop> for additional information.

### Value Types

Expand Down Expand Up @@ -141,7 +141,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars2.githubusercontent.com/u/674?v=4" width="100px;"/><br /><sub><b>Oliver Steele</b></sub>](https://osteele.com/)<br />[💻](https://github.com/osteele/liquid/commits?author=osteele "Code") [📖](https://github.com/osteele/liquid/commits?author=osteele "Documentation") [🤔](#ideas-osteele "Ideas, Planning, & Feedback") [🚇](#infra-osteele "Infrastructure (Hosting, Build-Tools, etc)") [👀](#review-osteele "Reviewed Pull Requests") [⚠️](https://github.com/osteele/liquid/commits?author=osteele "Tests") | [<img src="https://avatars0.githubusercontent.com/u/973593?v=4" width="100px;"/><br /><sub><b>James Littlejohn</b></sub>](https://github.com/thessem)<br />[💻](https://github.com/osteele/liquid/commits?author=thessem "Code") [📖](https://github.com/osteele/liquid/commits?author=thessem "Documentation") [⚠️](https://github.com/osteele/liquid/commits?author=thessem "Tests") | [<img src="https://avatars2.githubusercontent.com/u/12567?v=4" width="100px;"/><br /><sub><b>nsf</b></sub>](http://nosmileface.ru)<br />[💻](https://github.com/osteele/liquid/commits?author=nsf "Code") [⚠️](https://github.com/osteele/liquid/commits?author=nsf "Tests") |
| [<img src="https://avatars2.githubusercontent.com/u/674?v=4" width="100px;"/><br /><sub><b>Oliver Steele</b></sub>](https://osteele.com/)<br />[💻](https://github.com/autopilot3/liquid/commits?author=osteele "Code") [📖](https://github.com/autopilot3/liquid/commits?author=osteele "Documentation") [🤔](#ideas-osteele "Ideas, Planning, & Feedback") [🚇](#infra-osteele "Infrastructure (Hosting, Build-Tools, etc)") [👀](#review-osteele "Reviewed Pull Requests") [⚠️](https://github.com/autopilot3/liquid/commits?author=osteele "Tests") | [<img src="https://avatars0.githubusercontent.com/u/973593?v=4" width="100px;"/><br /><sub><b>James Littlejohn</b></sub>](https://github.com/thessem)<br />[💻](https://github.com/autopilot3/liquid/commits?author=thessem "Code") [📖](https://github.com/autopilot3/liquid/commits?author=thessem "Documentation") [⚠️](https://github.com/autopilot3/liquid/commits?author=thessem "Tests") | [<img src="https://avatars2.githubusercontent.com/u/12567?v=4" width="100px;"/><br /><sub><b>nsf</b></sub>](http://nosmileface.ru)<br />[💻](https://github.com/autopilot3/liquid/commits?author=nsf "Code") [⚠️](https://github.com/autopilot3/liquid/commits?author=nsf "Tests") |
| :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down Expand Up @@ -177,14 +177,14 @@ MIT License
[coveralls-url]: https://coveralls.io/r/osteele/liquid?branch=master
[coveralls-svg]: https://img.shields.io/coveralls/osteele/liquid.svg?branch=master

[godoc-url]: https://godoc.org/github.com/osteele/liquid
[godoc-svg]: https://godoc.org/github.com/osteele/liquid?status.svg
[godoc-url]: https://godoc.org/github.com/autopilot3/liquid
[godoc-svg]: https://godoc.org/github.com/autopilot3/liquid?status.svg

[license-url]: https://github.com/osteele/liquid/blob/master/LICENSE
[license-url]: https://github.com/autopilot3/liquid/blob/master/LICENSE
[license-svg]: https://img.shields.io/badge/license-MIT-blue.svg

[go-report-card-url]: https://goreportcard.com/report/github.com/osteele/liquid
[go-report-card-svg]: https://goreportcard.com/badge/github.com/osteele/liquid
[go-report-card-url]: https://goreportcard.com/report/github.com/autopilot3/liquid
[go-report-card-svg]: https://goreportcard.com/badge/github.com/autopilot3/liquid

[travis-url]: https://travis-ci.org/osteele/liquid
[travis-svg]: https://img.shields.io/travis/osteele/liquid.svg?branch=master
Expand Down
9 changes: 4 additions & 5 deletions cmd/liquid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
//
// Examples:
//
// echo '{{ "Hello " | append: "World" }}' | liquid
// liquid source.tpl
// echo '{{ "Hello " | append: "World" }}' | liquid
// liquid source.tpl
package main

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/osteele/liquid"
"github.com/autopilot3/liquid"
)

// for testing
Expand Down Expand Up @@ -49,7 +48,7 @@ func run(args []string) error {
usage()
exit(1)
case len(args) == 1:
s, err := ioutil.ReadFile(args[0])
s, err := os.ReadFile(args[0])
if err != nil {
return err
}
Expand Down
110 changes: 110 additions & 0 deletions cmd/liquid/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"bytes"
"testing"
"time"

"github.com/autopilot3/ap3-types-go/types/date"
"github.com/autopilot3/liquid"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -46,3 +49,110 @@ func TestMain(t *testing.T) {
require.NoError(t, run([]string{"file1", "file2"}))
require.Equal(t, 1, exitCode)
}

func TestRenderAllowedTags(t *testing.T) {

bindings := map[string]interface{}{
"people": map[string]interface{}{
"name": "bob",
},
}
tests := []struct {
name string
allowTagsWithDefault bool
src string
expected string
}{
{
"Allow name only",
false,
"Hello {{ people.name | default: 'there' }}, your email is {{ people.email }}! {% if people.random == '123' %} you can't see me {% endif %}",
"Hello bob, your email is {{ people.email }}! {% if people.random == '123' %} you can't see me {% endif %}",
},
{
"Allow name only, others have default",
false,
"Hello {{ people.name | default: 'there' }}, your email is {{ people.email | default: 'unknown' }}!",
"Hello bob, your email is {{ people.email | default: 'unknown' }}!",
},
{
"Allow name and default",
true,
"Hello {{ people.name | default: 'there' }}, your email is {{ people.email | default: 'unknown' }}!",
"Hello bob, your email is unknown!",
},
{
"Allow name and default",
true,
"Hello {{ people.name | default: 'there' }}, your email is {{ people.email | default: 'unknown' }}!{% if people.random == '123' %} you can't see me.{% endif %}",
"Hello bob, your email is unknown!{% if people.random == '123' %} you can't see me.{% endif %}",
},
}
for _, tt := range tests {
engine := liquid.NewEngine()
engine.SetAllowedTags(map[string]struct{}{
"people.name": {},
})
if tt.allowTagsWithDefault {
engine.AllowedTagsWithDefault()
}
tmpl, err := engine.ParseString(tt.src)
if err != nil {
t.Fatal(err)
}
t.Run(tt.name, func(t *testing.T) {
out, err := tmpl.RenderString(bindings)
if err != nil {
t.Fatal(err)
}
if out != tt.expected {
t.Errorf("TestRenderAllowedTags() = %v, want %v", out, tt.expected)
}
})
}
}

func TestDateFormat(t *testing.T) {

bDate, _ := date.New(1994, 4, 28, "UTC")
bindings := map[string]interface{}{
"people": map[string]interface{}{
"birthday": bDate,
},
}
tests := []struct {
name string
v interface{}
src string
expected string
}{
{
"date",
bDate,
"{{ people.birthday | dateFormatOrDefault: 'dmy' | default: '0001-01-01' }}",
"28/04/1994",
},
{
"time",
time.Date(1994, time.April, 28, 0, 0, 0, 0, time.UTC),
"{{ people.birthday | dateFormatOrDefault: 'dmy' | default: '0001-01-01' }}",
"28/04/1994",
},
}
for _, tt := range tests {
engine := liquid.NewEngine()
tmpl, err := engine.ParseString(tt.src)
if err != nil {
t.Fatal(err)
}
t.Run(tt.name, func(t *testing.T) {
out, err := tmpl.RenderString(bindings)
if err != nil {
t.Fatal(err)
}
if out != tt.expected {
t.Errorf("TestRenderAllowedTags() = %v, want %v", out, tt.expected)
}
})
}
}
79 changes: 79 additions & 0 deletions customtags/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package customtags

import (
"fmt"

crmtypes "github.com/autopilot3/ap3-crm-api-go/services/types"
)

const (
TagDisplayTypeDate string = "date"
TagDisplayTypeTime string = "time"
TagDisplayTypeBool string = "bool"
TagDisplayTypeCurrency string = "currency"
TagDisplayTypeDecimal string = "decimal"
TagDisplayTypeAggregate string = "aggregate"
TagDisplayTypePhone string = "phone"
)

type TagType string

const (
TagTypeMergeTag TagType = "merge"
TagTypeMergeText TagType = "text"
)

type Tag struct {
ID string `json:"id" bson:"id"`
Type TagType `json:"type" bson:"type"`
Icon string `json:"icon" bson:"icon"`
Title string `json:"title" bson:"title"`
DisplayType string `json:"display_type" bson:"display_type"`
LiquidName string `json:"liquid_name" bson:"liquid_name"`
DefaultValue string `json:"default_value" bson:"default_value"`
FormatOption string `json:"format_option" bson:"format_option"`
FieldID crmtypes.FieldID `json:"field_id" bson:"field_id"`
ActivityID crmtypes.FieldID `json:"activity_id" bson:"activity_id"`
}

func (t *Tag) GetPreviewString() (string, error) {
if t.Type == TagTypeMergeText {
return t.Title, nil
}
if t.Type == TagTypeMergeTag {
return fmt.Sprintf("[%s]", t.Title), nil
}
return "", fmt.Errorf("unsupported subject merge tag %+v type: %s", t, t.Type)
}

func (t *Tag) GetLiquidString() (string, error) {
if t.Type == TagTypeMergeText {
return t.Title, nil
}
if t.Type == TagTypeMergeTag {
switch t.DisplayType {
case TagDisplayTypeDate:
return fmt.Sprintf(`{{ %s | dateFormatOrDefault: "%s", "%s" }}`, t.LiquidName, t.FormatOption, t.DefaultValue), nil
case TagDisplayTypeTime:
return fmt.Sprintf(`{{ %s | dateTimeFormatOrDefault: "%s", "%s" }}`, t.LiquidName, t.FormatOption, t.DefaultValue), nil
case TagDisplayTypeBool:
return fmt.Sprintf(`{{ %s | booleanFormat: "%s" }}`, t.LiquidName, t.FormatOption), nil
case TagDisplayTypeCurrency, TagDisplayTypeDecimal, TagDisplayTypeAggregate:
return fmt.Sprintf(`{{ %s | decimal: "%s", "%s" }}`, t.LiquidName, t.FormatOption, t.DefaultValue), nil
case TagDisplayTypePhone:
willHide := false
if t.FormatOption == "hide" {
willHide = true
}
return fmt.Sprintf(`{{ %s | hideCountryCodeAndDefault: %t, "%s" }}`, t.LiquidName, willHide, t.DefaultValue), nil
default:
result := `{{ ` + t.LiquidName
if t.DefaultValue != "" {
result += ` | default: "` + t.DefaultValue + `"`
}
result += ` }}`
return result, nil
}
}
return "", fmt.Errorf("unsupported subject merge tag %+v type: %s", t, t.Type)
}
Loading