Disallow creating a struct from other modules.
defmodule Mystruct do
use ProtectedStruct
# for warning instead of failing:
# use ProtectedStruct, on_violation: :warn
defstruct [:name, :age]
def new(name, age) when is_binary(name) and is_integer(age) do
%__MODULE__{name: name, age: age}
end
endTrying to create %MyStruct{} from other modules will result in a compilation error:
defmodule StructUser do
def create_new_mystruct do
%MyStruct{}
end
end
# ** (ProtectedStruct.Error) %Mystruct{} can only be created inside its own module
# (myproj 0.1.0) expanding struct: Mystruct.__struct__/1
# struct_user.ex:3: (file)The package can be installed by adding the following to your mix.exs:
protected_structto list of dependenciesProtectedStruct.Tracerto list of Elixir tracers
# mix.exs
def project do
[
elixirc_options: [tracers: [ProtectedStruct.Tracer]],
]
end
def deps do
[
{:protected_struct, "~> 0.1.0"}
]
endDocumentation can be found at https://hexdocs.pm/protected_struct.