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

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement