diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2024-01-25 20:11:31 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-25 20:11:31 -0500 |
| commit | ebb97fb7367fb983cffc1935a8fb57e4b80f5249 (patch) | |
| tree | 43ef6cd01f629f60f59efe1e5b003f7c8e3a1257 /internal/funcs/conv.go | |
| parent | f1d9158ea99abbe556251c1ff2fe970f3b460ee9 (diff) | |
Move funcs package to internal (#1977)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'internal/funcs/conv.go')
| -rw-r--r-- | internal/funcs/conv.go | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/internal/funcs/conv.go b/internal/funcs/conv.go new file mode 100644 index 00000000..3361879d --- /dev/null +++ b/internal/funcs/conv.go @@ -0,0 +1,166 @@ +package funcs + +import ( + "context" + "net/url" + "text/template" + + "github.com/hairyhenderson/gomplate/v4/coll" + "github.com/hairyhenderson/gomplate/v4/conv" + "github.com/hairyhenderson/gomplate/v4/internal/deprecated" +) + +// ConvNS - +// +// Deprecated: don't use +func ConvNS() *ConvFuncs { + return &ConvFuncs{} +} + +// AddConvFuncs - +// +// Deprecated: use [CreateConvFuncs] instead +func AddConvFuncs(f map[string]interface{}) { + for k, v := range CreateConvFuncs(context.Background()) { + f[k] = v + } +} + +// CreateConvFuncs - +func CreateConvFuncs(ctx context.Context) map[string]interface{} { + ns := &ConvFuncs{ctx} + + f := map[string]interface{}{} + f["conv"] = func() interface{} { return ns } + + f["urlParse"] = ns.URL + f["bool"] = ns.Bool + f["join"] = ns.Join + f["default"] = ns.Default + return f +} + +// ConvFuncs - +type ConvFuncs struct { + ctx context.Context +} + +// Bool - +// +// Deprecated: use [ToBool] instead +func (f *ConvFuncs) Bool(s interface{}) bool { + deprecated.WarnDeprecated(f.ctx, "conv.Bool is deprecated - use conv.ToBool instead") + return conv.Bool(conv.ToString(s)) +} + +// ToBool - +func (ConvFuncs) ToBool(in interface{}) bool { + return conv.ToBool(in) +} + +// ToBools - +func (ConvFuncs) ToBools(in ...interface{}) []bool { + return conv.ToBools(in...) +} + +// Slice - +// +// Deprecated: use [CollFuncs.Slice] instead +func (f *ConvFuncs) Slice(args ...interface{}) []interface{} { + deprecated.WarnDeprecated(f.ctx, "conv.Slice is deprecated - use coll.Slice instead") + return coll.Slice(args...) +} + +// Join - +func (ConvFuncs) Join(in interface{}, sep string) (string, error) { + return conv.Join(in, sep) +} + +// Has - +// +// Deprecated: use [CollFuncs.Has] instead +func (f *ConvFuncs) Has(in interface{}, key string) bool { + deprecated.WarnDeprecated(f.ctx, "conv.Has is deprecated - use coll.Has instead") + return coll.Has(in, key) +} + +// ParseInt - +func (ConvFuncs) ParseInt(s interface{}, base, bitSize int) int64 { + return conv.MustParseInt(conv.ToString(s), base, bitSize) +} + +// ParseFloat - +func (ConvFuncs) ParseFloat(s interface{}, bitSize int) float64 { + return conv.MustParseFloat(conv.ToString(s), bitSize) +} + +// ParseUint - +func (ConvFuncs) ParseUint(s interface{}, base, bitSize int) uint64 { + return conv.MustParseUint(conv.ToString(s), base, bitSize) +} + +// Atoi - +func (ConvFuncs) Atoi(s interface{}) int { + return conv.MustAtoi(conv.ToString(s)) +} + +// URL - +func (ConvFuncs) URL(s interface{}) (*url.URL, error) { + return url.Parse(conv.ToString(s)) +} + +// ToInt64 - +func (ConvFuncs) ToInt64(in interface{}) int64 { + return conv.ToInt64(in) +} + +// ToInt - +func (ConvFuncs) ToInt(in interface{}) int { + return conv.ToInt(in) +} + +// ToInt64s - +func (ConvFuncs) ToInt64s(in ...interface{}) []int64 { + return conv.ToInt64s(in...) +} + +// ToInts - +func (ConvFuncs) ToInts(in ...interface{}) []int { + return conv.ToInts(in...) +} + +// ToFloat64 - +func (ConvFuncs) ToFloat64(in interface{}) float64 { + return conv.ToFloat64(in) +} + +// ToFloat64s - +func (ConvFuncs) ToFloat64s(in ...interface{}) []float64 { + return conv.ToFloat64s(in...) +} + +// ToString - +func (ConvFuncs) ToString(in interface{}) string { + return conv.ToString(in) +} + +// ToStrings - +func (ConvFuncs) ToStrings(in ...interface{}) []string { + return conv.ToStrings(in...) +} + +// Default - +func (ConvFuncs) Default(def, in interface{}) interface{} { + if truth, ok := template.IsTrue(in); truth && ok { + return in + } + return def +} + +// Dict - +// +// Deprecated: use [CollFuncs.Dict] instead +func (f *ConvFuncs) Dict(in ...interface{}) (map[string]interface{}, error) { + deprecated.WarnDeprecated(f.ctx, "conv.Dict is deprecated - use coll.Dict instead") + return coll.Dict(in...) +} |
