bahner

bahner

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.
https://github.com/bahner/myspace-ipfs/blob/develop/lib/myspace_ipfs/structs/bitswap/wantlist.ex
https://github.com/bahner/myspace-ipfs/blob/develop/lib/myspace_ipfs/structs/common.ex

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.

First 9 of 9 Posts Switch mode

al2o3cr

al2o3cr

Can you post an example of the error messages or some example code? I’m not quite following what you’re passing to Jason.decode.

I don’t see anything immediately “uh-oh” in those files.

bahner

bahner OP

If I try this function: (Whish doesn’t involve Jason.decode here. So it creates less confusion for this disciussion)
https://github.com/bahner/myspace-ipfs/blob/develop/lib/myspace_ipfs/dag.ex#L78
Like this:

  def put(data, opts \\ []) do
    multipart_content(data)
    |> post_multipart("/dag/put", query: opts)
[....]
myjson = "{\"Key\":\"Value\",\"Bar\":[1,2,3]}"
MyspaceIpfs.Dag.put(myjson)
# It yields:
{"Cid":{"/":"bafyreidicaztrpvtfzvq3xolltjotbs6hfmiwv5qzbtimd5245aaw2y6yq"}}
# which Tesla correctly turns into:
%{
  "Cid" => %{
    "/" => "bafyreidicaztrpvtfzvq3xolltjotbs6hfmiwv5qzbtimd5245aaw2y6yq"
  }
}

BUt i want to turn this into a struct, so I have a function which iuses Recase to turns keys into existing atoms and snake case them:

 @spec snake_atomize(map) :: map
  def snake_atomize(map) when is_map(map) do
    map
    |> Recase.Enumerable.convert_keys(&Recase.to_snake/1)
    |> Recase.Enumerable.convert_keys(&String.to_existing_atom/1)
  end

When I add this in the pipeline my result is as expected:

{"Cid":{"/":"bafyreidicaztrpvtfzvq3xolltjotbs6hfmiwv5qzbtimd5245aaw2y6yq"}}

%{cid: %{/: "bafyreidicaztrpvtfzvq3xolltjotbs6hfmiwv5qzbtimd5245aaw2y6yq"}}

But then the warnings start, I want to extract the cid, so I pipe like so:

  @spec put(binary, opts) :: {:ok, MyspaceIpfs.RootCid} | {:error, any}
  def put(data, opts \\ []) do
    multipart_content(data)
    |> post_multipart("/dag/put", query: opts)
    |> snake_atomize()
    |> Map.get(:cid, nil)

# Which correctly yields:
%{/: "bafyreidicaztrpvtfzvq3xolltjotbs6hfmiwv5qzbtimd5245aaw2y6yq"}

And then the warning starts.

This is not the only place, but it’s a pretty simple example.

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.

bahner

bahner OP

In VS Code, as I write. But I have a makefile, which cleans _build and I do that ever so often.

But I assume from your reply, that I shouldn’t worry too much about this.

hlx

hlx

It would be helpfull if you can post the output of mix dialyzer here as well. In case you do not have it in your deps you can add it: dialyxir | Hex

It seems that you have elixir-ls enabled in your vscode but sometimes it might be easier to see the output on the command line. (sometimes elixir-ls is “wrong” or needs to catch up)

Looking briefly at your code I think the return spec it wrong here:
https://github.com/bahner/myspace-ipfs/blob/develop/lib/myspace_ipfs/dag.ex#L77

I’m guessing you are missing .t() in your :ok tuple.

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.

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).

bahner

bahner OP

Thanks. I appreciate the advice. I have installed the dialyxir dependency and that renders the feedback much more easy to read. I will disable the dialyzer in VS Code and add “mix dialyser” to the test / compile process somewhere.

bahner

bahner OP

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.

https://github.com/bahner/myspace-ipfs/commit/c1af0371f225d525a4dafc7a0bf7fedf6667a664

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement