beatscode
I’m porting an application written in go to elixir. I’m just starting to learn elixir. In go it is easier to convert your data to structs to easily get properties from objects. I’d like to do the same in elixir. I’d like to convert a json payload to a set of defstructs. I see the docs on defstruct but I am wondering what developers do when the defstruct needed is too tedious to write. Are there any tools to convert a json payload to a defstruct definition?
I see the struct function but I would still need to write out the defstruct definition to make that function work. I am also using poison to convert the payload to a map but I’d like to use custom types instead.
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Why do you need it to be a struct? Can’t you use a map instead?
I do usually try to avoid to create struct-only modules, so unless you do not want to create related functions, using a plain map might be so much easier.
beatscode
I was looking at using guards with functions but doesn’t seem like I can use Map.get inside the guard.
I would like to create related functions as well. Can I not make related functions with structs?
NobbZ
You can’t access maps (structs are just sugar over maps) in a guard, but you can match on them in the function head or in a case-clause or using the match operator.
I do not understand that question.
beatscode
If you’re saying that its easier to use map structures instead, I’m confused as to how one would try to reason about these maps and think in terms of concrete types. In order to create concrete types I’d have to create defstructs.
I didn’t quite understand this statement, which prompted my question. You mentioned not creating related functions. Under what conditions can I not make related functions?
I’ll look into the case syntax
gon782
I made this some time ago just for shits and giggles: GitHub - GoNZooo/struct_provider: Macro(s) to convert data into struct definitions in Elixir · GitHub
It takes JSON input and creates structs from it. Note that I consider it pretty useless, but if you wanna make something internal from it that’s actually usable for your team, feel free to take and and work out some kinks. As an example of a deficiency, it doesn’t define new modules from nested JSON data, so you’ll only get the top level. The
from_jsonversion would probably be a good place to start with and then downloading JSON data to disk as schema for input.Edit: The reason I find it pretty useless is fundamentally that it’s not backed by statically checked types. If it was, you could know whether your assumptions changed from under you much easier, so then you could pull the data straight from HTTP and generate the types without having to care, because the compiler would always tell you anyway.
NobbZ
I said, that I avoid creating defstruct only modules. If though I have related functions for some type, I do create a module and a defstruct as well. Thats it.
Also JSON doesn’t give you any guarantee about types, so why care?
Creating and maintaining depply nested structs is a burden and comes with a technical debt, when in fact all you need to rely on is the contract that there will be certain fields and you never care for the rest.
This is especially true when you have a JSON which constantly changes its shape and you need to find out the proper type first by checking a certain key in it.
anthonator
Since your example was to use
Map.getin your guard it makes me think you might be able to pattern match instead.anthonator
I completely agree with this.
OvermindDL1
If using Poison you can have it auto-convert a JSON string to a specific struct(s). You’d still need to write your structs, but once done then you have a confirmed structure.