bahner

bahner

To struct or not to struct, Decode or not decode

So, I am working with my IPFS client library and I’ve run into dialyzer hell. The code is kinda working fine, but Dialyzer is not happy with me. Before I ignore it completely, I want to make sure I’m not guilty of an anti-pattern.

The IPFS API returns stuff like this:

{
  "BlocksReceived": "<uint64>",
  "BlocksSent": "<uint64>",
  "DataReceived": "<uint64>",
  "DataSent": "<uint64>",
  "DupBlksReceived": "<uint64>",
  "DupDataReceived": "<uint64>",
  "MessagesReceived": "<uint64>",
  "Peers": [
    "<string>"
  ],
  "ProvideBufLen": "<int>",
  "Wantlist": [
    {
      "/": "<cid-string>"
    }
  ]
}

I thought it would be nice to make structs for these types of return values, instead of just passing the JSON back to user. I have made structs to build these, where I have structs for wantlist and CIDs.
myspace-ipfs/wantlist.ex at develop · bahner/myspace-ipfs · GitHub
myspace-ipfs/common.ex at develop · bahner/myspace-ipfs · GitHub

Then I use the structs as components for complex structs and so forth. I like the wantlist.keys notation that they give me when I structure the keys as (existing) atoms.

But when I take a result and pass it to Jason.decode, then dialyser becomes really unhappy. The error messages blow up in length, as I try to fix them. And, hence, I suspect I’m doing it wrong.

Please note. I can’t always use only use the Tesla.Middleware.Jason for this, as the API sometimes returns \n<\n when data is streamed, and there is no known end to the data, so it canæt generate lists, and hence not proper JSON.

Am I doing it wrong by trying to massage the data and should I just pass the JSON strings to the user? Or just let the middleware do its best and decode the lines as they come in.

Most Liked

al2o3cr

al2o3cr

What warnings? Where do they appear? I ask because I’ve seen plenty of very strange messages in-editor when elixir-ls gets confused, but those usually disappear after cleaning _build etc and don’t show up in mix dialyzer.

zachallaun

zachallaun

I personally have dialyzer disabled in my vscode-elixirls settings because I find the warnings very distracting for WIP code. When I am closer to a final version, I run it on the command line to check my assumptions and catch things I missed.

bahner

bahner

I took in the replies I got and learned that I was trying a bit too hard. So i loosened up trying to cram everything into a specific type in the api-module. Refactored the whole thing, in a way that means I have to do a little more work over each function, but have more control. Using the mix dialyzer allowed be to see the errors more clearly in the shell, than as small windows in VS Code.
The hint, that dialyser is usually right made me follow the advice to get a better picture of what iot wanted. And it worked. Thanks.

paulanthonywilson

paulanthonywilson

Fun thing about elixir_ls is that it doesn’t compile to ./_build, but to ./.elixir_ls (specifically ./elixir_ls/build/test). I don’t think I’m alone in occasionally zapping the .elixir_ls directory.

Another fun thing is that dialyzer is almost always correct although often cryptic. (When I say fun, I really mean infuriating.)

As @hlx says, I’d be inclined to also include dialyxir as a dependency (only: [:dev, :test], runtime: false) so you can run mix dialyzer from the terminal and fix the errors, eg with the capitalisation typo MySpaceIPFS instead of MyspaceIPFS, eg here

Also as @hlx alludes {:ok,MyspaceIpfs.RootCid}should probably be{:ok, :MyspaceIpfs.RootCid.t()}` (which I see you’ve defined in the struct).

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement