88 "bytes"
99 "errors"
1010 "io"
11- "math/rand"
11+ "math/rand"
1212 "text/template"
1313 "time"
1414
@@ -50,22 +50,20 @@ var awsAZs map[string][]string = map[string][]string{
5050 "us-west-2" : {"us-west-2a" , "us-west-2b" , "us-west-2c" , "us-west-2d" },
5151}
5252
53- func calculateTotEventsWithTextTemplate (totSize uint64 , fieldMap map [string ]any , errChan chan error , tpl []byte ) (uint64 , error ) {
53+ func calculateTotEventsWithTextTemplate (totSize uint64 , fieldMap map [string ]any , errChan chan error , tpl []byte , templateFns template. FuncMap ) (uint64 , error ) {
5454 if totSize == 0 {
5555 return 0 , nil
5656 }
5757
5858 // Generate a single event to calculate the total number of events based on its size
5959 t := template .New ("estimate_tot_events" )
6060 t = t .Option ("missingkey=error" )
61-
62- templateFns := sprig .TxtFuncMap ()
63-
64- templateFns ["timeDuration" ] = func (duration int64 ) time.Duration {
65- return time .Duration (duration )
61+ tempTemplateFns := template.FuncMap {}
62+ for k , v := range templateFns {
63+ tempTemplateFns [k ] = v
6664 }
6765
68- templateFns ["generate" ] = func (field string ) any {
66+ tempTemplateFns ["generate" ] = func (field string ) any {
6967 state := NewGenState ()
7068 state .prevCacheForDup [field ] = make (map [any ]struct {})
7169 state .prevCacheCardinality [field ] = make ([]any , 0 )
@@ -127,21 +125,13 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields, totSize
127125
128126 errChan := make (chan error )
129127
130- totEvents , err := calculateTotEventsWithTextTemplate (totSize , fieldMap , errChan , tpl )
131- if err != nil {
132- return nil , err
133- }
134-
135- t := template .New ("generator" )
136- t = t .Option ("missingkey=error" )
137-
138128 templateFns := sprig .TxtFuncMap ()
139129
140130 templateFns ["timeDuration" ] = func (duration int64 ) time.Duration {
141131 return time .Duration (duration )
142132 }
143-
144- templateFns ["awsAZFromRegion" ] = func (region string ) string {
133+
134+ templateFns ["awsAZFromRegion" ] = func (region string ) string {
145135 azs , ok := awsAZs [region ]
146136 if ! ok {
147137 return "NoAZ"
@@ -160,6 +150,14 @@ func NewGeneratorWithTextTemplate(tpl []byte, cfg Config, fields Fields, totSize
160150 return bindF (state )
161151 }
162152
153+ totEvents , err := calculateTotEventsWithTextTemplate (totSize , fieldMap , errChan , tpl , templateFns )
154+ if err != nil {
155+ return nil , err
156+ }
157+
158+ t := template .New ("generator" )
159+ t = t .Option ("missingkey=error" )
160+
163161 parsedTpl , err := t .Funcs (templateFns ).Parse (string (tpl ))
164162 if err != nil {
165163 return nil , err
0 commit comments