Ilyes512

Ilyes512

Can't use (Erlang) Records that start with an uppercased letter

So I am using Erlsom to parse a XML file that also has a matching XSD file. Erlsome includes an option to generate records (.erl file) based on the model of the XSD (erlsom:write_xsd_hrl_file).

This auto-generated .erl file includes records that start with an uppercased letter. For example:

-record('Pip3A4PurchaseOrderRequestType', {anyAttribs :: anyAttribs(),
	fromRole :: roleType(),
	'Authentication' :: 'AuthenticationType'(),
	'GlobalDocumentFunctionCode' :: string(),
	'PurchaseOrder' :: 'PurchaseOrderType'(),
	thisDocumentGenerationDateTime :: 'DateTimeType'(),
	thisDocumentIdentifier :: 'ProprietaryDocumentIdentifierType'(),
	toRole :: roleType()}).

So when I use a record in my elixir module (like the one below) I can’t use it.

Record.defrecord :Pip3A4PurchaseOrderRequestType, Record.extract(:Pip3A4PurchaseOrderRequestType, from: @hrl)
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> require Ot.Poc
Ot.Poc
iex(2)> record = Ot.Poc.Pip3A4PurchaseOrderRequestType()
** (SyntaxError) iex:2: syntax error before: '('

iex(2)> record = Ot.Poc.pip3A4PurchaseOrderRequestType()
** (UndefinedFunctionError) function Ot.Poc.pip3A4PurchaseOrderRequestType/0 is undefined or private. Did you mean one of:

      * Pip3A4PurchaseOrderRequestType/0
      * Pip3A4PurchaseOrderRequestType/1
      * Pip3A4PurchaseOrderRequestType/2

    (ot) Ot.Poc.pip3A4PurchaseOrderRequestType()

I thought I could fix this by defining the record as fallowed:

  #                 v--- lower cased p                              v--- Upper cased p
  Record.defrecord :pip3A4PurchaseOrderRequestType, Record.extract(:Pip3A4PurchaseOrderRequestType, from: @hrl)

Now I can call Ot.Poc.pip3A4PurchaseOrderRequestType(), but when I try to pass the record data it fails because the record links to other records that are starting with upper cased names, but first it fails because it can’t match it self with the data:

** (ArgumentError) expected argument to be a literal atom, literal keyword or a :pip3A4PurchaseOrderRequestType record, got runtime: {:Pip3A4PurchaseOrderRequestType, [], {:roleType, [],

So at this point I tried everything I could think off without success. What can I do without having to adjust the .erl file manually (392 LoC)?

Marked As Solved

Ilyes512

Ilyes512

I got it working by doing the following:

defmodule Ot.Poc do
    for name <- Record.extract_all(from: @hrl) |> Keyword.keys do 
      Record.defrecord :"#{name |> to_string }", Record.extract(name, from: @hrl)
   end
  # ...
end

edit:
BTW thanks @peerreynders! And the rest to of course! (quite surprised how fast I had my first reaction here :smiley:)

Also Liked

peerreynders

peerreynders

What about this approach:

require Record
defmodule Person do
  Record.defrecord :"Person", [:name, age: 25]
end

.

iex> require Person
iex> default = Person."Person"
{:Person, nil, 25}
iex> name_and_age = Person."Person"(name: "Bob Jones", age: 55)
{:Person, "Bob Jones", 55}
iex>

Example from Working with Erlang Records in Elixir

michalmuskala

michalmuskala

Record.defrecord can accept an additional argument to differentiate the macro name from the record tag. And that’s probably what you want - lowercase macro with an uppercase tag.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement