Skip to content

Commit b7c26f0

Browse files
committed
review the proposal - interState data is now map[string]any instead of any which allows for more clarity and clear expectations
1 parent 1690c37 commit b7c26f0

3 files changed

Lines changed: 206 additions & 211 deletions

File tree

pkg/transform/engine/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ type Engine interface {
4444
type engineProtocol struct {
4545
FilterOut bool
4646
PartitionKey string
47-
Data any
47+
Data map[string]any
4848
HTTPHeaders map[string]string
4949
}

pkg/transform/engine/engine_javascript.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,25 @@ func (e *JSEngine) MakeFunction(funcName string) transform.TransformationFunctio
213213
}
214214

215215
// handling data
216-
switch protoData := protocol.Data.(type) {
217-
case string:
218-
message.Data = []byte(protoData)
219-
case map[string]any:
220-
216+
// protocol.Data is now always of type map[string]any, however
217+
// if the original input was an arbitrary string, we manually created map object,
218+
// and user want be expecting to data be transformed in this way, so we need to translate map object
219+
// back into string manually
220+
if strData, ok := protocol.Data["Data"]; ok {
221+
d, ok := strData.(string)
222+
if !ok {
223+
message.SetError(&models.TransformationError{
224+
SafeMessage: "failed to type assert data as string",
225+
})
226+
return nil, nil, message, nil
227+
}
228+
message.Data = []byte(d)
229+
} else {
221230
if e.RemoveNulls {
222-
protoData = transform.RemoveNullFromMap(protoData)
231+
protocol.Data = transform.RemoveNullFromMap(protocol.Data)
223232
}
224233
// encode
225-
encoded, err := json.Marshal(protoData)
234+
encoded, err := json.Marshal(protocol.Data)
226235
if err != nil {
227236
message.SetError(&models.TransformationError{
228237
SafeMessage: "error encoding message data",
@@ -231,11 +240,6 @@ func (e *JSEngine) MakeFunction(funcName string) transform.TransformationFunctio
231240
return nil, nil, message, nil
232241
}
233242
message.Data = encoded
234-
default:
235-
message.SetError(&models.TransformationError{
236-
SafeMessage: "invalid return type from JavaScript transformation; expected string or object",
237-
})
238-
return nil, nil, message, nil
239243
}
240244

241245
// setting pk if needed
@@ -327,11 +331,23 @@ func mkJSEngineInput(e *JSEngine, message *models.Message, interState any) (*eng
327331
}
328332

329333
candidate := &engineProtocol{
330-
Data: string(message.Data),
331334
HTTPHeaders: message.HTTPHeaders,
332335
}
333336

334337
if !e.SpMode {
338+
// For JS transformation we allow []byte input to be one of: [TSV string, Json string or arbitrary string]
339+
// At this stage input can either be Json string or arbitrary string,
340+
// so first try to JSON unmarshal it, but if fails, than it is a string and so manually create the input to JS
341+
var cInput map[string]any
342+
err := json.Unmarshal(message.Data, &cInput)
343+
if err == nil {
344+
candidate.Data = cInput
345+
return candidate, nil
346+
// return nil, fmt.Errorf("failed to transform input into JSON: %w", jErr)
347+
}
348+
cInput = make(map[string]any)
349+
cInput["Data"] = string(message.Data)
350+
candidate.Data = cInput
335351
return candidate, nil
336352
}
337353

0 commit comments

Comments
 (0)