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

new! doesn't work well with string key maps #12

Open
fishtreesugar opened this issue Nov 22, 2024 · 2 comments
Open

new! doesn't work well with string key maps #12

fishtreesugar opened this issue Nov 22, 2024 · 2 comments

Comments

@fishtreesugar
Copy link

For example, define a struct named Test with a field called foo of type string:

defmodule Test do
  use Flint.Schema

  embedded_schema do
    field :foo, :string
  end
end

When calling new! with the atom key map Test.new!(%{foo: 1}), it will raise an exception:

** (ArgumentError) %Test{foo: ["is invalid"]}
    (flint 0.6.0) lib/schema.ex:445: Flint.Schema.new!/3
    #cell:5kooho4pfqmyzjah:1: (file)    

When calling new! with the string key map Test.new!(%{"foo": 1}), it will raise another exception:

** (KeyError) key "foo" not found
    anonymous fn/2 in Test.__struct__/1
    (stdlib 6.0) maps.erl:860: :maps.fold_1/4
    (elixir 1.17.2) lib/kernel.ex:2463: Kernel.struct!/2
    (flint 0.6.0) lib/schema.ex:445: Flint.Schema.new!/3
    #cell:3pmvlnztbon6drwt:1: (file)

I think this is an unexpected behavior, the string key map input's exception should be align with atom key map.

@acalejos
Copy link
Owner

I think this is expected. The first error is an error from the type different since you specified the field is a :string but you provided an int. The second error is because the string key is different from an atom key, which also makes sense.

@fishtreesugar
Copy link
Author

fishtreesugar commented Nov 22, 2024

@acalejos but new accept both string and atom key maps.

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

No branches or pull requests

2 participants