Implement #[serde(implied(key = "key", value = "value")] for adding static values
#2908
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
impliedAttributeIt would be useful to introduce an
impliedattribute inserde, which automatically adds a specific key-value pair during serialization and ensures that the key is present during deserialization. This would allow us to automatically include predefined keys (likemethodin the following example) in the serialized and deserialized data without manually specifying them in thestruct.Example:
{ "method": "Target.setDiscoverTargets", "id": "some-id", "params": { } }Expected Behavior
1. Serialization
When serializing an instance of
TargetSetDiscoverTargets,serdeshould automatically include the"method": "Target.setDiscoverTargets"key-value pair in the resulting JSON:2. Deserialization
During deserialization, serde should check if the
"method"key exists in the input and match it against the predefined value ("Target.setDiscoverTargets"). If it does not match or is missing,serdeshould raise an error.Alternatives Considered
Manually adding the method field during serialization and validating its existence during deserialization. This approach introduces repetitive code that could be automated with the implied attribute. This also means that the user will have to implement
SerializeandDeserializethemselves.This PR implements this attribute.