Closed nycdotnet closed 6 years ago
Hi Steve,
I'm currently flying back from a conference, and will be at more conferences next week. I'll try to give this some thought, but it may be a few days. Hope that's okay!
No worries at all. Thanks and safe travels.
I'm fine with the xunit update
Using the default literal wouldn't work, as the default for string is null, which would immediately fail validation. I'm not sure that it's a good idea to have a default at all, to be honest. If you're creating a Money value, you should provide the currency. I'd expect anything receiving a value to want there to be a currency code. (Validating that it's a suitable ISO 4217 might be a bit far, admittedly.)
ArgumentOutOfRangeException sounds reasonable if the units don't fit. I would err on the side of throwing if the nanos don't fit as well - but you could potentially have an overload accepting a RoundingMode enum or something like that. I'd be tempted to add that at a later time though - which is another argument in favour of not having a default value for the currency parameter, as overloading gets hairier with optional parameters.
cc @chrisdunelm for visibility - Chris, does the above all sound reasonable to you?
RoundingMode
could be added.Thanks. I will make the ToMoney extension method have a required string currencyCode
on it and throw ArgumentOutOfRange
if either the units or nanos cannot be represented exactly (aka are not round-trippable). I prefer to have passing "" as the currencyCode not throw, as specifying "" when creating the proto manually with "" does not throw today. Attempting to set null
on the CurrencyCode field does already throw, and the extension method will inherit this behavior. I agree the rounding stuff can be done at a later time if anyone is so interested. Thanks!
Implemented via #267.
Closing this PR - but please let me know the level of urgency for creating a release with this change in. (I have no explicit reason to wait, beyond "it's a little bit of work to do it".)
No urgency. Thank you.
Greetings,
I am putting together a PR to add an extension method on
decimal
to convert toGoogle.Type.Money
with an optional currency code, and to add aToDecimal()
extension onGoogle.Type.Money
that omits the currency type. The pattern used will be similar to #251 and #256.I have a few questions that I'd love to resolve ahead of time.
public static Money ToMoney(this decimal value, string currencyCode = "")
as the signature of theToMoney
extension method. Is this acceptable to you? Would it be better to usedefault
withcurrencyCode
which would require upgrading the project from C# 7 to C# 7.1?I am thinking that if the integral part of the decimal doesn't fit in the
Units
field (long), theOverflowException
should be caught and used as theInnerException
of anArgumentOutOfRangeException
. Would love your thoughts on if there is value in doing this, or if we should just not catch theOverflowException
.What should happen when the Nanos don't fit is less clear, however. For example, should
0.555_555_555_5M
be truncated to555_555_555
nanos? rounded to555_555_556
nanos? Should this throw? Should the user be able to specify using some sort of enum with the default being to truncate since this sort of thing is only relevant to a very small number of people who will likely want to specify the behavior?Would love your thoughts on the above. Thanks!