Skip to content

Commit

Permalink
rename type struct and add parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nammn committed Oct 25, 2020
1 parent a7e59dd commit 49e807e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions runner/call_template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))

// call template data
type callTemplateData struct {
type callData struct {
WorkerID string // unique worker ID
RequestNumber int64 // unique incremented request number for each request
FullyQualifiedName string // fully-qualified name of the method call
Expand All @@ -42,11 +42,11 @@ var tmplFuncMap = template.FuncMap{
"randomString": randomString,
}

// newCallTemplateData returns new call template data
func newCallTemplateData(
// newCallData returns new callData
func newCallData(
mtd *desc.MethodDescriptor,
funcs template.FuncMap,
workerID string, reqNum int64) *callTemplateData {
workerID string, reqNum int64) *callData {
now := time.Now()
newUUID, _ := uuid.NewRandom()

Expand All @@ -61,7 +61,7 @@ func newCallTemplateData(
}
}

return &callTemplateData{
return &callData{
WorkerID: workerID,
RequestNumber: reqNum,
FullyQualifiedName: mtd.GetFullyQualifiedName(),
Expand All @@ -80,14 +80,14 @@ func newCallTemplateData(
}
}

func (td *callTemplateData) execute(data string) (*bytes.Buffer, error) {
func (td *callData) execute(data string) (*bytes.Buffer, error) {
t := template.Must(template.New("call_template_data").Funcs(td.templateFuncs).Parse(data))
var tpl bytes.Buffer
err := t.Execute(&tpl, td)
return &tpl, err
}

func (td *callTemplateData) executeData(data string) ([]byte, error) {
func (td *callData) executeData(data string) ([]byte, error) {
if len(data) > 0 {
input := []byte(data)
tpl, err := td.execute(data)
Expand All @@ -101,7 +101,7 @@ func (td *callTemplateData) executeData(data string) ([]byte, error) {
return []byte{}, nil
}

func (td *callTemplateData) executeMetadata(metadata string) (map[string]string, error) {
func (td *callData) executeMetadata(metadata string) (map[string]string, error) {
var mdMap map[string]string

if len(metadata) > 0 {
Expand Down
10 changes: 5 additions & 5 deletions runner/call_template_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestCallTemplateData_New(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, md)

ctd := newCallTemplateData(md, nil, "worker_id_123", 100)
ctd := newCallData(md, nil, "worker_id_123", 100)

assert.NotNil(t, ctd)
assert.Equal(t, "worker_id_123", ctd.WorkerID)
Expand All @@ -41,7 +41,7 @@ func TestCallTemplateData_ExecuteData(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, md)

ctd := newCallTemplateData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, nil, "worker_id_123", 200)

assert.NotNil(t, ctd)

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCallTemplateData_ExecuteMetadata(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, md)

ctd := newCallTemplateData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, nil, "worker_id_123", 200)

assert.NotNil(t, ctd)

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, md)

ctd := newCallTemplateData(md, nil, "worker_id_123", 200)
ctd := newCallData(md, nil, "worker_id_123", 200)

assert.NotNil(t, ctd)

Expand Down Expand Up @@ -249,7 +249,7 @@ func TestCallTemplateData_ExecuteFuncs(t *testing.T) {
})

t.Run("custom functions", func(t *testing.T) {
ctd = newCallTemplateData(md, template.FuncMap{
ctd = newCallData(md, template.FuncMap{
"getSKU": func() string {
return "custom-sku"
},
Expand Down
6 changes: 3 additions & 3 deletions runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type RunConfig struct {
data []byte

// data func
dataFunc func(mtd *desc.MethodDescriptor) []byte
dataFunc func(mtd *desc.MethodDescriptor, callData *callData) []byte

binary bool
metadata []byte
Expand Down Expand Up @@ -307,7 +307,7 @@ func WithBinaryData(data []byte) Option {

// WithBinaryDataFunc specifies the binary data func which will be called on each request
// WithBinaryDataFunc(changeFunc)
func WithBinaryDataFunc(data func(mtd *desc.MethodDescriptor) []byte) Option {
func WithBinaryDataFunc(data func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte) Option {
return func(o *RunConfig) error {
o.dataFunc = data
o.binary = true
Expand Down Expand Up @@ -573,7 +573,7 @@ func WithLogger(log Logger) Option {
}
}

// WithTemplateFuncs adds additional tempalte functions
// WithTemplateFuncs adds additional template functions
func WithTemplateFuncs(funcMap template.FuncMap) Option {
return func(o *RunConfig) error {
o.funcs = funcMap
Expand Down
4 changes: 2 additions & 2 deletions runner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (w *Worker) makeRequest() error {

reqNum := atomic.AddInt64(w.reqCounter, 1)

ctd := newCallTemplateData(w.mtd, w.config.funcs, w.workerID, reqNum)
ctd := newCallData(w.mtd, w.config.funcs, w.workerID, reqNum)

var inputs []*dynamic.Message
var err error
Expand Down Expand Up @@ -167,7 +167,7 @@ func (w *Worker) makeRequest() error {
return err
}

func (w *Worker) getMessages(ctd *callTemplateData, inputData []byte) ([]*dynamic.Message, error) {
func (w *Worker) getMessages(ctd *callData, inputData []byte) ([]*dynamic.Message, error) {
var inputs []*dynamic.Message

if w.cachedMessages != nil {
Expand Down

0 comments on commit 49e807e

Please sign in to comment.