### Install Nix Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Install Nix package manager by downloading and running the installation script. ```bash curl https://nixos.org/nix/install | sh ``` -------------------------------- ### Start Nix Shell Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Manually start a Nix shell environment for the project if direnv is not used. ```bash nix-shell ``` -------------------------------- ### Run Git Setup Script Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Execute the provided script to set up git-flow for the project. ```bash ./.gitsetup ``` -------------------------------- ### Install direnv Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Install direnv, a tool that automatically loads/unloads environment variables per directory. ```bash nix-env -i direnv ``` -------------------------------- ### Fetch Project Dependencies Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Download and install all project dependencies using Mix, Elixir's build tool. ```bash mix deps.get ``` -------------------------------- ### Start Feature Branch with Git Flow Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Use git-flow to start a new feature branch, following established branching conventions. ```bash git flow feature start ``` -------------------------------- ### TypedStruct Elixir Struct Definition Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md This example demonstrates the simplified struct definition using the TypedStruct library. It uses a `typedstruct` block to define fields, their types, default values, and enforcement status in a single place. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ use TypedStruct typedstruct do @typedoc "A person" field :name, String.t(), enforce: true field :age, non_neg_integer() field :happy?, boolean(), default: true field :phone, String.t() end end ``` -------------------------------- ### Run Static Analyzers Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Execute static analysis tools and run all project tests using Mix. ```bash mix check ``` -------------------------------- ### Allow direnv in Project Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Allow direnv to manage the environment for the current project directory. ```bash direnv allow ``` -------------------------------- ### Checkout Develop Branch Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Switch your local repository to the 'develop' branch, which is used for ongoing development. ```bash git checkout develop ``` -------------------------------- ### Create New Feature Branch Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Create a new branch for a small patch or a new feature. ```bash git checkout -b ``` -------------------------------- ### Using TypedStructLens Plugin Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Demonstrates how to use the TypedStructLens plugin to automatically generate lenses for struct fields. The `a_field/0` function is generated by the plugin. ```elixir defmodule MyStruct do use TypedStruct typedstruct do plugin TypedStructLens field :a_field, String.t() field :other_field, atom() end @spec change(t()) :: t() def change(data) do # a_field/0 is generated by TypedStructLens. lens = a_field() put_in(data, [lens], "Changed") end end ``` -------------------------------- ### Clone TypedStruct Repository Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Clone the TypedStruct repository from GitHub to your local machine. ```bash git clone https://github.com/you/typed_struct.git cd typed_struct ``` -------------------------------- ### Configure direnv hook Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Add the direnv hook to your shell's rc file to enable automatic environment loading. ```sh eval "$(direnv hook )" ``` -------------------------------- ### Configure Mix Formatter for TypedStruct Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Add this to your `.formatter.exs` to prevent `mix format` from adding parentheses on field definitions. ```elixir [..., import_deps: [:typed_struct] ] ``` -------------------------------- ### Rebase Feature Branch onto Develop Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Integrate the latest changes from the develop branch into your feature branch by rebasing. ```bash git checkout git rebase develop ``` -------------------------------- ### Add Upstream Remote Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Add the main TypedStruct repository as a remote named 'upstream' to your local clone. ```bash git remote add upstream https://github.com/ejpcmac/typed_struct.git ``` -------------------------------- ### Commit Changes Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Stage and commit changes to your feature branch. Conventional Commits naming is recommended. ```bash git commit -am "feat: my first change" git commit -am "refactor: my second change" ``` -------------------------------- ### Add TypedStruct to Mix Dependencies Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Add this to your Mix dependencies to use TypedStruct. Consider adding `runtime: false` if not compiling modules with TypedStruct at runtime. ```elixir {:typed_struct, "~> 0.3.0"} ``` -------------------------------- ### Update Develop Branch Source: https://github.com/ejpcmac/typed_struct/blob/main/CONTRIBUTING.md Fetch all upstream changes and rebase the local develop branch onto the latest upstream develop branch. ```bash git checkout develop git fetch --all --prune git rebase upstream/develop ``` -------------------------------- ### Add Documentation to Submodule Struct Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Document submodules containing typed structs using `@moduledoc` and `@typedoc` within the `typedstruct` block. ```elixir typedstruct module: MyStruct do @moduledoc "A submodule with a typed struct." @typedoc "A typed struct in a submodule" field :a_string, String.t() field :an_int, integer() end ``` -------------------------------- ### Defining a TypedStruct Field with Enforcement Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Shows how the `enforce: true` option adds the field to `@enforce_keys`, making it a required key. ```elixir field :name, String.t(), enforce: true ``` -------------------------------- ### Defining a TypedStruct Field with Default Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Demonstrates defining a field with a type and a default value. The `defstruct` will include the default, and the type will not be nullable. ```elixir defmodule Example do use TypedStruct typedstruct do field :name, String.t() end end ``` -------------------------------- ### Define a Basic Typed Struct Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Use `TypedStruct` and the `typedstruct` macro to define a struct with typed fields. Fields are defined using the `field/2` macro. ```elixir defmodule MyStruct do use TypedStruct typedstruct do field :a_string, String.t() field :string_with_default, String.t(), default: "default" field :enforced_field, integer(), enforce: true end end ``` -------------------------------- ### Defining a TypedStruct Field with Default Value Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Illustrates how the `default` option for a field sets the default value in the `defstruct`. ```elixir field :name, String.t(), default: "John Smith" ``` -------------------------------- ### Defining an Empty TypedStruct Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Shows how to define an empty `typedstruct` block, resulting in an empty struct with a `t()` type alias. ```elixir defmodule Example do use TypedStruct typedstruct do end end ``` -------------------------------- ### TypedStruct with Opaque Type Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Demonstrates using `opaque: true` within `typedstruct` to generate an `@opaque` type specification instead of `@type`. ```elixir typedstruct opaque: true do field :name, String.t() end ``` -------------------------------- ### Typed Struct with Default Enforcement Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Configure `typedstruct` to enforce all keys by default. Individual fields can override this behavior. ```elixir defmodule MyStruct do use TypedStruct typedstruct enforce: true do field :enforced_by_default, term() field :not_enforced, term(), enforce: false field :not_enforced_either, integer(), default: 1 end end ``` -------------------------------- ### Generated Empty Struct and Type Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Illustrates the Elixir code generated for an empty `typedstruct` block, including `defstruct` and the `t()` type specification. ```elixir defmodule Example do @enforce_keys [] defstruct [] @type t() :: %__MODULE__{} end ``` -------------------------------- ### Generated Struct within Nested Module Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Displays the Elixir code generated when `module: Struct` is used, showing the struct definition nested within `MyModule.Struct`. ```elixir defmodule MyModule do defmodule Struct do @enforce_keys [] defstruct field: nil @type t() :: %__MODULE__{ field: term() | nil } end end ``` -------------------------------- ### Generated Struct with Default Field Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Shows the generated Elixir code for a struct with a field that has a default value. The `defstruct` includes the default, and the type is not nullable. ```elixir defmodule Example do @enforce_keys [] defstruct name: nil @type t() :: %__MODULE__{ name: String.t() | nil } end ``` -------------------------------- ### Typed Struct with Opaque Type Generation Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Generate an opaque type for the struct by setting `opaque: true` in the `typedstruct` macro. ```elixir defmodule MyOpaqueStruct do use TypedStruct typedstruct opaque: true do field :name, String.t() end end ``` -------------------------------- ### Generated Opaque Type Specification Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Shows the resulting Elixir code when `opaque: true` is used, generating an `@opaque` type for the struct. ```elixir @opaque t() :: %__MODULE__{ name: String.t() } ``` -------------------------------- ### Generated Struct with Enforced Field Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Displays the generated Elixir code when a field is enforced. The field is added to `@enforce_keys`, and the type is not nullable. ```elixir defmodule Example do @enforce_keys [:name] defstruct name: nil @type t() :: %__MODULE__{ name: String.t() # Not nullable } end ``` -------------------------------- ### Traditional Elixir Struct Definition Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md This is the conventional way to define a struct in Elixir, including enforced keys, default values, and type specifications. It requires manual repetition of keys in both `defstruct` and `@typedoc`. ```elixir defmodule Person do @moduledoc """ A struct representing a person. """ @enforce_keys [:name] defstruct name: nil, age: nil, happy?: true, phone: nil @typedoc "A person" @type t() :: %__MODULE__{ name: String.t(), age: non_neg_integer() | nil, happy?: boolean(), phone: String.t() | nil } end ``` -------------------------------- ### Typed Struct in a Submodule Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Define a struct within a submodule to avoid boilerplate code when a module contains only a struct. ```elixir defmodule MyModule do use TypedStruct typedstruct module: Struct do field :field, term() end end ``` -------------------------------- ### Add @typedoc to Struct Type Source: https://github.com/ejpcmac/typed_struct/blob/main/README.md Add the `@typedoc` attribute within the `typedstruct` block to document the struct type. ```elixir typedstruct do @typedoc "A typed struct" field :a_string, String.t() field :an_int, integer() end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.