Raise error if struct field name is greater than 30 chars #1239
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.
What
Raise compile error if struct field name is greater than 30 chars, instead of 32 chars.
Why
As it happens the contract spec restricts the length of the struct field name to 30 characters. This is an oversight. The type is a StringM<30>, but we should have made it a ScSymbol in the XDR so that it picked up the max length of a Symbol. We did this with other parts of the spec, but not this one it seems.
It's probably too late to change the type in a backwards compatible way, at least now. And there isn't significant value in trying to fix that because struct fields over 30 characters long but still under 32 characters long are rare.
The code change introduces a constant in the code that matches the const generic expected on the StringM type. This probably looks a little odd but is done to introduce a compiler check/guarantee that the length of the value we print out can never diverge from the max size of the StringM that stores the value. If the XDR ever changes to use a larger value, this code in the SDK will fail to compile until we update it.
Close #1228
Other Problems This Creates
This discrepancy does create some problems. It's possible using the raw
Map
type to store key-value pairs where the keys areSymbol
s up to 32-bytes long, that cannot be transferred into acontracttype
because they cannot have field names greater than 30-bytes long. This is quite an edge case, and so unlikely to be a problem someone encounters, but it is unfortunate.Example
Before
No error. An empty field name is written to spec XDR.
After