Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back fmt.Stringer implementation for interpreter.Value #969

Merged
merged 2 commits into from
Jun 3, 2021

Conversation

turbolent
Copy link
Member

Description

In #906 we refactored the string formatting of interpreter values so it supports recursive structures (due to references). This PR then removed support for fmt.Stringer.

This is problematic, because many parts of the code, most prominently errors, rely on values implementing String() implicitly by using fmt functions with the %s format directive.
Now that support for String() was removed from values, error messages were rendered incorrectly.

Most notably, address values are just byte slices, and Go's default formatting, which will be used when the value does not implement fmt.Stringer, will encode the bytes directly as-is. For example, the byte slice []byte{0x1, 0x2} results in the Go string "\x01\x02" – which is not valid UTF-8.
This results in problems in downstream dependencies like flow-go, which requires strings to be valid UTF-8 (e.g. for Protocol Buffer encoding).

Bringing back support for fmt.Stringer thus also fixes many error messages that are currently incorrectly formatted.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@turbolent turbolent self-assigned this Jun 2, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2021

Codecov Report

Merging #969 (f067386) into master (39037d9) will increase coverage by 0.03%.
The diff coverage is 67.14%.

❗ Current head f067386 differs from pull request most recent head adc740e. Consider uploading reports for the commit adc740e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #969      +/-   ##
==========================================
+ Coverage   75.42%   75.46%   +0.03%     
==========================================
  Files         268      268              
  Lines       32773    32868      +95     
==========================================
+ Hits        24720    24803      +83     
- Misses       6921     6929       +8     
- Partials     1132     1136       +4     
Flag Coverage Δ
unittests 75.46% <67.14%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/interpreter/deployed_contract.go 32.00% <0.00%> (-1.34%) ⬇️
runtime/interpreter/function.go 56.00% <0.00%> (-1.45%) ⬇️
runtime/stdlib/builtin.go 95.52% <0.00%> (ø)
runtime/interpreter/value.go 77.44% <73.21%> (+0.17%) ⬆️
runtime/interpreter/block.go 74.19% <100.00%> (+0.86%) ⬆️
runtime/interpreter/errors.go 26.59% <100.00%> (+5.31%) ⬆️
runtime/runtime.go 87.22% <100.00%> (ø)
runtime/sema/type.go 88.13% <0.00%> (-0.25%) ⬇️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 39037d9...adc740e. Read the comment docs.

Copy link
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👏

@@ -6751,7 +6858,7 @@ func formatComposite(typeId string, fields *StringValueOrderedMap, results Strin
Value string
}{
Name: fieldName,
Value: value.String(results),
Value: value.RecursiveString(results),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea! 👍

runtime/interpreter/errors_test.go Outdated Show resolved Hide resolved
Co-authored-by: Supun Setunga <supun.setunga@gmail.com>
@turbolent turbolent merged commit 24b1a28 into master Jun 3, 2021
@turbolent turbolent deleted the bastian/value-stringer branch June 3, 2021 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants