samgaw

samgaw

I’m working on a small library to scratch a current itch involving IP addresses & network subnetting that gives me a more structured datatype (a struct) after parsing a string or tuple.

While trying to improve it and make it nicer to work with, I started experimenting with deimpl Enumerable and managed to get things functional. However to do that, I ended up introducing a hidden key much like __struct__. Which then led me into deimpl Inspect in order to hide the new key and now I’m at a bit of a cross roads trying to balance two separate needs.

The struct I’m working with looks like:

%CIDR{
  first: {172, 16, 0, 0},
  last: {172, 31, 255, 255},
  prefix: 12,
  hosts: 1048576,
  version: :v4
}

And I get that using:

defimpl Inspect, for: CIDR do
  def inspect(%module{} = network, opts \\ []) do
    filtered = Map.drop(network, [:__enum__, :__struct__])
    Inspect.Map.inspect(filtered, Code.Identifier.inspect_as_atom(module), opts)
  end
end

Reading that in the console standalone is perfect because I can quickly scan and grab whatever information I need from it. But if I’m looking at a list of maps, and the struct is the value of a key in each, it becomes a lot of noise.

To suppress things a bit I had a look at sigils and ended up implementing one which meant I was able to do:

defimpl Inspect, for: CIDR do
  import Inspect.Algebra
  def inspect(network, _opts), do: concat(["~n\"", CIDR.to_string(network) , "\""])
end

That’s perfect when it’s used as a datatype in a map or struct but I lose the convenience of seeing the struct in cases like:

iex> CIDR.parse("10.0.0.0/8")
~n"10.0.0.0/8"

It may be asking for a lot but is there anything contextual in Inspect that would allow me to use the short form when it’s a nested value but the long form when it’s the parent structure displayed?

Thanks.

Showing Posts 1 to 2

fuelen

fuelen

:custom_options key in https://hexdocs.pm/elixir/master/Inspect.Opts.html probably is what you are looking for

lud

lud

Hi,

I don’t think that could be automatic but you could use the inspect options for that:

defmodule Test do
  defmodule CIDR do
    defstruct a: nil, b: nil, c: nil
  end

  def test do
    t = %CIDR{a: 1, b: 2, c: 3}

    IO.inspect(t)
    IO.inspect(t, pretty: true)
  end
end

defimpl Inspect, for: Test.CIDR do
  def inspect(%Test.CIDR{a: a, b: b, c: c} = t, %Inspect.Opts{pretty: true}) do
    "#CIDR<#{a}/#{b}/#{c}>"
  end

  def inspect(%Test.CIDR{a: a, b: b, c: c} = t, _) do
    "#CIDR<#{a}>"
  end
end

Test.test()
#CIDR<1>
#CIDR<1/2/3>

You can also use custom options, or other options from Inspect.Opts.

Of course that would still be up to you to pass or not the option that would change the result. Options will be passed down to your implementation from the implementation for lists.

— 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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
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

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews