Skip to content

Introduce deviceOSProtobuf.getDefinition(name) to device-os-protobuf #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Jan 20, 2022

Conversation

joegoggins
Copy link
Contributor

@joegoggins joegoggins commented Jan 16, 2022

Overview

Introduces main public API methods for this module:

  • encode(protobufMessageName, {data: "foo"})
  • decode(protobufMessageName, buffer)
  • getDefinition(protobufMessageName)
  • getDefinitions() / getNamespaces()

Story details: https://app.shortcut.com/particle/story/95530

How to test?

  1. git checkout sc-95530/introduce-getDefinition-function
  2. npm i
  3. npm run test:ci; observe linter, tests, and coverage stats.
  4. Read the README.md; note anywhere things could be written more clearly.
  5. Read the rendered docs/reference.md; ask questions if anything isn't clear.

Joe Goggins added 26 commits January 15, 2022 19:56
… also:

- Adds `lodash` to dependencies
- Drops `mkdirp` from dependencies
This is dynamically using pbjs generated JSON to get the request ID
rather than an additional static mapping outside of *.proto files
…NumberRequest") instead of "SERIAL_NUMBER"

Intent behind "SERIAL_NUMBER" was ambiguous, it could be the request message OR the reply message.
Instead of introducing some kind of clever mapping we'll build the API this way
Uses similar approach to particle-usb (documentation module)

We ignore protobufjs generated documentation for now because it is HUGE
and potentially not useful
We use a typedef to describe the object that getDefinition returns
This commit introduces lots of other test coverage against real world
Device OS messages
@joegoggins joegoggins marked this pull request as ready for review January 19, 2022 21:17
@joegoggins
Copy link
Contributor Author

While critically reviewing this PR myself, I decided to read the entire README of the protobufjs to understand if we are using this module correctly.

One aspect of our technical approach is odd.

We use both npm build / pbjs -o static-module (for generated js code) and npm build:json / pbjs -o json (for the mapping to request id). This choice was made without considering the protobuf.load("awesome.proto".../reflection option which can reflect on *.proto files at runtime rather than via pre-generated code. If this worked, that approach would eliminate these 2 build steps.

I'm not proposing we pursue that now because I think this PR is a good step forward. But wanted to raise the option in case others wanted to chime in.

Copy link
Member

@monkbroc monkbroc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good at a glance. I haven't run the library directly.

@monkbroc
Copy link
Member

We use both npm build / pbjs -o static-module (for generated js code) and npm build:json / pbjs -o json

I'd say run both variants of the pbjs command with one npm run build

@joegoggins
Copy link
Contributor Author

joegoggins commented Jan 19, 2022

Cool. Thanks for the feedback @monkbroc , I've pushed commits that address all of your comments/questions.

@sergeuz I'm looking forward to your feedback as well.

One thing I forgot to mention earlier was this snippet of code inside particle-usb (see experimental branch) that demonstrates how low level API (sendProtobufRequest) vs high level API (getSerialNumber) will operate with this getDefinition() construct from the new sendProtobufRequest method we'll introduce there. I'm into it, that PR review should be coming along shortly.

async function main() {
    const particleUSB = require('./src/particle-usb');
    const devices = await particleUSB.getDevices();
    const device = devices[0];
    await device.open();
    const serialNumberHighLevelAPI = await device.getSerialNumber();
    console.log('serialNumberHighLevelAPI', serialNumberHighLevelAPI);

    const serialNumberLowLevelAPI = await device.sendProtobufRequest(
        'GetSerialNumberRequest',
        null,
        {timeout: 7000}
    )
    console.log('serialNumberLowLevelAPI', serialNumberLowLevelAPI.serial);

    await device.close();
}

main();

@joegoggins joegoggins force-pushed the sc-95530/introduce-getDefinition-function branch from 3b423cc to 0a47605 Compare January 19, 2022 23:31
"scripts": {
"build": "mkdirp dist && pbjs control/*.proto -t static-module -o dist/index.js --no-delimited --no-convert --no-verify",
"docs": "jsdoc -X dist/index.js > dist/index-docs.json",
"build": "npm run build:pbjs:module && npm run build:pbjs:json && npm run build:docs",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

Copy link
Member

@monkbroc monkbroc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now! :shipit:

Joe Goggins added 5 commits January 20, 2022 14:54
…reate, encode, decode methods

It used to only support a string; this simplifies upstream usage in particle-usb
@joegoggins joegoggins merged commit 359df26 into main Jan 20, 2022
@joegoggins joegoggins deleted the sc-95530/introduce-getDefinition-function branch January 20, 2022 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants