Skip to content

Commit

Permalink
Expose fields in assembly (#30)
Browse files Browse the repository at this point in the history
* Expose fields in assembly

* Add documentation and tests

Co-authored-by: Marius <[email protected]>
  • Loading branch information
l4u and Acconut authored Dec 29, 2021
1 parent 9fd880b commit 89728b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
type Assembly struct {
// NotifiyURL specifies a URL to which a request will be sent once the
// assembly finishes.
// See https://transloadit.com/docs#notifications.
// See https://transloadit.com/docs#notifications
NotifyURL string
// TemplateID specifies a optional template from which the encoding
// instructions will be fetched.
// See https://transloadit.com/docs/#15-templates
TemplateID string
// Fields specifies additional key-value pairs that can be accessed by
// Assembly Instructions to allow customizing steps on a per-assembly basis.
// See https://transloadit.com/docs/#assembly-variables
Fields map[string]interface{}

steps map[string]map[string]interface{}
readers []*upload
Expand Down Expand Up @@ -139,6 +143,7 @@ type FileInfo struct {
// an assembly using Client.StartAssembly.
func NewAssembly() Assembly {
return Assembly{
Fields: make(map[string]interface{}),
steps: make(map[string]map[string]interface{}),
readers: make([]*upload, 0),
}
Expand Down Expand Up @@ -220,6 +225,10 @@ func (assembly *Assembly) makeRequest(ctx context.Context, client *Client) (*htt
options["steps"] = assembly.steps
}

if len(assembly.Fields) != 0 {
options["fields"] = assembly.Fields
}

if assembly.TemplateID != "" {
options["template_id"] = assembly.TemplateID
}
Expand Down
11 changes: 11 additions & 0 deletions assembly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestStartAssembly_Success(t *testing.T) {
})

assembly.NotifyURL = "https://example.com/"
assembly.Fields["string_test"] = "foo"
assembly.Fields["number_test"] = 100

info, err := client.StartAssembly(ctx, assembly)
if err != nil {
Expand All @@ -45,6 +47,15 @@ func TestStartAssembly_Success(t *testing.T) {
t.Fatal("wrong notify url")
}

if info.Fields["string_test"] != "foo" {
t.Fatal("wrong field string_test")
}

fmt.Printf("%#v\n", info.Fields)
if info.Fields["number_test"] != float64(100) {
t.Fatal("wrong field number_test")
}

if len(info.Uploads) != 2 {
t.Fatal("wrong number of uploads")
}
Expand Down

0 comments on commit 89728b2

Please sign in to comment.