12/31/2023 0 Comments Read in json file elixir ecto# Creates a module under the main API.Const module using the filename. So that we can export into frontend clients if needed,ĪPI.(:ORG_STATUS_ACTIVE) => 100101ĪPI.(100101) => lib imsapi common filename We still break up the constants with regards to their respective areas, and Makes it a lot easier to debug things as we are basically just using atomsĮverywhere in the code - we change to integers on the app boundary. Let us encode/decode from either atom => integer or integer => atom. Like a map to expose atoms => integers we create a set of functions that So this is end result of all the suggestions (thanks again!) defmodule API.Const """Ī single point that we can use constants from. Dynamically create “nested modules” based on the JSON filename.Allow atoms to be used during dev/debug, but integers for storage/comms. Custom Ecto field for converting when entering/retrieving from DB.I’m sure there are things I can tweak in the below, but it seems to work fine for what I need right now. Seems I’ve been spending too much time using things that live reload code, the files DO recompile if I change the JSON, I was just waiting for it to happen live That makes a much nicer way to access the different files, so thanks for that suggestion I’m not sure if I’m doing something wrong, but it doesn’t seem to recompile if I modify one of the JSON files? Thanks guys, I’m really liking where this has gone! So, I’ve tried to implement both of those ideas, and I now have defmodule API.Const lib imsapi common filename ĭefmodule ncat() doĭef encode(unquote(String.to_atom(key))), do: unquote(value)ĭef decode(unquote(value)), do: unquote(String.to_atom(key)) |> Enum.reduce(fn(c, map) -> Map.merge(map, c) end) defmodule API.Const lib api common constants))Ĭonsts = Enum.map(fn(filename) -> File.read!(filename) |> code! end) I ended up going with the idea from - it makes sense to use atoms everywhere I can, then just have integers in the DB and frontend client. Likewise, if you need to send a response to some client, or store to the database, you perform Const.encode to convert the atom into a corresponding integer. :source_lvl1_api), so the code is ridden of magical numbers. Then in the rest of your code, you just deal with atoms (e.g. So what Michal is trying to tell you is that as soon as you take some input from say HTTP request, or the database, you invoke code to convert it into an atom. Using that approach, you could have something like: defmodule Const do But even if this is not the case, my previous point stands, and I would personally go for Michal’s solution. Moreover, I’m not completely sure whether this map is stored in the so called “constant pool”, and if it’s not, then the performance might suck. This can be easily done, even during compilation time, but then the code becomes as complex if not more than Michal’s version. If you need to convert an integer into a const, you’ll have to build a reversal map. So it looks like idea of using a map would be the easiest way to get them across, but I’m intrigued by what you said would definitely go for the approach from One problem with maps is that it only supports one way conversion (converting a const into an integer). Macros so they can be used in guards and matchesĬreate a module to define your shared constantsĭef myfunc(item) when item = Const.something, do: Const.something + 5ĭef myfunc(item) when item = Const.another, do: Const.anotherĭefmacro unquote(name), do: "Define a constant. Module that can be shared with other modules. This module offers an approach to define these in a defmodule Constants """Īn alternative to use value approach to defined reusable The benefit being that they work in matches and in guards. I have a macro for reusing constants across multiple modules. Is there a way to do something similar in Elixir? Or is there a better way to store lots of constants that could be the same name, but relate to different areas? If (SysConst::SOURCE_LVL1_API = $someOtherVariable) I’m porting an old PHP application over to Elixir and Phoenix, and I’m struggling to figure out the best way to transfer the constants that currently exist.Īt the moment there are multiple files with various constants used throughout the app, for instance:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |