Skip to content

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

Open
hasanaburayyan opened this issue Mar 13, 2023 · 9 comments
Open

Add support for fromJson() methods on container types #1796

hasanaburayyan opened this issue Mar 13, 2023 · 9 comments
Labels
🛠️ compiler Compiler ✨ enhancement New feature or request 🎨 sdk SDK

Comments

@hasanaburayyan
Copy link
Contributor

hasanaburayyan commented Mar 13, 2023

Community Note

Please vote by adding a 👍 reaction to the issue to help us prioritize.
If you are interested to work on this issue, please leave a comment.

Feature Spec

implemented for primitives in: #1768

https://docs.winglang.io/reference/spec#1146-schema-validation

let j = Json [1, 2, 3];
let a = Array<num>.fromJson(j);

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:

let myArray = Json [1,2,3,"hello"];
Array<num>.fromJson(myArray); // RUNTIME ERROR: unable to parse `[1,2,3,"hello"]` as an array of `num`.

Component

SDK

@hasanaburayyan hasanaburayyan added the ✨ enhancement New feature or request label Mar 13, 2023
@monadabot monadabot added this to Wing Mar 13, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New - not properly defined in Wing Mar 13, 2023
mergify bot pushed a commit that referenced this issue Mar 14, 2023
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)*.
@staycoolcall911 staycoolcall911 moved this from 🆕 New - not properly defined to 🤝 Backlog - handoff to owners in Wing Mar 14, 2023
@staycoolcall911
Copy link
Contributor

@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?

@github-actions
Copy link

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.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@github-actions github-actions bot added the Stale label May 15, 2023
@staycoolcall911 staycoolcall911 changed the title Add support for from_json() methods on container types Add support for fromJson() methods on container types May 15, 2023
@staycoolcall911 staycoolcall911 added 🎨 sdk SDK and removed Stale labels May 15, 2023
@skorfmann
Copy link
Contributor

Is there a workaround to get an Array object from a generic Json?

@hasanaburayyan
Copy link
Contributor Author

@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?

@staycoolcall911
Copy link
Contributor

@skorfmann I think you should be able to parse the json using extern js code.
Let me know if that doesn't work for you or if it turns out too ugly and we'll bump this issue to p1.

@skorfmann
Copy link
Contributor

@skorfmann I think you should be able to parse the json using extern js code. Let me know if that doesn't work for you or if it turns out too ugly and we'll bump this issue to p1.

Was just curious if there's a workaround in Wing itself. Not urgent, since doing this in JS land will do for now.

@staycoolcall911 staycoolcall911 moved this from Todo - p2 to Todo - p1 in Wing Language Roadmap Aug 16, 2023
@staycoolcall911 staycoolcall911 added this to the Containers milestone Sep 12, 2023
@hasanaburayyan hasanaburayyan self-assigned this Sep 18, 2023
@staycoolcall911 staycoolcall911 moved this from Todo - p1 to In Progress in Wing Language Roadmap Sep 18, 2023
@Chriscbr Chriscbr added this to the Winglang Stable Release milestone Feb 29, 2024
Copy link

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.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@github-actions github-actions bot added the Stale label May 31, 2024
@tsuf239
Copy link
Contributor

tsuf239 commented Aug 7, 2024

Another way to solve this will be to add tryAsArray, tryAsStruct,tryAsMap, tryAsMap macros to Json and mutJson (when the type is validated in the macro itself), it's definitely an easier approach, but will it be enough @Chriscbr ?

@boyney123
Copy link
Contributor

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 let a = Array<num>.fromJson(j);!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ compiler Compiler ✨ enhancement New feature or request 🎨 sdk SDK
Projects
Status: 🤝 Backlog - handoff to owners
Status: In Progress
Development

No branches or pull requests

6 participants