loco-rs / loco

🚂 🦀 The one-person framework for Rust for side-projects and startups
https://loco.rs
Apache License 2.0
3.85k stars 163 forks source link

Simplify Javascript Object Notation Patch #452

Open yinho999 opened 6 months ago

yinho999 commented 6 months ago

Feature Request

Based on my understanding, users often find it challenging to implement JavaScript Object Notation Patch (JSON Patch) as outlined in RFC 6902 and RFC7396.

To facilitate this, we could integrate a crate specifically for handling UpdateParams in views, making it an optional feature. This approach ensures flexibility, as not all users may require JSON Patch functionality.

jondot commented 6 months ago

How would people use this? in controller? can you show a few examples maybe?

yinho999 commented 6 months ago

How would people use this? in controller? can you show a few examples maybe?

Yeah I think we can put that into DTO and I will provide some examples as soon as I am free 👍️

yinho999 commented 6 months ago

@jondot, here is a simple example of using the json patch crate.

However, as you can see, the Patch type is very generic and does not offer any type safety or restrictions. For instance, JSON Patch has an operation called "replace" that can be applied to any field of the user to change its value. This is what I use to update the user's email in the example. However, we can also apply different operations like "test", "remove", or "add" on the same field.

I suggest we could add operation-based functions similar to what Spring Boot did, to restrict operations and provide a simple API for users Spring Boot. Moreover we can also provide macros to serialize the Patch JSON into multiple operations DTOs to provide type safety.