-
Notifications
You must be signed in to change notification settings - Fork 60
Support multiple --input-meta flags for the same key #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7eeb15e
a5ec0c1
6fdb55f
c57cd50
3c99dca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -623,13 +623,23 @@ func (p *PayloadInputOptions) buildRawInputPayloads() (*common.Payloads, error) | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Build metadata | ||||||||||||||||||||||||||
| metadata := map[string][]byte{"encoding": []byte("json/plain")} | ||||||||||||||||||||||||||
| metadata := map[string][][]byte{} | ||||||||||||||||||||||||||
| for _, meta := range p.InputMeta { | ||||||||||||||||||||||||||
| metaPieces := strings.SplitN(meta, "=", 2) | ||||||||||||||||||||||||||
| if len(metaPieces) != 2 { | ||||||||||||||||||||||||||
| return nil, fmt.Errorf("metadata %v expected to have '='", meta) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| metadata[metaPieces[0]] = []byte(metaPieces[1]) | ||||||||||||||||||||||||||
| if vals, ok := metadata[metaPieces[0]]; ok { | ||||||||||||||||||||||||||
| if len(vals) == len(inData) { | ||||||||||||||||||||||||||
| return nil, fmt.Errorf("received more --input-meta flags for key %q than number of inputs", metaPieces[0]) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| metadata[metaPieces[0]] = append(vals, []byte(metaPieces[1])) | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| metadata[metaPieces[0]] = [][]byte{[]byte(metaPieces[1])} | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+632
to
+639
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Pedantic, but for a map of slices, it's a bit more idiomatic Go to append to the nil result of the absent key rather than have conditionals (can use intermediate vars if you are concerned with the accesses) |
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| if _, ok := metadata["encoding"]; !ok { | ||||||||||||||||||||||||||
| metadata["encoding"] = [][]byte{[]byte("json/plain")} | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return CreatePayloads(inData, metadata, p.InputBase64) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,12 +9,25 @@ import ( | |
| "go.temporal.io/api/common/v1" | ||
| ) | ||
|
|
||
| func CreatePayloads(data [][]byte, metadata map[string][]byte, isBase64 bool) (*common.Payloads, error) { | ||
| // CreatePayloads creates API Payload objects from given data and metadata slices. | ||
| // If metadata has an entry at a data index, it is used, otherwise it uses the metadata entry at index 0. | ||
| func CreatePayloads(data [][]byte, metadata map[string][][]byte, isBase64 bool) (*common.Payloads, error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little confusing following this code. May need some godoc on this method explaining this Not saying this is wrong necessarily, just confusing and worth documenting somewhere clearly so it is clear that the logic below is by intention and what callers should expect. Maybe just toss a "If metadata has an entry at a data index, it is used, otherwise it uses the metadata entry at index 0" above the function would be enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like a comment was made as a result of this comment (it's a bit hard to know without an update to this thread). |
||
| ret := &common.Payloads{Payloads: make([]*common.Payload, len(data))} | ||
| for i, in := range data { | ||
| // If it's JSON, validate it | ||
| if strings.HasPrefix(string(metadata["encoding"]), "json/") && !json.Valid(in) { | ||
| return nil, fmt.Errorf("input #%v is not valid JSON", i+1) | ||
| var metadataForIndex = make(map[string][]byte, len(metadata)) | ||
| for k, vals := range metadata { | ||
| if len(vals) == 0 { | ||
| continue | ||
| } | ||
| v := vals[0] | ||
| if len(vals) > i { | ||
| v = vals[i] | ||
| } | ||
| // If it's JSON, validate it | ||
| if k == "encoding" && strings.HasPrefix(string(v), "json/") && !json.Valid(in) { | ||
| return nil, fmt.Errorf("input #%v is not valid JSON", i+1) | ||
| } | ||
| metadataForIndex[k] = v | ||
| } | ||
| // Decode base64 if base64'd (std encoding only for now) | ||
| if isBase64 { | ||
|
|
@@ -23,7 +36,7 @@ func CreatePayloads(data [][]byte, metadata map[string][]byte, isBase64 bool) (* | |
| return nil, fmt.Errorf("input #%v is not valid base64", i+1) | ||
| } | ||
| } | ||
| ret.Payloads[i] = &common.Payload{Data: in, Metadata: metadata} | ||
| ret.Payloads[i] = &common.Payload{Data: in, Metadata: metadataForIndex} | ||
| } | ||
| return ret, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.