Coder Social home page Coder Social logo

Comments (6)

ahamez avatar ahamez commented on May 31, 2024

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.

ananthakumaran avatar ananthakumaran commented on May 31, 2024

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.

ahamez avatar ahamez commented on May 31, 2024

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.

ananthakumaran avatar ananthakumaran commented on May 31, 2024

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.

ahamez avatar ahamez commented on May 31, 2024

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.

ananthakumaran avatar ananthakumaran commented on May 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.