Spark.Dsl.Entity is option explicit or inferred?
I'm trying to figure out if there is a way to determine if an option was explicitly set or inferred. As an example, say I want to write a
Transformer
that marks every attribute as sensitive unless it has been explicitly set as non-sensitive (not quite my usecase but close). I've found where Entity.build
receives that info as opts
, and I can get and replace the attribute with the Transformer
helpers, but I'm not seeing any way to determine if it was explicitly set in the Transformer
. Is there a built-in way to determine what has been explicitly set? If not, any ideas for easily grabbing that info?11 Replies
We don't track that, unfortunately.
😔 maybe someday? seems like it would be useful for overriding default behaviour. hacked this together for now, definitely not complete code (need to check that the path is valid and not accidentally a deep match) but works for me, for now, and sharing is caring so:
def is_set?(dsl_state, path, key) do
{:ok, ast} = dsl_state.persist.file |> File.read!() |> Code.string_toquoted()
pre = fn
{head, meta, [^key | args]}, {[head], } -> {[], true}
{form, meta, [^key | args]}, {[], } -> {[], true}
{head, meta, args}, {[head | left], right} -> {{head, meta, args}, {left, [head | right]}}
ast, acc -> {ast, acc}
end
post = fn
{head, meta, args}, {left, [head | right]} -> {{head, meta, args}, {[head | left], right}}
ast, acc -> {ast, acc}
end
{, found?} = Macro.traverse(ast, {path, []}, pre, post)
found? == true
end
also, not seeing how to mark as solved in the discord app, sorry for leaving open atm
thats....pretty wild 😆
whats the specific thing you're looking to do out of curiosity?
We could potentially track things that were set by defaults, it wouldn't be all that difficult
or probably the other way around
track everything explicitly set
I'm generating some ash resources from a json definition, was hoping I could just monkeypatch the entities with the transformer but I don't want to overwrite any explicitly set options so...
ah, interesting
Yeah, I see what you're saying
If you're interested, I could advise on how you might add this to spark to track all explicitly set options?
no promises but sure
no promises necessary 😆
So ignoring entities for now and just focusing on sections
for some reason github is being super weird and I can't link you to the file?
anyway, its like line 956 in
spark/dsl/extension.ex
in spark
that current_config.opts
is everything that was actually set in code
Later on, we store the result of that section being evaluted like this:
if we added something like set_keys: [:foo, :bar]
then later, when we do the logic in set_state
, we traverse all the sections and get their entities/opts
We could then put into the :persist
key, something like
interesting...I gave up poking around L1619 before, I'll take a look at this, thanks
My pleasure 🙂
Spark can be pretty heady, but hopefully with some guidance we can get you there.
pr sent, lmk if that looks right
will review tomorrow 🙂