Skip to content

Commit 627e2e6

Browse files
committed
feat(derive): add unit test and documentation
1 parent 29d5ece commit 627e2e6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,41 @@ pub enum Gender {
457457
}
458458
```
459459

460+
### Skipping Fields of Existing Types
461+
You may skip encoding or decoding fields in existing Rust types using the `skip`
462+
attribute. This will prevent the field from being encoded or decoded as part of
463+
the Protobuf message but otherwise allow the field to be part of the Rust type.
464+
465+
```rust,ignore
466+
use prost;
467+
use prost::Message;
468+
469+
#[derive(Clone, PartialEq, Message)]
470+
struct Person {
471+
#[prost(string, tag = "1")]
472+
pub id: String,
473+
#[prost(skip)]
474+
pub temp_data: String, // This field will be skipped
475+
}
476+
```
477+
478+
If the skipped field type does not implement `Default`, you must provide a
479+
default value for the field using the `default` attribute.
480+
481+
```rust,ignore
482+
use prost;
483+
use prost::Message;
484+
use std::collections::HashMap;
485+
486+
#[derive(Clone, PartialEq, Message)]
487+
struct Person {
488+
#[prost(string, tag = "1")]
489+
pub id: String,
490+
#[prost(skip, default = "HashMap::new")]
491+
pub temp_data: HashMap<String, String>, // This field will be skipped
492+
}
493+
```
494+
460495
## Nix
461496

462497
The prost project maintains flakes support for local development. Once you have

tests/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub mod oneof_attributes {
107107

108108
#[cfg(test)]
109109
mod proto3_presence;
110+
#[cfg(test)]
111+
mod skipped_fields;
110112

111113
use core::fmt::Debug;
112114

tests/src/skipped_fields.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Tests for skipping fields when using prost-derive.
2+
3+
use crate::alloc::string::ToString;
4+
use crate::check_serialize_equivalent;
5+
use alloc::collections::BTreeMap;
6+
use alloc::string::String;
7+
use prost::Message;
8+
9+
/// A struct with the same data as another, but with a skipped field, should be equal when encoded.
10+
#[test]
11+
fn skipped_field_serial_equality() {
12+
#[derive(Clone, PartialEq, prost::Message)]
13+
struct TypeWithoutSkippedField {
14+
#[prost(string, tag = "1")]
15+
value: String,
16+
}
17+
18+
fn create_hashmap() -> BTreeMap<String, String> {
19+
let mut map = BTreeMap::new();
20+
map.insert("key".to_string(), "value".to_string());
21+
map
22+
}
23+
24+
#[derive(Clone, PartialEq, prost::Message)]
25+
struct TypeWithSkippedField {
26+
#[prost(string, tag = "1")]
27+
value: String,
28+
#[prost(skip, default = "create_hashmap")]
29+
pub temp_data: BTreeMap<String, String>, // This field will be skipped
30+
}
31+
32+
let a = TypeWithoutSkippedField {
33+
value: "hello".to_string(),
34+
};
35+
let b = TypeWithSkippedField {
36+
value: "hello".to_string(),
37+
temp_data: create_hashmap(),
38+
};
39+
40+
// Encoded forms should be equal
41+
check_serialize_equivalent(&a, &b);
42+
43+
// Decoded forms should be equal, with the skipped field initialized using the default attribute
44+
let decoded = TypeWithSkippedField::decode(a.encode_to_vec().as_slice()).unwrap();
45+
assert_eq!(b, decoded);
46+
}

0 commit comments

Comments
 (0)