fix: std.format accepts boolean for numeric conversion codes#884
Open
He-Pin wants to merge 1 commit into
Open
Conversation
The official Jsonnet spec states that std.format follows Python's string formatting rules. In Python, bool is a subclass of int, so "%d" % True returns "1". This fix coerces boolean values to 0/1 before processing numeric conversion codes, matching Python's behavior. Added stdlib/std.jsonnet with the boolean coercion fix in format_code(). Regenerated astgen/stdast.go from the updated std.jsonnet. Updated golden files to reflect shifted line numbers in error traces.
4 tasks
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
The official Jsonnet spec states:
In Python,
boolis a subclass ofint, so"%d" % Truereturns"1". However, go-jsonnet currently rejects booleans for all numeric conversion codes withFormat required number at N, got boolean.This fix coerces boolean values to
0/1informat_code()before numeric conversion, matching Python's behavior.Upstream PR: google/jsonnet#1328
Before / After
"%d" % true"1""1""%f" % false"0.000000""0.000000""%x" % true"1""1""%s" % true"True""true""true"Changes
stdlib/std.jsonnet: Added boolean-to-number coercion informat_code(). The%sbranch is handled first (before coercion) so%swithtruestill returns"true". All numeric branches (d,o,x,f,e,g,c) usenum_valwhich coerces booleans to0/1.astgen/stdast.go: Regenerated from the updatedstdlib/std.jsonnet.Test plan
go test ./...passes)%d,%f,%x,%o,%e,%gwith boolean values produce correct output%swith boolean values still returns string representation