Skip to content

kzemek/protected_struct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ProtectedStruct

Disallow creating a struct from other modules.

Usage

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
end

Trying 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)

Installation

The package can be installed by adding the following to your mix.exs:

  1. protected_struct to list of dependencies
  2. ProtectedStruct.Tracer to list of Elixir tracers
# mix.exs

def project do
  [
    elixirc_options: [tracers: [ProtectedStruct.Tracer]],
  ]
end

def deps do
  [
    {:protected_struct, "~> 0.1.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/protected_struct.

About

Protect Elixir struct creation outside of its module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages