Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom :state attribute always returns error due to bug #61

Closed
DriesCruyskens opened this issue Oct 21, 2024 · 3 comments
Closed

Custom :state attribute always returns error due to bug #61

DriesCruyskens opened this issue Oct 21, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@DriesCruyskens
Copy link
Contributor

When a custom attribute for the state is defined:

state_machine do
  initial_states([:initial_state])
  default_initial_state(:initial_state)
  state_attribute(:state)
end

attributes do
  attribute :state, :string do
    allow_nil? false
    default :initial_state
    public? true
  end
end

The following error is thrown:

** (Spark.Error.DslError) [ResurrectRegistry.Credits.Credit]
attributes -> state:
  Expected the attribute state to have `default` set to :issued

Digging deeper I found that the default :initial_state attribute option arrives inside the transformer converted to a string:

attribute #=> %Ash.Resource.Attribute{
  name: :state,
  type: Ash.Type.String,
  allow_nil?: false,
....
  default: "issued",
...
}

And so the comparison attribute.default != default compares :initial_state with "initial_state" and always returns an error.

@DriesCruyskens DriesCruyskens added the enhancement New feature or request label Oct 21, 2024
@zachdaniel
Copy link
Contributor

Ah, good point. We should check that it's equal as an atom or as a string. Probably best to do to_string/1 on both before comparing. Would you be interested in PRing that fix?

@DriesCruyskens
Copy link
Contributor Author

Will do soon!

@DriesCruyskens
Copy link
Contributor Author

The problem was that I declared a state attribute myself, and that it had typed :string instead of :atom. This explains why the default value came back as string instead of an atom. I also assumed I had to create a custom attribute myself which turns out it wasn't necessary. I permitted myself to add this to the docs in #62.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants