Repository navigation
fix(fmt): format a reflect.Value as the value it holds - #544
Merged
Merged
Conversation
Go's fmt formats a valid reflect.Value operand as the value it holds: fmt.Sprint(reflect.ValueOf(5)) is "5". goscript's printed the Value's type, "int", and %d gave NaN. The operand is now replaced by its value, for both the print and the printf verbs; an invalid Value still formats as <invalid reflect.Value>. Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
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.
Go's fmt treats a valid reflect.Value operand specially: it formats the value the Value holds, so
fmt.Sprint(reflect.ValueOf(5))is5andfmt.Sprintf("%+v", reflect.ValueOf(p))prints the struct's fields. goscript's fmt printed the Value's type instead (int,main.pt,[]int), and%dprintedNaN. Test code that prints the reflect.Value results of an evaluation (for example a Go interpreter's test suite, which comparesfmt.Sprintf("%v", result)) failed throughout because of it.The two places every operand passes through, defaultFormatMaybe at depth 0 (Print, Println and the default verb) and formatOperandMaybe (the Printf verbs), now replace a valid reflect.Value by
Interface(), as Go's printArg does. An invalid Value is left alone and still formats as<invalid reflect.Value>. A reflect.Value nested inside another value is unchanged, as in Go, which unwraps only top-level operands.package_import_fmt checks %v, %d, %s and %+v, and Println with a slice and an invalid Value.