Skip to content

Commit 732843a

Browse files
author
Pavel Petroshenko
authored
Merge pull request #4 from mm-gmbd/master
Support custom data encoder for Record
2 parents 8094636 + 11cf819 commit 732843a

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

Diff for: .imptest

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"modelId": "bNA7KXCkI9jB" /* sender */,
2+
"modelId": "Xj-yNZibO5M-" /* M */,
33
"devices": [
4-
"234776801163a9ee" /* 234776801163a9ee */
4+
"500098f1706fd382" /* M */
55
],
66
"agentFile": "AWSKinesisStreams.agent.lib.nut",
77
"deviceFile": false,

Diff for: AWSKinesisStreams.agent.lib.nut

+8-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const _AWS_KINESIS_STREAMS_SERVICE = "kinesis";
5353
const _AWS_KINESIS_STREAMS_TARGET_PREFIX = "Kinesis_20131202";
5454

5555
class AWSKinesisStreams {
56-
static VERSION = "1.0.0";
56+
static VERSION = "1.1.0";
5757

5858
// Enables/disables the library debug output (including errors logging).
5959
// Disabled by default.
@@ -717,6 +717,7 @@ class AWSKinesisStreams.Record {
717717
_error = null;
718718
_explicitHashKey = null;
719719
_prevSequenceNumber = null;
720+
_encoder = null;
720721

721722
// Creates and returns AWSKinesisStreams.Record object that can be written into an
722723
// Amazon Kinesis stream using AWSKinesisStreams.Producer methods.
@@ -733,15 +734,19 @@ class AWSKinesisStreams.Record {
733734
// prevSequenceNumber : See http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-SequenceNumberForOrdering
734735
// string (optional)
735736
//
737+
// encoder : a custom JSON encoder function for encoding the provided data (e.g. [JSONEncoder.encode](https://github.com/electricimp/JSONEncoder))
738+
// function (otpional)
739+
//
736740
// Returns: AWSKinesisStreams.Record object that can be
737741
// written into the Amazon Kinesis stream using
738742
// AWSKinesisStreams.Producer putRecord/putRecords
739743
// methods.
740-
constructor(data, partitionKey, explicitHashKey = null, prevSequenceNumber = null) {
744+
constructor(data, partitionKey, explicitHashKey = null, prevSequenceNumber = null, encoder = null) {
741745
this.data = data;
742746
this.partitionKey = partitionKey;
743747
_explicitHashKey = explicitHashKey;
744748
_prevSequenceNumber = prevSequenceNumber;
749+
_encoder = encoder == null ? http.jsonencode.bindenv(http) : encoder;
745750
}
746751

747752
// -------------------- PRIVATE METHODS -------------------- //
@@ -750,7 +755,7 @@ class AWSKinesisStreams.Record {
750755
_error = AWSKinesisStreams._utils._validateNonEmpty(partitionKey, "partitionKey");
751756
if (!_error) {
752757
local result = {
753-
"Data" : http.base64encode(typeof data == "blob" ? data.tostring() : http.jsonencode(data)),
758+
"Data" : http.base64encode(typeof data == "blob" ? data.tostring() : _encoder(data)),
754759
"PartitionKey" : partitionKey,
755760
};
756761
if (_explicitHashKey) {

Diff for: Examples/DataConsumer.agent.nut

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// OTHER DEALINGS IN THE SOFTWARE.
2424

2525
#require "AWSRequestV4.class.nut:1.0.2"
26-
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
26+
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
2727

2828
// AWSKinesisStreams.Consumer library sample.
2929
// Periodically reads new data records from all shards of the specified preconfigured

Diff for: Examples/DataProducer.agent.nut

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// OTHER DEALINGS IN THE SOFTWARE.
2424

2525
#require "AWSRequestV4.class.nut:1.0.2"
26-
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
26+
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
2727

2828
// AWSKinesisStreams.Producer library sample.
2929
// Periodically writes data records to the specified preconfigured AWS Kinesis Stream

Diff for: README.md

+26-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The AWSKinesisStreams library utilizes the [AWSRequestV4](https://github.com/ele
1313

1414
```squirrel
1515
#require "AWSRequestV4.class.nut:1.0.2"
16-
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
16+
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
1717
```
1818

1919
## Library Usage ##
@@ -68,7 +68,7 @@ This class represents an AWS Kinesis Streams record: a combination of data attri
6868
| *timestamp* | Integer | The approximate time that the record was inserted into the stream. In number of seconds since Unix epoch (midnight, 1 Jan 1970) |
6969
| *encryptionType* | [AWS_KINESIS_STREAMS_ENCRYPTION_TYPE](#aws_kinesis_streams_encryption_type-enum) | The encryption type used on the record |
7070

71-
### Constructor: AWSKinesisStreams.Record(*data, partitionKey[, explicitHashKey][, prevSequenceNumber]*) ###
71+
### Constructor: AWSKinesisStreams.Record(*data, partitionKey[, explicitHashKey][, prevSequenceNumber][, encoder]*) ###
7272

7373
This method creates and returns an AWSKinesisStreams.Record object that can be written into an Amazon Kinesis stream using [AWSKinesisStreams.Producer](#awskinesisstreamsproducer-class) methods.
7474

@@ -78,6 +78,7 @@ This method creates and returns an AWSKinesisStreams.Record object that can be w
7878
| *partitionKey* | String | Yes | Identifies which shard in the stream the data record is assigned to (see the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_Record.html#Streams-Type-Record-PartitionKey)) |
7979
| *explicitHashKey* | String | Optional | The hash value used to explicitly determine the shard the data record is assigned to by overriding the partition key hash (see the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-ExplicitHashKey)) |
8080
| *prevSequenceNumber* | String | Optional | See the [Kinesis Streams documentation](http://docs.aws.amazon.com/kinesis/latest/APIReference/API_PutRecord.html#Streams-PutRecord-request-SequenceNumberForOrdering) |
81+
| *encoder* | Function | Optional | A custom JSON encoder function for encoding the provided data (e.g. [JSONEncoder.encode](https://github.com/electricimp/JSONEncoder)) |
8182

8283
## AWS_KINESIS_STREAMS_ENCRYPTION_TYPE Enum ##
8384

@@ -268,7 +269,26 @@ A type of Squirrel data which can be encoded/decoded into/from JSON, eg. table,
268269

269270
```squirrel
270271
#require "AWSRequestV4.class.nut:1.0.2"
271-
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
272+
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
273+
#require "JSONEncoder.class.nut:2.0.0"
274+
275+
//This class can be used to hold numbers larger than Squirrel can natively support (i.e. anything larger than 32-bit)
276+
//and then be encoded as a number (rather than a string) when encoded with `JSONEncoder.encode`.
277+
class JSONLiteralString {
278+
_string = null;
279+
280+
constructor (string) {
281+
_string = string.tostring();
282+
}
283+
284+
function _serializeRaw() {
285+
return _string;
286+
}
287+
288+
function toString() {
289+
return _string;
290+
}
291+
}
272292
273293
// Substitute with real values
274294
const AWS_KINESIS_REGION = "<YOUR_AWS_REGION>";
@@ -292,7 +312,8 @@ producer.putRecord(AWSKinesisStreams.Record("Hello!", "partitionKey"), function
292312
records <- [
293313
AWSKinesisStreams.Record("test", "partitionKey1"),
294314
AWSKinesisStreams.Record(12345, "partitionKey2"),
295-
AWSKinesisStreams.Record({ "temperature" : 21, "humidity" : 60 }, "partitionKey3")
315+
AWSKinesisStreams.Record({ "temperature" : 21, "humidity" : 60 }, "partitionKey3"),
316+
AWSKinesisStreams.Record({ "a" : JSONLiteralString("123456789123456789") }, "partitionKey4", null, null, JSONEncoder.encode.bindenv(JSONEncoder)) //write record using custom encoder
296317
];
297318
298319
producer.putRecords(records, function (error, failedRecordCount, putResults) {
@@ -315,7 +336,7 @@ producer.putRecords(records, function (error, failedRecordCount, putResults) {
315336

316337
```squirrel
317338
#require "AWSRequestV4.class.nut:1.0.2"
318-
#require "AWSKinesisStreams.agent.lib.nut:1.0.0"
339+
#require "AWSKinesisStreams.agent.lib.nut:1.1.0"
319340
320341
// Substitute with real values
321342
const AWS_KINESIS_REGION = "<YOUR_AWS_REGION>";

Diff for: tests/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ The tests for AWSKinesisStreams library require pre-setup described below.
2929
- Click **Policies** in the left menu.
3030
- Click **Create policy**.
3131
- Click **Select** next to **Policy Generator**.
32-
- Choose **Amazon Kinesis** as the **AWS service**.
33-
- Choose **All Actions** in the **Actions** field.
34-
- Enter your **Stream ARN** in the **Amazon Resource Name (ARN)** field.
35-
- Click **Add Statement**.
36-
- Click **Next Step**.
32+
- Choose **Kinesis** as the **Service**.
33+
- Choose **All Kinesis Actions** in the **Actions** field.
34+
- Click **Resources**.
35+
- Click **Add ARN to restrict access**.
36+
- Enter your **Stream ARN** in the **Specify ARN for stream** field.
37+
- Click **Add**.
38+
- Click **Review policy**.
3739
- Change **Policy Name** to `imptestStreamPolicy`.
3840
- Click **Create Policy**.
3941
- Click **Users** in the left menu.

0 commit comments

Comments
 (0)