-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add third_party/collectorpayload module
- Loading branch information
Showing
9 changed files
with
1,054 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# collectorpayload | ||
|
||
This module is forked from the [Scala equivalent](https://github.com/snowplow/snowplow/tree/master/2-collectors/thrift-schemas/collector-payload-1). | ||
|
||
To generate the `gen-go/model1` directory: | ||
|
||
1. Install `thrift`: https://thrift.apache.org/download | ||
2. From this directory run `thrift -r --gen go collector_payload_1.thrift` | ||
|
||
_NOTE_: Running `make format` will fix formatting issues in the auto-generated code. | ||
|
||
To see how to use the library have a look at `collector_payload_test.go` which contains helper functions for seralizing and deserializing payloads. |
62 changes: 62 additions & 0 deletions
62
third_party/snowplow/collectorpayload/collector_payload.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. | ||
// | ||
// This program is licensed to you under the Snowplow Community License Version 1.0, | ||
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
|
||
package collectorpayload | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
|
||
thrift "github.com/apache/thrift/lib/go/thrift" | ||
|
||
model1 "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload/gen-go/model1" | ||
) | ||
|
||
// BinarySerializer serializes a CollectorPayload into a byte array ready for transport | ||
func BinarySerializer(ctx context.Context, collectorPayload *model1.CollectorPayload) ([]byte, error) { | ||
t := thrift.NewTMemoryBufferLen(1024) | ||
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t) | ||
|
||
serializer := &thrift.TSerializer{ | ||
Transport: t, | ||
Protocol: p, | ||
} | ||
|
||
return serializer.Write(ctx, collectorPayload) | ||
} | ||
|
||
// BinaryDeserializer deserializes a CollectorPayload byte array back to a struct | ||
func BinaryDeserializer(ctx context.Context, collectorPayloadBytes []byte) (*model1.CollectorPayload, error) { | ||
var inputBytes []byte | ||
|
||
// Attempt to decode from base64 as most payloads will arrive with the thrift string re-encoded | ||
base64DecodedCollectorPayload, base64Err := base64.StdEncoding.DecodeString(string(collectorPayloadBytes)) | ||
if base64Err != nil { | ||
inputBytes = collectorPayloadBytes | ||
} else { | ||
inputBytes = []byte(base64DecodedCollectorPayload) | ||
} | ||
|
||
t := thrift.NewTMemoryBufferLen(1024) | ||
p := thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(t) | ||
|
||
deserializer := &thrift.TDeserializer{ | ||
Transport: t, | ||
Protocol: p, | ||
} | ||
|
||
collectorPayload := model1.NewCollectorPayload() | ||
err := deserializer.Read(ctx, collectorPayload, inputBytes) | ||
|
||
return collectorPayload, err | ||
} | ||
|
||
// ToJSON converts the collector payload struct to a JSON representation for simpler portability | ||
func ToJSON(collectorPayload *model1.CollectorPayload) ([]byte, error) { | ||
return json.Marshal(collectorPayload) | ||
} |
33 changes: 33 additions & 0 deletions
33
third_party/snowplow/collectorpayload/collector_payload_1.thrift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. | ||
// | ||
// This program is licensed to you under the Snowplow Community License Version 1.0, | ||
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
|
||
namespace go model1 | ||
|
||
struct CollectorPayload { | ||
31337: string schema | ||
|
||
// Required fields which are intrinsic properties of HTTP | ||
100: string ipAddress | ||
|
||
// Required fields which are Snowplow-specific | ||
200: i64 timestamp | ||
210: string encoding | ||
220: string collector | ||
|
||
// Optional fields which are intrinsic properties of HTTP | ||
300: optional string userAgent | ||
310: optional string refererUri | ||
320: optional string path | ||
330: optional string querystring | ||
340: optional string body | ||
350: optional list<string> headers | ||
360: optional string contentType | ||
|
||
// Optional fields which are Snowplow-specific | ||
400: optional string hostname | ||
410: optional string networkUserId | ||
} |
36 changes: 36 additions & 0 deletions
36
third_party/snowplow/collectorpayload/collector_payload_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. | ||
// | ||
// This program is licensed to you under the Snowplow Community License Version 1.0, | ||
// and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
// You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
|
||
package collectorpayload | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
model1 "github.com/snowplow/snowbridge/third_party/snowplow/collectorpayload/gen-go/model1" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestBinarySerializer | ||
func TestBinarySerializerAndDeserializer(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
assert := assert.New(t) | ||
|
||
payload := model1.NewCollectorPayload() | ||
payload.IpAddress = "192.168.0.1" | ||
|
||
res, err := BinarySerializer(ctx, payload) | ||
assert.Nil(err) | ||
assert.NotNil(res) | ||
|
||
res1, err1 := BinaryDeserializer(ctx, res) | ||
assert.Nil(err1) | ||
assert.NotNil(res1) | ||
assert.Equal("192.168.0.1", res1.IpAddress) | ||
} |
5 changes: 5 additions & 0 deletions
5
third_party/snowplow/collectorpayload/gen-go/model1/GoUnusedProtection__.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
third_party/snowplow/collectorpayload/gen-go/model1/collector_payload_1-consts.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.