Comments (9)
I've got a working solution for the decoding part (I still need to figure how to handle the encoding part):
iex(1)> defmodule MyModule do
...(1)> use Protox,
...(1)> schema: """
...(1)> syntax = "proto2";
...(1)>
...(1)> message SessionCommand {
...(1)> enum SessionCommandType {
...(1)> PING = 1000;
...(1)> }
...(1)> extensions 100 to max;
...(1)> }
...(1)>
...(1)> message Command_Ping {
...(1)> extend SessionCommand {
...(1)> optional Command_Ping ext = 1000;
...(1)> }
...(1)> }
...(1)> """
...(1)> end
...
iex(2)> SessionCommand.decode!(<<194, 62, 0>>)
%SessionCommand{__uf__: [], ext: {Command_Ping, %Command_Ping{__uf__: []}}}
iex(3)>
Note that now ext
is set to the tuple {Command_Ping, %Command_Ping{...}}
. Even though it may seem redundant, it's required as nested extensions can declare fields with the same type, thus we need a way to disambiguate from which extension the field comes from. For instance, we can have the following proto schema:
syntax = "proto2";
message Extendee {
extensions 100 to max;
}
message Extension1 {
extend Extendee {
optional int32 ext = 101;
}
}
message Extension2 {
extend Extendee {
optional int32 ext = 102;
}
}
So, to sum up: use the first field of the tuple to get the type of the extension field :-).
I'll address the encoding part as soon as possible!
from protox.
it looks like you're using a different definition for Command_Ping than i am:
// mine, not working
message Command_Ping {
extend SessionCommand {
optional Command_Ping ext = 1000;
}
}
// yours, working
message Command_Ping {
}
extend SessionCommand {
optional Command_Ping ext = 1000;
}
from protox.
OK, thanks for the feedback! If it works as is, I suggest you keep using this branch while I come up with a better solution. It might involve some API breaking, so I want to make sure I've got everything right.
from protox.
Hello, I have to think more about this, but I'm not sure it's feasible
from protox.
Hello again,
I can't reproduce your problem:
iex(1)> defmodule MyModule do
...(1)> use Protox, schema: """
...(1)> syntax = "proto2";
...(1)>
...(1)> message SessionCommand {
...(1)> enum SessionCommandType {
...(1)> PING = 1000;
...(1)> // etc
...(1)> }
...(1)> extensions 100 to max;
...(1)> }
...(1)>
...(1)> message Command_Ping {
...(1)> }
...(1)>
...(1)> extend SessionCommand {
...(1)> optional Command_Ping ext = 1000;
...(1)> }
...(1)> """
...(1)> end
...
iex(2)> SessionCommand.decode!(<<194, 62, 0>>)
%SessionCommand{ext: %Command_Ping{__uf__: []}, __uf__: []}
Command_Ping
is directly accessible in the ext
field.
So I'm not sure where the problem is?
from protox.
i did a bit of testing, and it seems like the issue has to do with how code is generated when the definition is nested like i have it, since it affects encoding too
running this:
SessionCommand.encode!(%SessionCommand{ext: %Command_Ping{}}) |> IO.iodata_to_binary()
returns <<194, 62, 0>>
when run with the non-nested definition, but the nested definition raises with
** (KeyError) key :ext not found
expanding struct: SessionCommand.__struct__/1
iex:6: (file)
(elixir 1.14.2) expanding macro: Kernel.|>/2
iex:6: (file)
In order to get the same output with the nested definition, i had to run
Command_Ping.encode!(%Command_Ping{ext: %Command_Ping{}}) |> IO.iodata_to_binary()
If you need it, the full set of proto defs i am working with is here: https://github.com/Cockatrice/Cockatrice/tree/master/common/pb
The Command_Ping
example is from here
from protox.
OK, thanks, with all this information it will be easier to pinpoint the problem! I'll try to come up with a solution as soon as I can.
from protox.
I think I've fixed this. Could you try with the branch nested_extensions
to see if it works with your use case?
from protox.
Yes, it seems to be working correctly!
The only issue i have found is it currently (identically) defines encode_ext/2
, default(:ext)
, field_def("ext")
and field_def(:ext)
on the base message once for each extension, and duplicates the call to encode_ext(msg)
for each extension.
from protox.
Related Issues (20)
- Generating code for services? HOT 7
- Supporting FileOptions HOT 9
- key not found when encoding optional fields HOT 10
- Feature request: ability to ignore unknown fields and not have __uf__ properties in structs HOT 6
- Not seeing mix tasks HOT 6
- protox and the `package` directive (was: Proposal - Support Namespace definition when use files generation) HOT 4
- [BUG] Fix compile warning when use --keep-unknown-fields=false HOT 1
- Fails to generate module HOT 1
- Enable manual import path specification HOT 2
- Proto3 Field Presence HOT 3
- feature request: ability to inject definition into existing module HOT 6
- protox generates nested lists (was: Strange behavior when encoding complex nested struct) HOT 2
- [BUG] README.md contains misleading examples HOT 1
- [BUG] Compilation of Protox 1.6 fails HOT 6
- Inconsistency in generated functions specs? HOT 1
- [QUESTION] Enums implementation HOT 9
- [Feature Request] support for decimal 1.9.x HOT 4
- [BUG] The code generated is not deterministic HOT 6
- [BUG] HOT 4
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 protox.