Format inspect output as valid elixir

59 views
Skip to first unread message

Adam Kirk

unread,
May 9, 2023, 11:42:40 AM5/9/23
to elixir-lang-core
I do this ALL the time, but heres a specific example: When I write a test, I do something like 

assert [] = Repo.all(Thing)

it fails, I copy what the match shows into my test and modify it to what I need.  

except that it outputs stuff like:

```
__meta__: #Ecto.Schema.Metadata<:loaded, "thing">
```
which isn't valid elixir, so I can't run mix format to make it nice before I modify it.

This applies to a ton of workflows I have. I'm curious why it can't be output as valid elixir instead of this special formatting with special characters like `#` etc

José Valim

unread,
May 9, 2023, 11:51:54 AM5/9/23
to elixir-l...@googlegroups.com
There is a "structs: false" option but I am not sure if that is supported when diffing.

But also keep in mind there are structures that represent memory data (such as PIDs and REFs) and those can never really be copy and pasted. And sometimes, like above, it shows in a special style because it is private and you are not supposed to assert on it (i.e. you should remove it).

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3648f1d0-73a0-47a5-ac83-6bec7c933290n%40googlegroups.com.

Adam Kirk

unread,
May 9, 2023, 11:57:04 AM5/9/23
to elixir-lang-core
Ok, I guess I just wish it was `mix format` friendly because then its much much easier to remove it

José Valim

unread,
May 9, 2023, 12:03:36 PM5/9/23
to elixir-l...@googlegroups.com
Yeah, I see. One solution would be to introduce /* ... */ style comments (not exactly with this syntax) but it sounds like a large change for small benefits. Can anyone think of other approaches?

Adam Kirk

unread,
May 9, 2023, 12:04:49 PM5/9/23
to elixir-lang-core
Yeah, I was afraid it might be a poor cost/benefit ratio. I guess I posted here in case a lot of other people felt this pain as well.

Billy Lanchantin

unread,
May 9, 2023, 12:06:46 PM5/9/23
to elixir-lang-core
I encounter this often. Here's a small workaround I use.

I copy/paste the struct into a new VSCode tab and do the following find/replace

Search: `.+#.+\n`
Replace: `` (aka nothing)

Then you can format. E.g. this turns:

```
%User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: 123,
primary_contact_id: 456,
primary_contact: #Ecto.Association.NotLoaded<association :primary_contact is not loaded>,
name: "Name",
created_at: ~U[2023-03-27 20:27:27.756360Z]
}
```

into

```
%User{
id: 123,
primary_contact_id: 456,
name: "Name",
created_at: ~U[2023-03-27 20:27:27.756360Z]
}
```

This doesn't really address your concern, but I do it so frequently that I figured I'd share.

Adam Kirk

unread,
May 9, 2023, 12:32:21 PM5/9/23
to elixir-lang-core
Thats smart. I think I'll try making an alfred workflow to transform it in my clipboard
Reply all
Reply to author
Forward
0 new messages