tangui

tangui

Hi all,

I’m working on a Phoenix project (Asteroid) in which I define some behaviours such as Asteroid.TokenStore.AccessToken. I want to have different implementations in separate libraries, such as

  • AsteroidTokenStoreAccessTokenMnesia
  • AsteroidTokenStoreAccessTokenRiak

My question is: what’s the best practice to import Asteroid.TokenStore.AccessToken (in lib/token_store/access_token.ex) in my separate libs?

I’ve tried with dependencies:

defmodule AsteroidTokenStoreAccessTokenRiak.MixProject do
...  
defp deps do
    [
      {:asteroid, path: "../asteroid", runtime: false},
      ....
    ]
  end
end

However, this approach causes 2 problems:

  • it imports the whole Phoenix project, dependencies and compiles them all - this is not wanted
  • side effect: this results in endpoint.ex of the Phoenix project not compiling because it cannot find Poison which is not loaded (and the configuration config :phoenix, :json_library, Jason in my Phoenix project is lost)

Cheers!

Showing Posts 1 to 4

hauleth

hauleth

You cannot do that. If you want to share that behaviours definitions then you need to extract the code to separate library.

gregvaughn

gregvaughn

@hauleth is correct. I want to also caution you against premature abstraction. If you try to do all the decoupling steps now while you really don’t even know how to do it, you are going to make your project more complex for yourself. I encourage you to write the application that you need to write. Later, after you’ve used and lived with the code for a while, then look for seams where it might make sense to extract a library.

tangui

tangui OP

Thanks for your answers.

Extracting the code seemed to be the way to go, but while coding I realized that this behaviour uses types and returns structs that are in other modules of the Phoenix project, and moving them also would result in extracting a important amount of modules (not sure where it’d stop).

Wouldn’t I be looking for something similar to C/C++/Erlang header file includes? Afaik there is no such thing in Elixir.

I think I’ll merge it in the core app as suggested, even though I’m not comfortable to include riak, redis, etc. as dependencies when not needed (but I guess runtime: false in mix.exs will do the the trick). I’ll post back for suggestions when I’ve some code to show.

Cheers

tangui

tangui OP

Now that I have some code to show, here’s an example. The goal is to allow using a behaviour in separate libraries (eg. : a Redis implementation of an access token store). The access token store behaviour of Asteroid refers to type in other modules:

defmodule Asteroid.TokenStore.AccessToken do

[...]

  @callback get_from_subject_id(Asteroid.Subject.id(), opts()) ::
  {:ok, [Asteroid.Token.AccessToken.id()]} | {:error, any()}

[...]
end

At that point I see 2 options:

  • Extract these types altogether with the behaviour to external libs (why not a type lib)
  • Not using these Asteroid.Subject.id() and Asteroid.Token.AccessToken.id() types but some more generic ones such as String.t() and extract only the behaviour

What do you think?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews