|
| 1 | +// |
| 2 | +// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. |
| 3 | +// |
| 4 | +// This program is licensed to you under the Snowplow Community License Version 1.0, |
| 5 | +// and you may not use this file except in compliance with the Snowplow Community License Version 1.0. |
| 6 | +// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 |
| 7 | + |
| 8 | +package transform |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "encoding/json" |
| 13 | + "errors" |
| 14 | + |
| 15 | + "github.com/snowplow/snowbridge/config" |
| 16 | + "github.com/snowplow/snowbridge/pkg/models" |
| 17 | + |
| 18 | + collectorpayload "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload" |
| 19 | + collectorpayloadmodel1 "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload/gen-go/model1" |
| 20 | +) |
| 21 | + |
| 22 | +// JSONToCollectorPayloadThriftConfig is a configuration object for the spJSONToCollectorPayloadThrift transformation |
| 23 | +type JSONToCollectorPayloadThriftConfig struct { |
| 24 | +} |
| 25 | + |
| 26 | +type jsonToCollectorPayloadThriftAdapter func(i interface{}) (interface{}, error) |
| 27 | + |
| 28 | +// Create implements the ComponentCreator interface. |
| 29 | +func (f jsonToCollectorPayloadThriftAdapter) Create(i interface{}) (interface{}, error) { |
| 30 | + return f(i) |
| 31 | +} |
| 32 | + |
| 33 | +// ProvideDefault implements the ComponentConfigurable interface |
| 34 | +func (f jsonToCollectorPayloadThriftAdapter) ProvideDefault() (interface{}, error) { |
| 35 | + // Provide defaults |
| 36 | + cfg := &JSONToCollectorPayloadThriftConfig{} |
| 37 | + |
| 38 | + return cfg, nil |
| 39 | +} |
| 40 | + |
| 41 | +// adapterGenerator returns a spJSONToCollectorPayloadThrift transformation adapter. |
| 42 | +func jsonToCollectorPayloadThriftAdapterGenerator(f func(c *JSONToCollectorPayloadThriftConfig) (TransformationFunction, error)) jsonToCollectorPayloadThriftAdapter { |
| 43 | + return func(i interface{}) (interface{}, error) { |
| 44 | + cfg, ok := i.(*JSONToCollectorPayloadThriftConfig) |
| 45 | + if !ok { |
| 46 | + return nil, errors.New("invalid input, expected jsonToCollectorPayloadThriftConfig") |
| 47 | + } |
| 48 | + |
| 49 | + return f(cfg) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// jsonToCollectorPayloadThriftConfigFunction returns an spJSONToCollectorPayloadThrift transformation function, from an jsonToCollectorPayloadThriftConfig. |
| 54 | +func jsonToCollectorPayloadThriftConfigFunction(c *JSONToCollectorPayloadThriftConfig) (TransformationFunction, error) { |
| 55 | + return SpJSONToCollectorPayloadThrift, nil |
| 56 | +} |
| 57 | + |
| 58 | +// JSONToCollectorPayloadThriftConfigPair is a configuration pair for the spJSONToCollectorPayloadThrift transformation |
| 59 | +var JSONToCollectorPayloadThriftConfigPair = config.ConfigurationPair{ |
| 60 | + Name: "spJSONToCollectorPayloadThrift", |
| 61 | + Handle: jsonToCollectorPayloadThriftAdapterGenerator(jsonToCollectorPayloadThriftConfigFunction), |
| 62 | +} |
| 63 | + |
| 64 | +// SpJSONToCollectorPayloadThrift is a specific transformation implementation to transform a raw message into a valid Thrift encoded Collector Payload |
| 65 | +// so that it can be pushed directly into the egress stream of a Collector. |
| 66 | +func SpJSONToCollectorPayloadThrift(message *models.Message, intermediateState interface{}) (*models.Message, *models.Message, *models.Message, interface{}) { |
| 67 | + var p *collectorpayloadmodel1.CollectorPayload |
| 68 | + unmarshallErr := json.Unmarshal(message.Data, &p) |
| 69 | + if unmarshallErr != nil { |
| 70 | + message.SetError(unmarshallErr) |
| 71 | + return nil, nil, message, nil |
| 72 | + } |
| 73 | + |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + res, serializeErr := collectorpayload.BinarySerializer(ctx, p) |
| 77 | + if serializeErr != nil { |
| 78 | + message.SetError(serializeErr) |
| 79 | + return nil, nil, message, nil |
| 80 | + } |
| 81 | + |
| 82 | + message.Data = res |
| 83 | + return message, nil, nil, intermediateState |
| 84 | +} |
0 commit comments