GithubHelp home page GithubHelp logo

tcoopman / boundary Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sasa1977/boundary

0.0 1.0 0.0 144 KB

manage and restrain cross-module dependencies in Elixir projects

License: MIT License

Elixir 100.00%

boundary's Introduction

Boundary

Boundary is a library which helps managing and restraining cross-module dependencies in Elixir projects.

Status

Highly experimental, untested, and unstable.

Documentation

For a detailed reference see moduledoc here and here.

Basic usage

To use this library, you first need to define the boundaries of your project. A boundary is a named group of one or more modules. Each boundary exports some (but not all!) of its modules, and can depend on other boundaries. During compilation, the boundary compiler will find and report all cross-module function calls which are not permitted according to the boundary configuration.

Example

The following code defines boundaries for a typical Phoenix based project generated with mix phx.new.

defmodule MySystem do
  use Boundary, deps: [], exports: []
  # ...
end

defmodule MySystemWeb do
  use Boundary, deps: [MySystem], exports: [Endpoint]
  # ...
end

defmodule MySystem.Application do
  use Boundary, deps: [MySystem, MySystemWeb]
  # ...
end

The configuration above defines three boundaries: MySystem, MySystemWeb, and MySystem.Application.

Boundary modules are determined automatically from the boundary name. For example, the MySystem boundary contains the MySystem module, as well as any module whose name starts with MySystem. (e.g. MySystem.User, MySystem.User.Schema, ...).

The configuration specifies the following rules:

  • Modules residing in the MySystemWeb boundary are allowed to invoke functions from modules exported by the MySystem boundary.
  • Modules residing in the MySystem.Application namespace are allowed to invoke functions from modules exported by MySystem and MySystemWeb boundaries.

All other cross-boundary calls are not permitted.

Next, you need to add the mix compiler:

defmodule MySystem.MixProject do
  use Mix.Project

  def project do
    [
      compilers: [:phoenix, :gettext] ++ Mix.compilers() ++ [:boundary],
      # ...
    ]
  end

  # ...
end

Boundary rules are validated during compilation. For example, if we have the following code:

defmodule MySystem.User do
  def auth do
    MySystemWeb.Endpoint.url()
  end
end

The compiler will emit a warning:

$ mix compile

warning: forbidden call to MySystemWeb.Endpoint.url/0
  (calls from MySystem to MySystemWeb are not allowed)
  lib/my_system/user.ex:3

The complete working example is available here.

Because boundary is implemented as a mix compiler, it integrates seamlessly with editors which can work with mix compiler. For example, in VS Code with Elixir LS:

VS Code warning 1

VS Code warning 2

Roadmap

  • support nested boundaries (defining internal boundaries within a boundary)
  • validate calls to external deps (e.g. preventing Ecto usage from MySystemWeb, or Plug usage from MySystem)
  • support Erlang modules

License

MIT

boundary's People

Contributors

sasa1977 avatar

Watchers

 avatar

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.