-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat(map): Codegen for Rust Target #72
Conversation
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
@martinhalford can you review please? |
rustValueType = mapValueType; | ||
} | ||
|
||
parameters.fileWriter.writeLine(1, `pub mut ${this.toValidRustName(field.getName())}: HashMap<${rustKeyType}, ${rustValueType}> = HashMap::new();`); |
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.
Since none of the other generated fields a mutable by default, I think you can keep this immutable too. I didn't realise at first that this was forming a struct definition, I don't think you can use mut
here anyway.
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.
Also, I think this should be wrapped up in an option, and you are not initializing this in the struct
definition, it's not valid Rust.
parameters.fileWriter.writeLine(1, `pub mut ${this.toValidRustName(field.getName())}: HashMap<${rustKeyType}, ${rustValueType}> = HashMap::new();`); | |
parameters.fileWriter.writeLine(1, `pub ${this.toValidRustName(field.getName())}: Option<HashMap<${rustKeyType}, ${rustValueType}>>,`); |
} else if (ModelUtil.isScalar(mapDeclaration.getKey())) { | ||
const scalarDeclaration = mapDeclaration.getModelFile().getType(mapDeclaration.getKey().getType()); | ||
const scalarType = scalarDeclaration.getType(); | ||
rustKeyType = this.toRustType(scalarType); |
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.
toRustType
definition is lacking Integer
conversion, here
concerto-codegen/lib/codegen/fromcto/rust/rustvisitor.js
Lines 289 to 303 in 41428ed
toRustType(type) { | |
switch (type) { | |
case 'DateTime': | |
return 'DateTime<Utc>'; | |
case 'Boolean': | |
return 'bool'; | |
case 'Long': | |
return 'u64'; | |
case 'Double': | |
return 'f64'; | |
default: { | |
return type; | |
} | |
} | |
} |
Integer
is one of the possibilities, here https://github.com/accordproject/concerto/blob/5b72c1951c4f1546cbc29a8a18e89d516efa74e3/packages/concerto-core/lib/introspect/scalardeclaration.js#L61-L75
I think we can convert Integer
to u32
.
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.
Also checked DataTime<Utc>
from the chronos
library, it is hashable. So we are safe there.
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.
JavaScript uses double-precision floating-point format, which is a 64-bit binary format defined by the IEEE 754-2008 standard. I think f64
is better suited?
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 would you map Integer
to a floating point format?
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.
It seems counter-intuitive, my line of thinking is there will be no loss of precision when working with JS 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.
I have two counterpoints for using f64
. First: I would be very surprised if I see a float number when my concerto type is an integer. Especially the point of code generation is not JS compatibility.
More importantly, the second issue is that f64
does not implement hash. So it would break for keys.
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 the 3rd one is that for Long
we are converting them to u64, which should have been u128 anyway, but for integers it could at least be i64
. I don't know, maybe @martinhalford can weight in.
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.
Concerto documentation dictates that Integers should be i32
and longs i64
https://concerto.accordproject.org/docs/design/specification/model-properties#:~:text=Integer,64%20bit%20signed%20whole%20number. so existing conversion of Long
is also wrong?
|
||
// Key | ||
if (ModelUtil.isPrimitiveType(mapKeyType)) { | ||
rustKeyType = this.toRustType(mapKeyType); |
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 comment on the scalars goes for primitive types too. https://github.com/accordproject/concerto/blob/5b72c1951c4f1546cbc29a8a18e89d516efa74e3/packages/concerto-core/lib/modelutil.js#L159-L162
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.
@ ekarademir any chance you can get this PR over the line so we can merge?
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.
@ ekarademir any chance you can get this PR over the line so we can merge?
Definitely!
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.
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
Signed-off-by: Jonathan Casey <[email protected]>
@dselman - apologies for the delayed response. Will do. Thanks @jonathan-casey for doing this. |
Description
Adds Rust code generation for Map Type.
Sample <String, String>
Sample <Scalar String, Scalar>
Sample <Scalar DateTime, Person>
Author Checklist
--signoff
option of git commit.main
fromfork:branchname