Comments (11)
If there isn't a bug that I don't know of, then for a field named "id" there should are already be these three options:
- getId() / setId(...)
- get_id() / set_id(...)
- direct access to
id
from protobuf.js.
I meant multi-word field names, such as something_id
.
from protobuf.js.
So, if you name it "somethingId", you should be able to access it through:
- getSomethingId() ...
- get_somethingId()
- and of course direct access to
somethingId
And if you name it something_id:
- getSomethingId()
- get_something_id()
- direct access to
something_id
Or am I getting something wrong?
Relevant part: https://github.com/dcodeIO/ProtoBuf.js/blob/master/src/ProtoBuf/Reflect.js#L450
from protobuf.js.
Protobuf fields normally use camel_case, as per the convention, and the .proto file might be coming from an external source. What I'm suggesting is being able to access a something_id
field using somethingId
(and construct a respective Message using a key named as such).
from protobuf.js.
I see. The current way it is done is through the getters and setters that are available both in camelCase as well as in under_score notation. The field itself is accessed through the exact naming and at least I am feeling comfortable with that, for the following reason: The problem here is if let's say you have an external proto file that doesn't stick to the convention and uses both some_thing and someThing (for whatever reason) as two seperate fields, then it would still be possible to access these fields properly through direct access while getters and setters, which are an optional convenience addition, would refer to the first field only. Imho this is the most straight forward way to handle this exact case.
from protobuf.js.
Such a .proto file would break the official Java implementation. In the very unlikely event that someone encounters this, they can resort to editing their copy of the file.
from protobuf.js.
Or here's an alternative approach. You could store field values in "private" properties, e.g. _some_thing
and _someThing
(preferably defined with Object.defineProperty to make them non-enumerable). That way, both of them would be accessible with (get|set)_some_thing and (get|set)_someThing methods respectively. someThing
and some_thing
would both be accessor properties to _some_thing
.
from protobuf.js.
I am sorry but I am still not sure if it's worth the trouble of breaking compatibility eventually...
from protobuf.js.
I'm not sure what you mean by breaking compatibility. If you mean compatibility with previous ProtoBuf.js versions, then it would break only in the following two cases:
- Someone has a .proto file with a message that defines both some_thing and someThing and accesses the fields using
some_thing
andsomeThing
properties respectively. With my original suggestion, they would need to modify their .proto file, but with the alternative approach, they would have to change their code to access the latter field using get_someThing and set_someThing. Chances that this will affect any current ProtoBuf.js users are virtually none. - Someone has a .proto file that defines any camelCase fields (say, someThing) and accesses them using get_ and set_ methods, which currently convert the field name into snake_case (i.e. get_some_thing and set_some_thing). If you go with the alternative approach, they would need to use either (get|set)someThing, or access it using the property (
someThing
), since the get and set_ methods would need to preserve the field's actual name. Again, very unlikely to affect anyone, because: 1) I've never seen a .proto file with camelCase fields and 2) why anyone would access said fields using snake_case methods is beyond me.
And if you mean compatibility with other Protobuf libraries (excluding chrisdew's protobuf and the official Java implementation), then that would be preserved with the alternative approach.
from protobuf.js.
Hmm... sorry, but I am not convinced. If you actually prefer namings in camal case for your self, you should change the .proto files instead. We shouldn't break compatibility for a convenience function like that and we already have both getters and setters around.
What you however could do would be to implement an alternative constructor, a constructor flag that converts camel cased object keys into the matching underscore representation explicitly or a global property on the protobuf namespace, i.e. by specifying a field like "$convertFieldsToUnderscore": true
or a property ProtoBuf.convertFieldsToUnderscore = true
and I could incorporate it into the master branch.
See: https://github.com/dcodeIO/ProtoBuf.js/blob/master/src/ProtoBuf/Reflect.js#L374
from protobuf.js.
How about a ProtoBuf.convertFieldsToCamelCase
property instead, that would do both what you have suggested and what I had proposed?
from protobuf.js.
Related Issues (20)
- Protobuf sources can't contain doxygen tags
- Parsing camelCase and snake_case JSON objects into protobuf.js messages HOT 1
- I found that @protobufjs/utf8 on npm or yarn is still the old code version. HOT 1
- "Invalid wire type n" "index out of range: n + 1 > n" HOT 5
- Is 6.11.4 an official release? HOT 2
- --target static-module --es6 --wrap es6 produces a code that fails to run HOT 3
- npm ERR! The "file" argument must be of type string
- When sending a payload I'm getting trailing NULLs
- not throw Error: illegal token ';' when loadSync with about 30+ proto files
- Bug in convert Root to object with solution
- camelCasing failing when key includes numbers HOT 1
- Cesium is not building due to protobufjs error HOT 2
- Project health? HOT 3
- Enumeration of OneOf field
- Reader.read_uint32 causes reader head misalignment when consuming oversized VARINTs in some cases
- not provide an export named 'load'
- Will support HarmonyOS (ArtTS)in the future?
- Generated proto doesn't use class inheritance
- order of imports matters? HOT 1
- Decode delimited is getting the message length wrong
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf.js.