Skip to content

Commit

Permalink
localization: implements sprig functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Jun 19, 2019
1 parent 1f5f835 commit 53ec88e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module gitlab.com/Cacophony/go-kit

require (
cloud.google.com/go v0.40.0 // indirect
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.20.0+incompatible
github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937
github.com/Unleash/unleash-client-go v0.0.0-20190225211619-9febc6ff26f4
github.com/aws/aws-sdk-go v1.19.46 // indirect
Expand All @@ -13,6 +16,8 @@ require (
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-redis/redis v6.15.2+incompatible
github.com/google/uuid v1.1.1
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/jinzhu/gorm v1.9.8
github.com/jinzhu/inflection v0.0.0-20190603042836-f5c5f50e6090 // indirect
github.com/json-iterator/go v1.1.6
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ github.com/Azure/go-autorest/tracing v0.1.0/go.mod h1:ROEEAFwXycQw7Sn3DXNtEedEvd
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20190418212003-6ac0b49e7197/go.mod h1:aJ4qN3TfrelA6NZ6AXsXRfmEVaYin3EDbSPJrKS8OXo=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.20.0+incompatible h1:dJTKKuUkYW3RMFdQFXPU/s6hg10RgctmTjRcbZ98Ap8=
github.com/Masterminds/sprig v2.20.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937 h1:i7uyK5VEr5L/ZJEL4Bn8ff0dk+a5z0g9zJjSYxGyf0c=
github.com/Seklfreak/polr-go v0.0.0-20190324143256-a87fed130937/go.mod h1:eoGl/sGHUD/1hNQEHPvNunsRryc3c4Im57OavEzSxiw=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
Expand Down Expand Up @@ -128,6 +134,10 @@ github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/jinzhu/gorm v1.9.8 h1:n5uvxqLepIP2R1XF7pudpt9Rv8I3m7G9trGxJVjLZ5k=
github.com/jinzhu/gorm v1.9.8/go.mod h1:bdqTT3q6dhSph2K3pWxrHP6nqxuAp2yQ3KFtc3U3F84=
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k=
Expand Down
2 changes: 1 addition & 1 deletion localization/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package localization
import "text/template"

func newTemplate(key, value string) (*template.Template, error) {
return template.New(key).Funcs(translationFuncs).Parse(value)
return template.New(key).Funcs(getTranslationFuncs()).Parse(value)
}
28 changes: 26 additions & 2 deletions localization/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"text/template"
"time"

polr "github.com/Seklfreak/polr-go"
"github.com/Masterminds/sprig"
"github.com/Seklfreak/polr-go"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"

humanize "github.com/dustin/go-humanize"
"github.com/dustin/go-humanize"
)

var (
Expand All @@ -38,6 +39,29 @@ func init() {
}
}

func getTranslationFuncs() map[string]interface{} {
// start with sprig
methods := sprig.FuncMap()

// remove dangerous methods
for _, unsafe := range []string{
"env",
"expandenv",
"typeOf",
"kindIs",
"typeIsLike",
} {
delete(methods, unsafe)
}

// add custom methods
for key, method := range translationFuncs {
methods[key] = method
}

return methods
}

var (
// the additional functions to use in the template engine
translationFuncs = template.FuncMap{
Expand Down

0 comments on commit 53ec88e

Please sign in to comment.