Comments (6)
Protox provides the namespace
option:
defmodule Bar do
use Protox, schema: """
syntax = "proto3";
enum Enum {
FOO = 0;
BAR = 1;
}
""",
namespace: Namespace
end
Isn't it what you're looking for?
from protox.
no, the namespace will create a nested module
defmodule Bar
defmodule Namespace do
defstruct [..]
end
end
inject directly injects the defstruct
into the calling module.
something like
defmodule Enum do
use Protox, schema: """
syntax = "proto3";
enum Enum {
FOO = 0;
BAR = 1;
}
""",
inject: true
end
it will inject the defstruct and other methods directly into the calling module without creating nested module using defmodule
from protox.
Actually, the following code
defmodule Dummy do
use Protox, schema: """
syntax = "proto3";
message Foo {
int32 a = 1;
}
""",
namespace: Namespace
end
produces
defmodule Namespace do
defmodule Foo do
defstruct [...]
end
end
Thus, Dummy
is completely ignored (I should make it clear in the documentation) and Foo
is "injected" into Namespace
.
If you don't use the namespace
option, then the code is directly "injected" in the global namespace and you would end up with:
defmodule Foo do
defstruct [...]
end
Does it answer your question?
from protox.
Let me try with an example. For the schema
message Foo {
int32 a = 1;
}
What I want is the ability to add extra code inside the generated Foo module.
defmodule Foo do
# added by protox
defstruct [...]
def encode(.)
def decode(.)
# added by user
def bar() do
end
end
with the proposed inject future, I would be able to do
defmodule Foo do
use Protox, schema: "..", inject: true
# added by user
def bar() do
end
end
I would be able to call Foo.bar()
. From user perspective, the defstruct
is injected by the protox. If protox always generates defmodule Foo
, there is no way for a user to add extra functions to the generated module.
from protox.
OK, I understand now. To be honest, I discarded this possibility right from the beginning as I felt it was a lot of work just to avoid writing a new module:
defmodule Dummy do
use Protox, schema: """
syntax = "proto3";
message Foo {
int32 a = 1;
}
"""
end
defmodule WorkWithFoo do
def bar() do
end
end
Also, I don't like what it implies in terms of renaming messages names:
defmodule Bar do
use Protox, schema: """
syntax = "proto3";
message Foo {
int32 a = 1;
}
""",
inject: true
end
Suddenly, Foo
becomes Bar
just because it has been "injected" into Bar
. Someone looking at the protobuf definition files won't find the messages he's looking for.
I realise it's more or less the debate of "inheritance vs composition". I usually strongly favour the latest.
That being said, I'll welcome any PR that brings this functionality :-)
from protox.
Those are good points, I am also not sure how well it would work with Enums, Nested msgs etc.
But for some use cases, API wise, it would be ideal to add extra methods to the generated module instead of using a different module, otherwise one would have to come up with a new name for the same thing.
defmodule Friends.Person do
use Ecto.Schema
schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end
def changeset(person, params \\ %{}) do
person
|> Ecto.Changeset.cast(params, ~w(first_name last_name age))
|> Ecto.Changeset.validate_required([:first_name, :last_name])
end
end
Here for example, casting/validation logic are kept together
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] support `extend` when nested inside a message definition HOT 9
- [Feature request] Define spec for structs HOT 1
- 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
- Enable manual import path specification HOT 2
- Proto3 Field Presence HOT 3
- 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.