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

Decimal Support [Or Custom Type Support Documentation] #149

Open
bmurithi opened this issue Dec 27, 2017 · 3 comments
Open

Decimal Support [Or Custom Type Support Documentation] #149

bmurithi opened this issue Dec 27, 2017 · 3 comments

Comments

@bmurithi
Copy link

Noticed that :decimal ecto native type is not supported but managed to get it working via a custom ecto type.

Where is the best place to include this? README or module docs?

defmodule DecimalType do
  
    @behaviour Ecto.Type
    def type, do: :string
  
    def cast(term) when is_binary(term) or is_number(term) do
      {:ok, Decimal.new(term)}
    end
    def cast(_), do: :error
  
    def dump(%Decimal{} = term) do
      {:ok, Decimal.to_string(term)}
    end
    def dump(_), do: :error
  
    def load(term) when is_binary(term) or is_number(term) do
      {:ok, Decimal.new(term)}
    end
    def load(_), do: :error
  
  end
@ankhers
Copy link
Collaborator

ankhers commented Feb 8, 2018

Decimal support should be added to this adapter. Would you be willing to submit a PR?

@bmurithi
Copy link
Author

bmurithi commented Feb 9, 2018

I'll give it a go this weekend.

@bmurithi
Copy link
Author

@ankhers finally came around to look at this.

See (This fails the integration tests I believe as a result of incomplete decimal support):

https://github.com/bmurithi/mongodb_ecto/commit/165677ff6a98ce5947f5ab25a7f4411dec4b4e24

From what I can tell, there's mongodb does not support Mongo decimals as documented here:

https://docs.mongodb.com/manual/tutorial/model-monetary-data/

So my implementation would work only by coercing decimals to binaries.

Kindly take a look and advise?

Will take another stab at it see if I can get actual decimals marshalled correctly by the adapter both ways.

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

No branches or pull requests

2 participants