Implement units.parse and units.parse_bytes builtins#152
Open
glasses-and-hat wants to merge 1 commit into
Open
Implement units.parse and units.parse_bytes builtins#152glasses-and-hat wants to merge 1 commit into
glasses-and-hat wants to merge 1 commit into
Conversation
Adds the units.parse and units.parse_bytes OPA builtins, which were
previously unimplemented and raised FunctionNotFoundError. Both accept
strings like "10K", "1.5Gi", or "100m" and convert them to a number,
supporting the standard SI decimal (k/K, M, G, T, P, E) and binary
(Ki, Mi, Gi, Ti, Pi, Ei) suffixes, plus the milli ("m") suffix for
units.parse.
Arithmetic is done with BigDecimal to avoid floating-point precision
loss, mirroring the reference Go implementation's use of big.Rat/
big.Float: units.parse_bytes truncates towards zero, while
units.parse produces an exact integer when possible and otherwise
rounds to 10 decimal places.
Fixes open-policy-agent#133
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the
units.parseandunits.parse_bytesOPA builtins (closes #133), which were previously unimplemented and raisedFunctionNotFoundErrorfor every call."10K","1.5Gi","100m"into a number, supporting the standard SI decimal (k/K,M,G,T,P,E) and binary (Ki,Mi,Gi,Ti,Pi,Ei) suffixes, plus them(milli) suffix forunits.parse.extractNumAndUnitintopdown/parse_bytes.go/topdown/parse_units.go), including scientific-notation handling and theexponent too largeguard (max 6 exponent digits).BigDecimalfor exact arithmetic rather thandouble, to avoid floating-point precision loss on suffix multiplication (mirrors the reference implementation's use ofbig.Rat/big.Float):units.parse_bytestruncates the product towards zero (matchesbig.Float.Int()).units.parsereturns an exact integer when the product has no fractional part, otherwise rounds to 10 decimal places (matchesbig.Rat.FloatString(10)).no amount provided,could not parse amount to a number,spaces not allowed in resource strings,exponent too large,unit X not recognized) match the reference implementation's wording.BuiltinRegistry.BUILTIN_CLASSES(no external dependencies needed, consistent withArithmeticBuiltins/HexBuiltins).Test plan
opa-evaluator/src/test/resources/compliance/.../TestData/v1/units/(covering both happy-path and error cases, including the precision-regression cases intest-units-precision.jsonandtest-issue-4856.json) now pass — previously they were silently skipped via the compliance suite's "missing builtin" allowance.units.parse/units.parse_bytesno longer appear in the compliance suite's "Missing Builtin Report".opa-evaluatortest suite passes (./gradlew :opa-evaluator:test), no regressions../gradlew checkstyleMain checkstyleTest pmdMain pmdTestclean for the new file../gradlew compileJavasucceeds across all modules.