TooTallNate / ref-struct

Create ABI-compliant "struct" instances on top of Buffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Struct should not rely on object key order

kmsquire opened this issue · comments

As noted here: http://stackoverflow.com/questions/9179680/is-it-acceptable-style-for-node-js-libraries-to-rely-on-object-key-order

This is somewhat pedantic, but the code will break if it's ever run on a javascript engine that does not keep track of key order.

Of course, after writing this, I'm not sure how the following would work:

var PasswordEntry = Struct({
    'username': 'string'
  , 'password': 'string'
  , 'salt':     int
})

I'd be open to an alternative syntax that's more future-proof, but I'm not overly worried about it since, as you've already pointed out, ref-struct only runs on node (though I could see that changing some day in the future).

Following in the footsteps of Python's OrderedDict, the keys would be specified as an array of arrays:

var PasswordEntry = Struct([
    [ "username", "string" ],
    [ "password", "string" ],
    [ "salt", "int" ]
]);

@parasyte +1, I love it :)

@parasyte @kmsquire So I actually forgot about it since there's no tests or examples, but there's actually a "legacy API" implemented in ref-struct. It's identical to @parasyte's proposed syntax, however, the type and name values are inverted. That is, you put the type first and the name second, kinda resembling actual C syntax.

I believe that this covers the needs of this issue enough that we can close. Cheers!

👍 Got it.

For anyone else who stumbles upon this ticket in the future:

ref-struct/lib/struct.js

Lines 131 to 136 in 19af92d

// legacy API
arg.forEach(function (a) {
var type = a[0]
var name = a[1]
StructType.defineProperty(name, type)
})

Why not to remove legacy API ? At least write to console deprecation warning for some time.

So, Please change example in README.md that will show NEW API (use Array instaed of Object), and, if you want, legacy API. I think legacy API should not be show in examples since it become legacy.

ARGHHH. you think that Array-API is alternative, not Main API. :( well, no changes is required so.

I don't think either one is legacy. I shouldn't have used that word before. They're just alternative syntaxes.

I'll take a pull request showing both styles in the README 😉