Skip to content

Commit

Permalink
[breaking] Pass decodedPayload to Decoder.decode method (#243)
Browse files Browse the repository at this point in the history
Instantiate the `DecoderPayload` and pass it directly to the `Decoder.decode` method.

Before
```
  async decode(
    payload: JSONObject,
  ): Promise<DecodedPayload<DummyTempDecoder>> {
    const decodedPayload = new DecodedPayload<DummyTempDecoder>(this);
    // decode measurements..
    return decodedPayload;
  }
```

After
```
  async decode(
    decodedPayload: DecodedPayload<DummyTempDecoder>,
    payload: JSONObject,
  ): Promise<DecodedPayload<DummyTempDecoder>> {
    // decode measurements..
    return decodedPayload;
  }
```
  • Loading branch information
Adrien Maret committed Dec 19, 2022
1 parent 1253222 commit fda20f1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
7 changes: 4 additions & 3 deletions features/fixtures/application/decoders/DummyTempDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export class DummyTempDecoder extends Decoder {
return true;
}

async decode(payload: JSONObject): Promise<DecodedPayload<Decoder>> {
const decodedPayload = new DecodedPayload<DummyTempDecoder>(this);

async decode(
decodedPayload: DecodedPayload<DummyTempDecoder>,
payload: JSONObject,
): Promise<DecodedPayload<DummyTempDecoder>> {
if (payload?.metadata?.color) {
decodedPayload.addMetadata(payload.deviceEUI, {
color: payload.metadata.color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export class DummyTempPositionDecoder extends Decoder {
return true;
}

async decode(payload: JSONObject): Promise<DecodedPayload<Decoder>> {
const decodedPayload = new DecodedPayload<DummyTempPositionDecoder>(this);

async decode(
decodedPayload: DecodedPayload<DummyTempPositionDecoder>,
payload: JSONObject,
): Promise<DecodedPayload<DummyTempPositionDecoder>> {
decodedPayload.addMeasurement<TemperatureMeasurement>(
payload.deviceEUI,
"temperature",
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/decoder/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ export abstract class Decoder {
*/
// eslint-disable-next-line no-unused-vars
abstract decode(
decodedPayload: DecodedPayload<any>,
payload: JSONObject,
request: KuzzleRequest
): Promise<DecodedPayload<Decoder>>;
): Promise<DecodedPayload<any>>;

/**
* Checks if the provided properties are present in the payload
Expand Down
5 changes: 4 additions & 1 deletion lib/modules/decoder/PayloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../../core";
import { DeviceContent, DeviceSerializer } from "../device";
import { EventMeasureIngest } from "../measure";
import { DecodedPayload } from "./DecodedPayload";

import { Decoder } from "./Decoder";

Expand Down Expand Up @@ -63,7 +64,9 @@ export class PayloadService {
await this.savePayload(decoder.deviceModel, uuid, valid, payload);
}

const decodedPayload = await decoder.decode(payload, request);
let decodedPayload = new DecodedPayload<any>(decoder);

decodedPayload = await decoder.decode(decodedPayload, payload, request);

const devices = await this.retrieveDevices(
decoder.deviceModel,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-device-manager",
"version": "2.0.0-rc33",
"version": "2.0.0-rc34",
"description": "Manage your IoT devices and assets. Choose a provisioning strategy, receive and decode payload, handle your IoT business logic.",
"author": "The Kuzzle Team (support@kuzzle.io)",
"repository": {
Expand Down

0 comments on commit fda20f1

Please sign in to comment.