Replies: 1 comment 5 replies
-
Thanks for the feedback. We are discussing some of these ideas and built a small prototype based on them. They look really nice. I also created #461 to use I think the annotations around field behavior are less promising. Too many fields are "required if you are doing X, but optional (or must not be set) if doing Y". Therefore, the annotations are at best incomplete, and sometimes misleading. Furthermore, my experience from other languages is that they are troublesome, e.g. immutable and output-only fields really need setters in mocks. I think less is better when it comes to using these annotations. |
Beta Was this translation helpful? Give feedback.
-
This may be very premature, but since early is the best time to plant opinions about long-term API decisions, I'd like to make some suggestions on the request API 😄
Correct requests by design
There is enough information in Google protobuf APIs to do significantly better than basic protobuf for creating the request RPCs. The most important here is
FieldBehavior
annotations, which is already mentioned as a priority in #27The obvious possibilities are:
Option
These would make the client API less confusing, and allow compiler errors to guide a code writer to a correct request.
A struct?
These possibilities can all be encoded by an input-specific struct. If one elides output-only values, defines required values as required values rather than options, and use
{ ..Default::default() }
when defining the structure, it mostly matches the abilities above.There are a number of downsides that come to mind:
A builder API?
A builder API over the current struct definition can also encode these possibilities - some with a bit more work than others.
Into
. This also makes it reasonable to use more efficient types likeCow<'static, str>
for stringsimpl IntoIterator
forVec
,HashMap
, etc.I'm personally a fan of the builder API idea, especially one that encodes the invariants mentioned above. In private code, I've used this approach with some success (but manually adding the code based on annotations rather than it being done automatically).
The library I've used is https://bon-rs.com - which gives (IMO) a very nice API around struct builders, allowing all the aims mentioned above to be fulfilled.
As you are generating code, you don't necessarily need all the features of bon, e.g. it's a general system which does a lot of work in its builder macros, whereas you already know in the builder, what the shape of the API is going to be. The compile-time type invariants also add to compile time.
But still, I think its builder API is probably the nicest I've seen in the rust ecosystem, and it's worth looking at for ideas.
Hope this is useful feedback - and good luck with the project here in general!
Beta Was this translation helpful? Give feedback.
All reactions