Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

"github.com/yarpc/yab/encoding"
"github.com/yarpc/yab/internal/yamlalias"
"github.com/yarpc/yab/templateargs"

Expand Down Expand Up @@ -56,6 +57,8 @@ type template struct {
Request map[interface{}]interface{} `yaml:"request"`
Requests []map[interface{}]interface{} `yaml:"requests"`
Timeout time.Duration `yaml:"timeout"`
File string `yaml:"file"`
Encoding string `yaml:"encoding"`
}

func readYAMLFile(yamlTemplate string, templateArgs map[string]string, opts *Options) error {
Expand Down Expand Up @@ -129,6 +132,13 @@ func readYAMLRequest(base string, contents []byte, templateArgs map[string]strin
return err
}

if t.File != "" {
body, err = ioutil.ReadFile(t.File)
if err != nil {
return err
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should prevent users of using file and request together. Right now, File have priority over request, only one should be available

if t.Peer != "" {
opts.TOpts.Peers = []string{t.Peer}
opts.TOpts.PeerList = ""
Expand Down Expand Up @@ -160,6 +170,17 @@ func readYAMLRequest(base string, contents []byte, templateArgs map[string]strin
opts.ROpts.ThriftFile = thriftFileURL.Path
}

switch strings.ToLower(t.Encoding) {
case "thrift":
opts.ROpts.Encoding = encoding.Thrift
case "json":
opts.ROpts.Encoding = encoding.JSON
case "protobuf":
opts.ROpts.Encoding = encoding.Protobuf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the option -e, --encoding, encoding.Protobuf will match with proto and not protobuf.

case "raw":
opts.ROpts.Encoding = encoding.Raw
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 169, there is the following line of code overrideParam(&opts.ROpts.RequestJSON, string(body)). How would it work when the body given in file has another format than JSON? Right now, body could be with format proto, thrift or raw.

overrideParam(&opts.TOpts.CallerName, t.Caller)
overrideParam(&opts.TOpts.ServiceName, t.Service)
overrideParam(&opts.ROpts.Procedure, t.Procedure)
Expand Down
6 changes: 6 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,9 @@ func TestReadYAMLRequestFails(t *testing.T) {
})
}
}

func TestReadYAMLSucceeds(t *testing.T) {
opts := newOptions()
err := readYAMLFile("testdata/valid.yab", nil, opts)
assert.NoError(t, err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more test please? We should provide the following unit tests:

  • Yab template with file + JSON format
  • Yab template with file + YAML format
  • Yab template with file + RAW format
  • Yab template with file + Proto format
  • Yab template with file + Thrift format
  • Negative test with bad combinaison of options (like File and request together)