-
Notifications
You must be signed in to change notification settings - Fork 140
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
enhance ToGoValue() in cadence.Value #2531
Conversation
values.go
Outdated
return string(v) | ||
value := string(v) | ||
if value == "" { | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return nil
here for an empty string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is just a leftover from my blindly porting over the overflow code. The reasoning there was to make the output as terse as possible.
I can change this all the places here because I think that in cadence we should not make this distinction.
@@ -333,7 +338,7 @@ func (Address) MeteredType(common.MemoryGauge) Type { | |||
} | |||
|
|||
func (v Address) ToGoValue() any { | |||
return [AddressLength]byte(v) | |||
return v.String() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change to a string here? A Go byte array seems like a better mapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason i did this is that i just find 0x123 easier to work with and reason about then a byte array, but that might just be me.
if err != nil { | ||
panic(err) | ||
} | ||
return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed point numbers are not floating point numbers. This potentially a lossy conversion, as some fixed-point values cannot be represented as floating point numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, what situations would that occur in?
if len(ret) == 0 { | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe move this to the top of the function, before unnecessarily allocating an empty slice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to skip nil values in the list we cant do this. Do we want to do that or not? I personally prefer it, but if you do not want it I can change it around.
values.go
Outdated
@@ -1617,12 +1636,18 @@ func (v Dictionary) WithType(dictionaryType *DictionaryType) Dictionary { | |||
} | |||
|
|||
func (v Dictionary) ToGoValue() any { | |||
ret := map[any]any{} | |||
ret := map[string]any{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change the key type to always being a string? What if the key type of the dictionary is e.g. Int
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing left over from overflow. Will fix.
values_test.go
Outdated
value: NewArray([]Value{ | ||
NewInt(10), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of changing the existing value, add an additional one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old test has an array that has both and int and a string, I can always add it back i just though it was very strange.
@@ -1493,7 +1502,11 @@ func (v UFix64) MeteredType(common.MemoryGauge) Type { | |||
} | |||
|
|||
func (v UFix64) ToGoValue() any { | |||
return uint64(v) | |||
value, err := strconv.ParseFloat(v.String(), 64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But here it is OK to use ParseFloat or can we not do that as per the comment in Fix64?
Sorry for the delay here, I added some comments and fixed some things! |
Status? |
@SupunS Could you please own this? |
Yeah, can do. I'll have a look later today. In the meantime, were there any particular use-case(s) we wanted to enhance, or is this just a general improvement? |
Discussion around the use-cases: https://discord.com/channels/613813861610684416/1108479699732152503/1146911629481619606 |
Given there are many different use cases and needs, and there is no "correct" Go type to return in all cases. It might be a good idea to "remove" this method from
How does that sound @bjartek? |
|
I dont have much time for this atm :( a lot to do. |
No worries! We can pick this up after shipping Cadence 1.0, thank you for these contributions and your patience with getting them in |
When will the removal of ToGoValue() hit a network? I think i use that in overflow/underflow. |
It's not affecting networks, only code that imports Cadence / the |
Why was removing ToGoValue() not a flip? this is a pretty breaking change. |
@bjartek We only have FLIPs for the language, not the implementation. The fact that we expose some functionality from this implementation of the language is just a side-effect, there are no guarantees about what is exposed from it. In any case, the functionality got removed from the package, but it can be easily provided by third-party code (e.g. another open-source Go module, or directly as part of an application), none of the underlying functionality needed for it is gone. |
Can I ask why it is removed? Ref Hyrums law. The cost og this change could be quite high. |
See my comment above: #2531 (comment). I had also tried to explain it here: onflow/flow-go#5820 (comment) The goal of #3290 was to remove access by index. The removal was literally a result of this PR: We cannot change the implementations of |
Ok. So when will this version of cadence be used? Because this is more work for everybody and they need to know that they have to do the work. |
@bjartek I though I had already answered the same question above: #2531 (comment). This change only affects developers using the Go package, and the changes are in the release notes. |
So i assume this can just be closed then? |
@bjartek yeah, I guess we can close this, given in it's current form it won't apply. We can definitely add such conversion functionality back to the code, but ideally in a well-defined (so it matches e.g. JS SDK) and strongly typed fashion (no any return type). |
Description
Started on suggesting new ToGo() values
master
branchFiles changed
in the Github PR explorer