-
Notifications
You must be signed in to change notification settings - Fork 190
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
Document
type requires multiple serialization / deserialization for usage with serde_json::Value
-based APIs
#2702
Comments
It would be really nice if Smithy codegen could use |
@thomas-k-cameron has done a lot of work to add The same pattern can be followed to also add them to We won't get to this any time soon, but you're welcome to contribute and work with @thomas-k-cameron to get this done. |
To clarify, this is more about the |
Since they are not making any guarantee, I don't know if it would be a good idea. |
Once RFC30 is implemented you should be able to do something like this. let doc = aws_smithy_types::Document::Object(HashMap::new());
let serde_json_value = serde_json::to_value(doc).unwrap();
let target_datatype = serde_json::from_value(serde_json_value).unwrap(); If your concern is for the UX then I think we could add a function like this. impl Document {
pub fn to_serde_json(&self) -> serde_json::Value {
// convert to Value
}
} |
My concern isn't about implementing I'm saying that 99.9% of the Rust ecosystem uses Converting between map types is not trivial. Users will need to pay a performance cost for arbitrarily large map conversions.
I'm saying IMO forcing one map type onto all users is not ideal. It would be nice to allow users to specify the type like I guess my concern also applies to Other than |
I understand the concern, but we don't want to depend on any Our concern is with maintainability. The SDK will have to be maintained without breaking changes for many years, so exposing separate libraries in the public API is a risk. |
Any thoughts on handling this with a generic and allowing the user to specify the concrete type? I'd think that would be the best of both worlds. |
Every data type that uses Document would need to accept generic parameters, so I'm pretty sure you would need to make a lot of changes to the code gen logic. Also, I'm not quite sure how maintain-able it is to introduce a new interface that needs to be implemented. I think it's more simpler/practical to do what the |
Why? I'm suggesting to do what This would give users to choice to use Document or Value or HashMap or anything else. |
I thought you meant something like this. enum Document<T: GenericMap = HashMap<String, Document>> I don't think I understand you correctly, would you mind giving me an example? |
That would be nice to have a default generic parameter to allow users to choose the kind of map, but when I said There are so many parallels to If I use a Smithy structure, I get a Smithy type which is a struct. If I use a Smithy enum, I get a Smithy type. If I choose String or Number or bool, however, I get a standard Rust type. I'm wondering if a Smithy model Document type could be more of a generic itself than a concrete type, and deserialize to a type I have defined in my own crate rather than one Smithy defined. This would allow users to choose the specific map implementation used and have the benefit of not being subject to the orphan rule. E.g. the Smithy model type is The route handler for the server, for example, would accept a function which accepts an argument of any type which implements Deserialize. Then, as a user, I can write my handler |
To phrase it slightly differently, the point of serde_json is to provide polymorphism over anything which can be serialized into or deserialized from JSON. The Value enum is only one form of this polymorphism. Generics are another, more powerful and flexible form. If Smithy is providing the enum, could it also provide the generic? |
This would be fixed with #2858 , which is what I was asking for and more 😄 |
@andrewbanchich Why does #2858 solve this? In #2858, server operation handlers would take in types that implement |
I had spoken with @hlbarber about this and it sounded like we'd be able to impl FromRequest for our custom types as well. Maybe I misunderstood. |
I don't think we should let users implement If we allow users to implement |
I think exposing this trait would be very beneficial. Smithy cannot serve as a source of truth for all validation rules for inputs, so currently our validation is split between our Smithy-generated types and our custom server code, with the server code performing the validation which Smithy cannot express. Because of this, the code we work with has no single source of truth for validation, which is not ideal. I would argue that the inverse is already true: Smithy allows applications to violate their "true" set of validation rules since some cannot be expressed by Smithy. In this context, I think it would be better to have a single source of truth defined by application code than to have two sources of truth. Exposing the trait would also mean we can avoid having a ton of code for converting between types, as well as running into the orphan rule when trying to add abstractions over Smithy types which don't exist. |
Because
Document
usesHashMap
for objects instead ofserde_json::Map
, there needs to be a second pass at converting thatHashMap
to aserde_json::Map
when using libraries which work withserde_json::Value
s, and then another for turning it back to aHashMap
/Document
for use in Smithy output types.serde_json::Value
is the standard for the Rust ecosystem, so I expect a lot of crates will need to do this.The text was updated successfully, but these errors were encountered: