refactor data member of the InvokeMessage to be []byte (#2733)

Signed-off-by: KapilSareen <kapilsareen584@gmail.com>
This commit is contained in:
Kapil Sareen 2025-03-06 19:15:00 +05:30 committed by GitHub
parent 51a14ccf23
commit 4ade4ab3d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 28 deletions

View File

@ -172,7 +172,7 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err
if err != nil { if err != nil {
return err return err
} }
m.Data = string(content) m.Data = content
} }
// Invoke // Invoke
@ -217,7 +217,7 @@ type invokeConfig struct {
ID string ID string
Source string Source string
Type string Type string
Data string Data []byte
ContentType string ContentType string
File string File string
Confirm bool Confirm bool
@ -233,7 +233,7 @@ func newInvokeConfig() (cfg invokeConfig, err error) {
ID: viper.GetString("id"), ID: viper.GetString("id"),
Source: viper.GetString("source"), Source: viper.GetString("source"),
Type: viper.GetString("type"), Type: viper.GetString("type"),
Data: viper.GetString("data"), Data: []byte(viper.GetString("data")),
ContentType: viper.GetString("content-type"), ContentType: viper.GetString("content-type"),
File: viper.GetString("file"), File: viper.GetString("file"),
Confirm: viper.GetBool("confirm"), Confirm: viper.GetBool("confirm"),
@ -247,7 +247,7 @@ func newInvokeConfig() (cfg invokeConfig, err error) {
if err != nil { if err != nil {
return cfg, err return cfg, err
} }
cfg.Data = string(b) cfg.Data = b
} }
// if not in confirm/prompting mode, the cfg structure is complete. // if not in confirm/prompting mode, the cfg structure is complete.
@ -368,7 +368,7 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
Name: "Data", Name: "Data",
Prompt: &survey.Input{ Prompt: &survey.Input{
Message: "Data Content", Message: "Data Content",
Default: c.Data, Default: string(c.Data),
}, },
}, },
} }
@ -389,7 +389,8 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
Message: contentTypeMessage, Message: contentTypeMessage,
Default: c.ContentType, Default: c.ContentType,
}, },
}} },
}
if err := survey.Ask(qs, &c); err != nil { if err := survey.Ask(qs, &c); err != nil {
return c, err return c, err
} }
@ -401,7 +402,8 @@ func (c invokeConfig) prompt() (invokeConfig, error) {
Message: "Allow insecure server connections when using SSL", Message: "Allow insecure server connections when using SSL",
Default: c.Insecure, Default: c.Insecure,
}, },
}} },
}
if err := survey.Ask(qs, &c); err != nil { if err := survey.Ask(qs, &c); err != nil {
return c, err return c, err
} }

View File

@ -1710,7 +1710,7 @@ func TestClient_Invoke_HTTP(t *testing.T) {
dataAsStr := string(data) dataAsStr := string(data)
// Verify the body is correct // Verify the body is correct
if dataAsStr != message.Data { if dataAsStr != string(message.Data) {
t.Errorf("expected message data %q, got %q", message.Data, dataAsStr) t.Errorf("expected message data %q, got %q", message.Data, dataAsStr)
return return
} }

View File

@ -3,7 +3,6 @@ package functions
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -30,8 +29,8 @@ type InvokeMessage struct {
Source string Source string
Type string Type string
ContentType string ContentType string
Data string Data []byte
Format string //optional override for function-defined message format Format string // optional override for function-defined message format
} }
// NewInvokeMessage creates a new InvokeMessage with fields populated // NewInvokeMessage creates a new InvokeMessage with fields populated
@ -41,7 +40,7 @@ func NewInvokeMessage() InvokeMessage {
Source: DefaultInvokeSource, Source: DefaultInvokeSource,
Type: DefaultInvokeType, Type: DefaultInvokeType,
ContentType: DefaultInvokeContentType, ContentType: DefaultInvokeContentType,
Data: DefaultInvokeData, Data: []byte(DefaultInvokeData),
// Format override not set by default: value from function being preferred. // Format override not set by default: value from function being preferred.
} }
} }
@ -50,7 +49,6 @@ func NewInvokeMessage() InvokeMessage {
// invocation message. Returned is metadata (such as HTTP headers or // invocation message. Returned is metadata (such as HTTP headers or
// CloudEvent fields) and a stringified version of the payload. // CloudEvent fields) and a stringified version of the payload.
func invoke(ctx context.Context, c *Client, f Function, target string, m InvokeMessage, verbose bool) (metadata map[string][]string, body string, err error) { func invoke(ctx context.Context, c *Client, f Function, target string, m InvokeMessage, verbose bool) (metadata map[string][]string, body string, err error) {
// Get the first available route from 'local', 'remote', a named environment // Get the first available route from 'local', 'remote', a named environment
// or treat target // or treat target
route, err := invocationRoute(ctx, c, f, target) // choose instance to invoke 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
event.SetID(m.ID) event.SetID(m.ID)
event.SetSource(m.Source) event.SetSource(m.Source)
event.SetType(m.Type) event.SetType(m.Type)
if m.ContentType == "application/json" { err = event.SetData(m.ContentType, (m.Data))
var d interface{} if err != nil {
err = json.Unmarshal([]byte(m.Data), &d) return "", fmt.Errorf("cannot set data: %w", err)
if err != nil {
return
}
err = event.SetData(m.ContentType, d)
if err != nil {
return
}
} else if err = event.SetData(m.ContentType, m.Data); err != nil {
return
} }
c, err := cloudevents.NewClientHTTP( c, err := cloudevents.NewClientHTTP(
cloudevents.WithTarget(route), cloudevents.WithTarget(route),
cloudevents.WithRoundTripper(t)) cloudevents.WithRoundTripper(t))
@ -200,7 +188,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
"Source": {m.Source}, "Source": {m.Source},
"Type": {m.Type}, "Type": {m.Type},
"ContentType": {m.ContentType}, "ContentType": {m.ContentType},
"Data": {m.Data}, "Data": {string(m.Data)},
} }
if verbose { if verbose {
fmt.Println("Sending values") fmt.Println("Sending values")
@ -209,7 +197,7 @@ func sendPost(ctx context.Context, route string, m InvokeMessage, t http.RoundTr
} }
} }
req, err := http.NewRequestWithContext(ctx, "POST", route, bytes.NewBufferString(m.Data)) req, err := http.NewRequestWithContext(ctx, "POST", route, bytes.NewReader(m.Data))
if err != nil { if err != nil {
return nil, "", fmt.Errorf("failure to create request: %w", err) return nil, "", fmt.Errorf("failure to create request: %w", err)
} }