Fix json oneof shadowing#163
Conversation
Signed-off-by: inge4pres <fgualazzi@gmail.com>
Signed-off-by: inge4pres <fgualazzi@gmail.com>
There was a problem hiding this comment.
Woudln't it be possible to have the options exposed in the protobuf api?
i think that we could have something like:
generated file:
pub fn jsonEncode(
self: @This(),
stdJsonOptions: std.json.Stringify.Options,
protobufJsonOptions : protobuf.json.Options,
allocator: std.mem.Allocator,
) ![]const u8 {
return protobuf.json.encode(self, stdJsonOptions, protobufJsonOptions, allocator);
}json.zig:
pub fn encode(
data: anytype,
stdJsonOptions: std.json.Stringify.Options,
protobufJsonOptions : protobuf.json.Options,
allocator: std.mem.Allocator,
) ![]u8 {
const Wrapper = struct {
data : @TypeOf(data),
pbJsonOptions : protobuf.json.Options,
pub fn jsonStringify (self: *const @This(), jws: anytype) !void {
return protobuf.json.stringify(@This(), self, jws, self.pbJsonOptions);
}
}
return std.json.Stringify.valueAlloc(allocator, Wrapper{.data = data, .pbJsonOptions = pbJsonOptions}, stdJsonOptions);
}
pub fn stringify(Self: type, self: *const Self, jws: anytype, opts: ?Options) !void {
const data = self.data;
const pbJsonOptions = self.pbJsonOptions;
// work with that
}This might even allow us to remove the jsonStringify from the generated structs.
I'm not sure if this works, this is just a theory. What do you think @inge4pres ?
|
@Arwalk that seems reasonable, and I think it's a great idea! Because right now using I noticed there is additional logic that there is in |
|
I'm ok with breaking changes in the JSON interfaces. As stated in the README, it's still considered beta. Still, we'll increase the major version accordingly (as it will be a non-retro-compatible interface change and we're doing our best to stay true to semantic versioning) Thanks again @inge4pres ! |
@Arwalk nice thanks, LGTM I will proceed if you can confirm my assumption in the previous comment
|
Sorry i wasn't clear on that. Yes, you can go ahead. Thanks again for everything. |
This is a breaking change! json.encode has a new signature accepting decode options and supersedes the existing json.stringify API. Signed-off-by: inge4pres <fgualazzi@gmail.com>
Signed-off-by: inge4pres <fgualazzi@gmail.com>
|
It's done! 🚀 This is a breaking change so mind it when merging, a new major version upgrade will be needed. |
Arwalk
left a comment
There was a problem hiding this comment.
Minor changes on the tests but overall this is a great job.
Signed-off-by: inge4pres <fgualazzi@gmail.com>
Feedback incorporated 🚀 |
|
Really cool work. I really like how passing custom options is handled here. I hope i'll find the time to make a blog post or something for other people that'd like to have custom informations in their json encoding. Thanks @inge4pres , i'll do a release soonish |
Reason for this PR
Closes #147
Supersedes: #148
We want to give users ability to discard the nested container value in JSON encoding/decoding.
Note for reviewers: don't get scared by the large diff, reviewing by commit should be doable. The second commit has an update to the exiting API that cascades to all generated files.
Let me know if you prefer to keep the second commit or if we should split the API adding a "WithOptions" public variant.
Details
Split by commit:
Optionsstruct to configure the encoding/decoding behaviorjson.stringifyAPI to add an optional parameter passing user-provided Options (mouthful not intended 😅)