chazwatkins
I’m considering writing an integration with Salesforce. Salesforce has SObjects of various types like Case, Quote, Lead, CustomObject__c, etc. These SObjects are like database tables with their own schemas. I’d like to convert the JSON responses I get back from Salesforce into structs that represent the SObject type. For example, %Case{}, %Quote{} instead of something more generic and known at compile time like %SObject{type: "Case"}. There are hundreds of SObject types, so defining them all isn’t reasonable and wouldn’t cover custom SObjects or custom SObject fields not provided by Salesforce itself.
Is there any way to define structs at runtime to achieve something like %{SObjectName}{}?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
D4no0
What is the value of having those schemas if you are defining them at runtime?
The whole reason behind schemas is to have more compile-time guarantees, if you generate them at runtime there is no way your codebase will have any idea of those schemas.
Maybe the question was not addressed correctly and you are looking for one of the following things:
sodapopcan
Hello and welcome!
There is not, one of the whole points of structs is to provide compile time checks, including against keys.
If you are going for dynamic runtime, maps should be largely fine. You can still pattern match. I guess the only problem you might face is accidentally adding a wrong key after initialization but you could guard against that with custom functions, I suppose (though probably not worth it).
chazwatkins
There’s no database involved, so schemas are necessary. This is just an integration wrapper for Salesforce APIs. The value is pattern matching on the struct itself and knowing its SObjectType without looking at a field value. I can make a generic SObject struct and have a type field if that is impossible.
chazwatkins
Thanks for the feedback. I suspected such, but I wanted to check.
D4no0
It highly depends on what you are looking for, first of all ecto can be used without a database, and it is widely used for data validation. In your case, from what I understand the protocol defines the type of the structure in one of its fields.
As long as you can define those types in your codebase, you can always use ecto + polymorphic embed.
chazwatkins
Thanks for the information. I consider this one. I hadn’t heard about polymorphic embeds before.
VOUwCNOD
Yes, you can create structs in runtime. Consider this code
This is a dark magic, but there is no need to limit yourself, be expressive, break laws, make people be afraid of yourself and make them treat your code with fear and respect.
To match on such structs in runtime, consider something like
sodapopcan
@chazwatkins Oh boy, do not follow the advice above. You’re opening yourself up to atom overflow issues and as well as a host of warnings about module redefinitions you’ll have to code around.
chazwatkins
The use case I’m looking at wouldn’t go anywhere near the atom limit as there are only, at most, one to two thousand SObject types in complex Salesforce orgs. You’re likely only using a subset of them, a few hundred at most for large organizations.
It doesn’t look like there is a solid way to convert a string to a module name without a prefix of
:like%:Case{}. It’d be great for using them with Protocols, but it’s just as easy to pattern match on something like%SObject{type: "Case", fields: ...}and dispatch to the corresponding Case business logic.It’s good to know that this is possible, though. I completely understand the point of not using it if the input is unbound, where it could overflow the atom table.
sodapopcan
The problem lies more in that you are dynamically creating modules at run time. Once those are created, they’re there. If you need to change them and your users are, for instance, using hot code reloading, they won’t get your changes. As far as atom overflow goes, sure, there may be a finite number of entities you are dealing with, but it’s now something you have to think about and secure against. In short, this is not well documented for a reason and why I didn’t mention it in the first place. If you’re ok mixing artsy code into your business logic, that’s on you. It’s not something I would deem responsible in a library you are wanting people to use, though. I personally would never use something if I knew it was doing this.