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

Numeric type casting of attributes nested in maps #120

Open
linkyndy opened this issue Apr 4, 2022 · 7 comments
Open

Numeric type casting of attributes nested in maps #120

linkyndy opened this issue Apr 4, 2022 · 7 comments
Labels
feature-request A feature should be added or improved.

Comments

@linkyndy
Copy link

linkyndy commented Apr 4, 2022

We have float_attr and map_attr definitions, however there seems to be no way of defining the types of attributes nested in maps. I am currently reading a float out of a map and the result is a BigDecimal (0.2997e2 instead of 29.97), as per DynamoDB's Ruby SDK.

Is there any way to address this?

@mullermp
Copy link
Contributor

mullermp commented Apr 4, 2022

Thanks for opening an issue. I believe maps behave similar to JSON objects, and each key-value is unknown to aws-record. (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes). Can you share your code on how you're reading out the float out of your map? And can you confirm that 29.97 is what's stored in DynamoDB (check raw values and not parsed values by the Ruby SDK, by either checking the wire tracing logs or check console)?

@awood45
Copy link
Member

awood45 commented Apr 4, 2022 via email

@linkyndy
Copy link
Author

linkyndy commented Apr 5, 2022

Thank you both for your answers.

Well, attributes (except the hash/sort keys) don't need to have a type, however aws-record allows you to define it for practical reasons. This is why I was curious why wouldn't it be possible for maps as well. 29.97 is actually stored in DynamoDB, yes.

A custom marshaller sounds indeed interesting, thank you for your suggestion.

This being said, are there no plans of supporting type declarations for map attributes?

@awood45
Copy link
Member

awood45 commented Apr 5, 2022 via email

@mullermp
Copy link
Contributor

mullermp commented Apr 5, 2022

Well you can certainly call to_f on BigDecimal if you'd like to work with floats. It seems that DynamoDB has a very large number range and doesn't have a concept of integer or float, just "number" where BigDecimal is a catch-all.

@linkyndy
Copy link
Author

linkyndy commented Apr 6, 2022

What I had in mind, was something along the lines of:

string_attr :foo
map_attr :bar do
  float_attr :baz
end

Attributes not defined inside a map would resort to the defaults, same as the attributes defined at the model level. This won't require any new marshaller, rather than recursively marshall nested attributes using the known marshallers (there's a finite list of types that can be nested inside a map).

@mullermp mullermp added the feature-request A feature should be added or improved. label Apr 8, 2022
@mullermp
Copy link
Contributor

Related to #96

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

3 participants