-
Notifications
You must be signed in to change notification settings - Fork 209
Add support for fromJson()
methods on container types
#1796
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
Comments
This PR introduces Json static methods. It required some creativity with the grammar to use the `Json` keyword for both static methods and instantiating Json objects. Closes: #1679 *note*: This PR does not include `from_json()` methods on container types. Need a better way to ensure type safety with separate issue: #1796 *By submitting this pull request, I confirm that my contribution is made under the terms of the [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
@hasanaburayyan - can you please add some implementation notes on why this is not trivial? I believe you said it has to do with type checking (for homogenous arrays, etc.), right? |
Hi, This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
from_json()
methods on container typesfromJson()
methods on container types
Is there a workaround to get an |
@skorfmann I cant think of a workaround off the top of my head. However I can probably wip up a solution for this using a macro real quick it might not be efficient or optimal but it would enable uses to extract container types from Json. @staycoolcall911 any thoughts? |
@skorfmann I think you should be able to parse the json using |
Was just curious if there's a workaround in Wing itself. Not urgent, since doing this in JS land will do for now. |
Hi, This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. |
Another way to solve this will be to add |
Not sure how much value it adds, but adding this here, I was trying to understand how to do this and got it working with the hacky workaround. pub inflight getFriends(id:str): MutArray<types.Friend>? {
let data = this.table.query(
KeyConditionExpression: "PK = :spaceID AND begins_with(SK, :friendId)",
ExpressionAttributeValues: {
":spaceID": "SPACE#{id}",
":friendId": "FRIEND_ID"
},
);
let friends = MutArray<types.Friend>[];
if (data.Count == 0) {
return friends;
}
for item in data.Items {
friends.push(types.Friend.fromJson(item));
}
return friends;
} I'm all for |
Community Note
Feature Spec
implemented for primitives in: #1768
https://docs.winglang.io/reference/spec#1146-schema-validation
Use Cases
Allows us to parse container types safely from Json at runtime.
Implementation Notes
The implementation here is not trivial as we need to ensure that in any given Json container type (ie. Map, Array) that the entries are homogenous and match the expected type. Our Json types allow for heterogenous arrays so just verifying a single index is not sufficient. There needs to be O(n) checks where n is the number of entries in the Json container.
Note: Should append example in language reference 1.1.4.5 to contain:
Component
SDK
The text was updated successfully, but these errors were encountered: