cedar-policy / cedar-go

Apache License 2.0
44 stars 7 forks source link

Expose `newDecimalValue` Function for Simplified Decimal Conversion #8

Closed caiorcferreira closed 2 months ago

caiorcferreira commented 2 months ago

Description:

The cedar-go library uses an int64 data type to represent decimal values, necessitating specific conversion logic to translate string representations of decimals into their numeric counterparts. Currently, this conversion is only accessible through the Decimal.UnmarshalJSON method.

In practice, this implementation poses limitations, especially when integrating with systems that do not use JSON for serialization, such as gRPC. For instance, when receiving data in formats other than the expected JSON structure ({"__extn": {"fn": "decimal", "arg": "12.5"}}), there is no straightforward way to utilize cedar-go's conversion logic directly.

To enhance flexibility and usability, I propose making the newDecimalValue function publicly accessible. This change would allow developers to directly convert a decimal string like "12.5" into the cedar.Decimal format (e.g., cedar.Decimal(125000)) without dealing with JSON-specific constraints.

Such an enhancement would greatly benefit those of us integrating cedar-go with various RPC frameworks and would streamline the handling of decimal data across different platforms.

Impact:

Proposed Solution:

Expose the newDecimalValue function as a public method, thereby extending the utility of the cedar-go library to handle decimal conversions more flexibly.

If you see fit, I can send a PR making this change.

philhassey commented 2 months ago

I think it would be sensible to do this along with exposing newIPAddr. I think these names would be fairly Go-idiomatic:

ParseDecimal and ParseIPAddr

If you want to make a PR, that would be great!

Thanks! -Phil