Skip to content

Commit 4ade4ab

Browse files
authored
refactor data member of the InvokeMessage to be []byte (knative#2733)
Signed-off-by: KapilSareen <kapilsareen584@gmail.com>
1 parent 51a14cc commit 4ade4ab

File tree

3 files changed

+18
-28
lines changed

3 files changed

+18
-28
lines changed

cmd/invoke.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
172172
if err != nil {
173173
return err
174174
}
175-
m.Data = string(content)
175+
m.Data = content
176176
}
177177

178178
// Invoke
@@ -217,7 +217,7 @@ type invokeConfig struct {
217217
ID string
218218
Source string
219219
Type string
220-
Data string
220+
Data []byte
221221
ContentType string
222222
File string
223223
Confirm bool
@@ -233,7 +233,7 @@ func newInvokeConfig() (cfg invokeConfig, err error) {
233233
ID: viper.GetString("id"),
234234
Source: viper.GetString("source"),
235235
Type: viper.GetString("type"),
236-
Data: viper.GetString("data"),
236+
Data: []byte(viper.GetString("data")),
237237
ContentType: viper.GetString("content-type"),
238238
File: viper.GetString("file"),
239239
Confirm: viper.GetBool("confirm"),
@@ -247,7 +247,7 @@ func newInvokeConfig() (cfg invokeConfig, err error) {
247247
if err != nil {
248248
return cfg, err
249249
}
250-
cfg.Data = string(b)
250+
cfg.Data = b
251251
}
252252

253253
// if not in confirm/prompting mode, the cfg structure is complete.
@@ -368,7 +368,7 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
368368
Name: "Data",
369369
Prompt: &survey.Input{
370370
Message: "Data Content",
371-
Default: c.Data,
371+
Default: string(c.Data),
372372
},
373373
},
374374
}
@@ -389,7 +389,8 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
389389
Message: contentTypeMessage,
390390
Default: c.ContentType,
391391
},
392-
}}
392+
},
393+
}
393394
if err := survey.Ask(qs, &c); err != nil {
394395
return c, err
395396
}
@@ -401,7 +402,8 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
401402
Message: "Allow insecure server connections when using SSL",
402403
Default: c.Insecure,
403404
},
404-
}}
405+
},
406+
}
405407
if err := survey.Ask(qs, &c); err != nil {
406408
return c, err
407409
}

pkg/functions/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ func TestClient_Invoke_HTTP(t *testing.T) {
17101710
dataAsStr := string(data)
17111711

17121712
// Verify the body is correct
1713-
if dataAsStr != message.Data {
1713+
if dataAsStr != string(message.Data) {
17141714
t.Errorf("expected message data %q, got %q", message.Data, dataAsStr)
17151715
return
17161716
}

pkg/functions/invoke.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package functions
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"io"
@@ -30,8 +29,8 @@ type InvokeMessage struct {
3029
Source string
3130
Type string
3231
ContentType string
33-
Data string
34-
Format string //optional override for function-defined message format
32+
Data []byte
33+
Format string // optional override for function-defined message format
3534
}
3635

3736
// NewInvokeMessage creates a new InvokeMessage with fields populated
@@ -41,7 +40,7 @@ func NewInvokeMessage() InvokeMessage {
4140
Source: DefaultInvokeSource,
4241
Type: DefaultInvokeType,
4342
ContentType: DefaultInvokeContentType,
44-
Data: DefaultInvokeData,
43+
Data: []byte(DefaultInvokeData),
4544
// Format override not set by default: value from function being preferred.
4645
}
4746
}
@@ -50,7 +49,6 @@ func NewInvokeMessage() InvokeMessage {
5049
// invocation message. Returned is metadata (such as HTTP headers or
5150
// CloudEvent fields) and a stringified version of the payload.
5251
func invoke(ctx context.Context, c *Client, f Function, target string, m InvokeMessage, verbose bool) (metadata map[string][]string, body string, err error) {
53-
5452
// Get the first available route from 'local', 'remote', a named environment
5553
// or treat target
5654
route, err := invocationRoute(ctx, c, f, target) // choose instance to invoke
@@ -153,20 +151,10 @@ func sendEvent(ctx context.Context, route string, m InvokeMessage, t http.RoundT
153151
event.SetID(m.ID)
154152
event.SetSource(m.Source)
155153
event.SetType(m.Type)
156-
if m.ContentType == "application/json" {
157-
var d interface{}
158-
err = json.Unmarshal([]byte(m.Data), &d)
159-
if err != nil {
160-
return
161-
}
162-
err = event.SetData(m.ContentType, d)
163-
if err != nil {
164-
return
165-
}
166-
} else if err = event.SetData(m.ContentType, m.Data); err != nil {
167-
return
154+
err = event.SetData(m.ContentType, (m.Data))
155+
if err != nil {
156+
return "", fmt.Errorf("cannot set data: %w", err)
168157
}
169-
170158
c, err := cloudevents.NewClientHTTP(
171159
cloudevents.WithTarget(route),
172160
cloudevents.WithRoundTripper(t))
@@ -200,7 +188,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
200188
"Source": {m.Source},
201189
"Type": {m.Type},
202190
"ContentType": {m.ContentType},
203-
"Data": {m.Data},
191+
"Data": {string(m.Data)},
204192
}
205193
if verbose {
206194
fmt.Println("Sending values")
@@ -209,7 +197,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
209197
}
210198
}
211199

212-
req, err := http.NewRequestWithContext(ctx, "POST", route, bytes.NewBufferString(m.Data))
200+
req, err := http.NewRequestWithContext(ctx, "POST", route, bytes.NewReader(m.Data))
213201
if err != nil {
214202
return nil, "", fmt.Errorf("failure to create request: %w", err)
215203
}

0 commit comments

Comments
 (0)