diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebf2d3..1fd07a8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,14 +63,17 @@ Finish it on its branch: `Vendored from crossplane/crossplane` comment back above `syntax` with the new version. 2. Run `cargo run -p codegen` and commit the changes in `sdk/src/generated` - and `sdk/tests/generated`. A new field needs nothing else. -3. If codegen stops at a field kind it doesn't write yet, its error gives + and `sdk/tests/generated`. +3. Set each new field in the samples of `sdk/tests/generated_messages.rs`. + A sample lists every field of its message, so that file doesn't compile + until you do. +4. If codegen stops at a field kind it doesn't write yet, its error gives the field and the `#[prost(...)]` attribute. Add the kind to `codegen/src/messages.rs`, following what prost's derive generates for it. -4. If the update adds a message, `the_checks_cover_every_message_of_the_proto` +5. If the update adds a message, `the_checks_cover_every_message_of_the_proto` fails. Add the message to `for_every_message` and to `sample` in - `sdk/tests/generated_messages.rs`, and set new fields in the samples. -5. Run the checks above. + `sdk/tests/generated_messages.rs`. +6. Run the checks above. A new field on any message is a breaking change for this crate, because the message structs are public and can be built with a struct literal. Say so in diff --git a/codegen/src/messages.rs b/codegen/src/messages.rs index f63b9d9..c52228a 100644 --- a/codegen/src/messages.rs +++ b/codegen/src/messages.rs @@ -619,18 +619,22 @@ impl MapValue { mod tests { use super::*; + /// A message whose fields are declared out of tag order, with a oneof + /// that lists its highest tag first. On the wire the order is `tag` (1), + /// `source` (3), `kind` (4): the declaration order and a sort by a + /// oneof's first tag are both another order. const PROST: &str = r#" /// A message. #[allow(clippy::all)] #[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] pub struct Meta { + #[prost(enumeration = "Kind", optional, tag = "4")] + pub kind: ::core::option::Option, + #[prost(oneof = "meta::Source", tags = "6, 3")] + pub source: ::core::option::Option, /// Its tag. #[prost(string, tag = "1")] pub tag: ::prost::alloc::string::String, - #[prost(enumeration = "Kind", optional, tag = "2")] - pub kind: ::core::option::Option, - #[prost(oneof = "meta::Source", tags = "4, 3")] - pub source: ::core::option::Option, } /// Nested message and enum types in `Meta`. pub mod meta { @@ -638,7 +642,7 @@ pub mod meta { pub enum Source { #[prost(string, tag = "3")] Name(::prost::alloc::string::String), - #[prost(message, tag = "4")] + #[prost(message, tag = "6")] Inner(Inner), } #[derive(Clone, Copy, PartialEq, ::prost::Message)] @@ -681,7 +685,7 @@ pub enum Kind { source.contains("_ => self.unknown_fields.merge(tag, wire_type, buf, ctx),"), "{source}" ); - assert!(source.contains("4u32 | 3u32 => {"), "{source}"); + assert!(source.contains("6u32 | 3u32 => {"), "{source}"); assert!( source.contains("pub fn set_kind(&mut self, value: Kind) {"), "{source}" @@ -709,16 +713,29 @@ pub enum Kind { } #[test] - fn fields_are_encoded_in_tag_order() { + fn fields_are_encoded_in_tag_order_with_a_oneof_at_its_lowest_tag() { let source = rewrite(PROST).expect("rewrites").source; let encode = &source[source.find("fn encode_raw").expect("encode_raw")..]; let tag = encode.find("encode(1u32").expect("tag 1"); - let kind = encode.find("encode(2u32").expect("tag 2"); let oneof = encode.find("oneof.encode(buf)").expect("the oneof"); + let kind = encode.find("encode(4u32").expect("tag 4"); let unknown = encode .find("self.unknown_fields.encode(buf)") .expect("the unknown fields"); - assert!(tag < kind && kind < oneof && oneof < unknown); + assert!(tag < oneof && oneof < kind && kind < unknown, "{encode}"); + } + + #[test] + fn debug_prints_the_fields_as_declared() { + let source = rewrite(PROST).expect("rewrites").source; + let debug = &source[source.find("fn fmt").expect("the Debug impl")..]; + let kind = debug.find("stringify!(kind)").expect("kind"); + let oneof = debug.find("stringify!(source)").expect("source"); + let tag = debug.find("stringify!(tag)").expect("tag"); + let unknown = debug + .find("\"unknown_fields\"") + .expect("the unknown fields"); + assert!(kind < oneof && oneof < tag && tag < unknown, "{debug}"); } #[test]