github.com/hairyhenderson/gomplate/v3@v3.11.7/funcs/conv.go (about) 1 package funcs 2 3 import ( 4 "context" 5 "net/url" 6 "text/template" 7 8 "github.com/hairyhenderson/gomplate/v3/coll" 9 "github.com/hairyhenderson/gomplate/v3/conv" 10 "github.com/hairyhenderson/gomplate/v3/internal/deprecated" 11 ) 12 13 // ConvNS - 14 // Deprecated: don't use 15 func ConvNS() *ConvFuncs { 16 return &ConvFuncs{} 17 } 18 19 // AddConvFuncs - 20 // Deprecated: use CreateConvFuncs instead 21 func AddConvFuncs(f map[string]interface{}) { 22 for k, v := range CreateConvFuncs(context.Background()) { 23 f[k] = v 24 } 25 } 26 27 // CreateConvFuncs - 28 func CreateConvFuncs(ctx context.Context) map[string]interface{} { 29 ns := &ConvFuncs{ctx} 30 31 f := map[string]interface{}{} 32 f["conv"] = func() interface{} { return ns } 33 34 f["urlParse"] = ns.URL 35 f["bool"] = ns.Bool 36 f["join"] = ns.Join 37 f["default"] = ns.Default 38 return f 39 } 40 41 // ConvFuncs - 42 type ConvFuncs struct { 43 ctx context.Context 44 } 45 46 // Bool - 47 // Deprecated: use ToBool instead 48 func (f *ConvFuncs) Bool(s interface{}) bool { 49 deprecated.WarnDeprecated(f.ctx, "conv.Bool is deprecated - use conv.ToBool instead") 50 return conv.Bool(conv.ToString(s)) 51 } 52 53 // ToBool - 54 func (ConvFuncs) ToBool(in interface{}) bool { 55 return conv.ToBool(in) 56 } 57 58 // ToBools - 59 func (ConvFuncs) ToBools(in ...interface{}) []bool { 60 return conv.ToBools(in...) 61 } 62 63 // Slice - 64 // Deprecated: use coll.Slice instead 65 func (f *ConvFuncs) Slice(args ...interface{}) []interface{} { 66 deprecated.WarnDeprecated(f.ctx, "conv.Slice is deprecated - use coll.Slice instead") 67 return coll.Slice(args...) 68 } 69 70 // Join - 71 func (ConvFuncs) Join(in interface{}, sep string) (string, error) { 72 return conv.Join(in, sep) 73 } 74 75 // Has - 76 // Deprecated: use coll.Has instead 77 func (f *ConvFuncs) Has(in interface{}, key string) bool { 78 deprecated.WarnDeprecated(f.ctx, "conv.Has is deprecated - use coll.Has instead") 79 return coll.Has(in, key) 80 } 81 82 // ParseInt - 83 func (ConvFuncs) ParseInt(s interface{}, base, bitSize int) int64 { 84 return conv.MustParseInt(conv.ToString(s), base, bitSize) 85 } 86 87 // ParseFloat - 88 func (ConvFuncs) ParseFloat(s interface{}, bitSize int) float64 { 89 return conv.MustParseFloat(conv.ToString(s), bitSize) 90 } 91 92 // ParseUint - 93 func (ConvFuncs) ParseUint(s interface{}, base, bitSize int) uint64 { 94 return conv.MustParseUint(conv.ToString(s), base, bitSize) 95 } 96 97 // Atoi - 98 func (ConvFuncs) Atoi(s interface{}) int { 99 return conv.MustAtoi(conv.ToString(s)) 100 } 101 102 // URL - 103 func (ConvFuncs) URL(s interface{}) (*url.URL, error) { 104 return url.Parse(conv.ToString(s)) 105 } 106 107 // ToInt64 - 108 func (ConvFuncs) ToInt64(in interface{}) int64 { 109 return conv.ToInt64(in) 110 } 111 112 // ToInt - 113 func (ConvFuncs) ToInt(in interface{}) int { 114 return conv.ToInt(in) 115 } 116 117 // ToInt64s - 118 func (ConvFuncs) ToInt64s(in ...interface{}) []int64 { 119 return conv.ToInt64s(in...) 120 } 121 122 // ToInts - 123 func (ConvFuncs) ToInts(in ...interface{}) []int { 124 return conv.ToInts(in...) 125 } 126 127 // ToFloat64 - 128 func (ConvFuncs) ToFloat64(in interface{}) float64 { 129 return conv.ToFloat64(in) 130 } 131 132 // ToFloat64s - 133 func (ConvFuncs) ToFloat64s(in ...interface{}) []float64 { 134 return conv.ToFloat64s(in...) 135 } 136 137 // ToString - 138 func (ConvFuncs) ToString(in interface{}) string { 139 return conv.ToString(in) 140 } 141 142 // ToStrings - 143 func (ConvFuncs) ToStrings(in ...interface{}) []string { 144 return conv.ToStrings(in...) 145 } 146 147 // Default - 148 func (ConvFuncs) Default(def, in interface{}) interface{} { 149 if truth, ok := template.IsTrue(in); truth && ok { 150 return in 151 } 152 return def 153 } 154 155 // Dict - 156 // Deprecated: use coll.Dict instead 157 func (f *ConvFuncs) Dict(in ...interface{}) (map[string]interface{}, error) { 158 deprecated.WarnDeprecated(f.ctx, "conv.Dict is deprecated - use coll.Dict instead") 159 return coll.Dict(in...) 160 }