diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d2f64a8..79cde21 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,6 +25,11 @@ cargo clippy --workspace --tests cargo run -p codegen ``` +The `State` and `Resource` messages are written by hand in +`sdk/src/proto/state.rs` so that they keep unknown fields. When an update of +the vendored proto changes either message, update that file to match; a test +fails until you do. + ## Submitting changes * Open an issue first for anything beyond a small fix, so we can agree on the diff --git a/Cargo.lock b/Cargo.lock index aa7a6d4..3b0702e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -405,6 +405,7 @@ dependencies = [ "pbjson-types", "prometheus-client", "prost", + "prost-types", "rustls", "serde", "serde_json", @@ -897,7 +898,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0" dependencies = [ "proc-macro2", - "syn 3.0.4", + "syn 3.0.10", ] [[package]] @@ -959,7 +960,7 @@ dependencies = [ "pulldown-cmark", "pulldown-cmark-to-cmark", "regex", - "syn 3.0.4", + "syn 3.0.10", "tempfile", ] diff --git a/README.md b/README.md index 6231c61..088bade 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,12 @@ with a few deliberate differences: `Server::ready`: until it completes, the server reports NOT_SERVING and answers RunFunction with UNAVAILABLE, and if it fails, the server stops with `Error::NotReady`. +- **Unknown fields are kept on `State` and `Resource` only.** protobuf-go + keeps fields it does not know on every message. This SDK keeps them on the + two messages `response::to` copies from the request, so a field a newer + Crossplane adds to them passes through a function unchanged. + As in Go, replacing a desired resource instead of modifying it in place + drops that resource's unknown fields. - **Graceful shutdown is built in.** The server drains in-flight requests on SIGTERM and SIGINT. - **Metrics are Go-compatible, served as OpenMetrics first.** Like diff --git a/codegen/src/main.rs b/codegen/src/main.rs index c982d13..60fa7c2 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -11,6 +11,11 @@ const PROTO: &str = "sdk/proto/v1/run_function.proto"; const INCLUDE: &str = "sdk/proto"; const OUT: &str = "sdk/src/generated"; +/// Messages written by hand in `sdk/src/proto/state.rs` so that they keep +/// unknown fields, which prost-generated messages discard. +const HAND_WRITTEN: [&str; 2] = ["State", "Resource"]; +const UNKNOWN_FIELDS_INIT: &str = "unknown_fields: Default::default(),"; + fn main() -> Result<(), Box> { let root = workspace_root(); let tmp = tempfile::tempdir()?; @@ -21,6 +26,12 @@ fn main() -> Result<(), Box> { .file_descriptor_set_path(&descriptor) .compile_well_known_types() .extern_path(".google.protobuf", "::pbjson_types"); + for message in HAND_WRITTEN { + config.extern_path( + format!(".apiextensions.fn.proto.v1.{message}"), + format!("crate::proto::state::{message}"), + ); + } tonic_prost_build::configure() .build_client(true) @@ -49,7 +60,12 @@ fn main() -> Result<(), Box> { } else { continue; }; - fs::copy(&path, out.join(target))?; + if target == "v1.serde.rs" { + let serde = init_unknown_fields(&fs::read_to_string(&path)?)?; + fs::write(out.join(target), serde)?; + } else { + fs::copy(&path, out.join(target))?; + } println!("wrote {}", out.join(target).display()); } fs::copy(&descriptor, out.join("fileset.bin"))?; @@ -57,9 +73,65 @@ fn main() -> Result<(), Box> { Ok(()) } +/// Makes the generated serde constructors of the hand-written messages +/// initialize `unknown_fields`, a field the descriptor does not have. +fn init_unknown_fields(serde: &str) -> Result> { + let mut patched = serde.to_owned(); + for message in HAND_WRITTEN { + let constructor = format!("Ok({message} {{\n"); + if patched.matches(&constructor).count() != 1 { + return Err(format!("expected one serde constructor for {message}").into()); + } + let start = patched.find(&constructor).expect("counted above"); + let end = start + + patched[start..] + .find("})") + .ok_or_else(|| format!("unterminated serde constructor for {message}"))?; + let line_start = patched[..end].rfind('\n').map_or(0, |i| i + 1); + let indent = &patched[line_start..end]; + let init = format!("{indent} {UNKNOWN_FIELDS_INIT}\n"); + patched.insert_str(line_start, &init); + } + Ok(patched) +} + fn workspace_root() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) .parent() .expect("codegen crate lives one level below the workspace root") .to_path_buf() } + +#[cfg(test)] +mod tests { + use super::*; + + const GENERATED: &str = "\ + Ok(Resource { + ready: ready__.unwrap_or_default(), + }) + Ok(State { + composite: composite__, + }) +"; + + #[test] + fn init_unknown_fields_extends_both_constructors() { + let want = "\ + Ok(Resource { + ready: ready__.unwrap_or_default(), + unknown_fields: Default::default(), + }) + Ok(State { + composite: composite__, + unknown_fields: Default::default(), + }) +"; + assert_eq!(init_unknown_fields(GENERATED).expect("patches"), want); + } + + #[test] + fn init_unknown_fields_fails_without_a_constructor() { + assert!(init_unknown_fields("Ok(State {\n})").is_err()); + } +} diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index dd53260..73ce207 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -35,4 +35,5 @@ thiserror = "2" rustls = { version = "0.23.45", default-features = false } [dev-dependencies] +prost-types = "0.14" tokio-test = "0.4" diff --git a/sdk/src/generated/v1.rs b/sdk/src/generated/v1.rs index ecb7fc0..b8bc1d0 100644 --- a/sdk/src/generated/v1.rs +++ b/sdk/src/generated/v1.rs @@ -10,7 +10,7 @@ pub struct RunFunctionRequest { /// to each function is fresh as of the time the pipeline was invoked, not as /// of the time each function was invoked. #[prost(message, optional, tag = "2")] - pub observed: ::core::option::Option, + pub observed: ::core::option::Option, /// Desired state according to a function pipeline. The state passed to a /// particular function may have been accumulated by previous functions in the /// pipeline. @@ -22,7 +22,7 @@ pub struct RunFunctionRequest { /// as desired before will result in them being deleted from the objects in the /// cluster. #[prost(message, optional, tag = "3")] - pub desired: ::core::option::Option, + pub desired: ::core::option::Option, /// Optional input specific to this function invocation. A JSON representation /// of the 'input' block of the relevant entry in a function pipeline. #[prost(message, optional, tag = "4")] @@ -111,7 +111,7 @@ pub struct CredentialData { #[derive(Clone, PartialEq, ::prost::Message)] pub struct Resources { #[prost(message, repeated, tag = "1")] - pub items: ::prost::alloc::vec::Vec, + pub items: ::prost::alloc::vec::Vec, } /// A RunFunctionResponse contains the result of a function run. #[allow(clippy::all)] @@ -132,7 +132,7 @@ pub struct RunFunctionResponse { /// as desired before will result in them being deleted from the objects in the /// cluster. #[prost(message, optional, tag = "2")] - pub desired: ::core::option::Option, + pub desired: ::core::option::Option, /// Results of the function run. Results are used for observability purposes. #[prost(message, repeated, tag = "3")] pub results: ::prost::alloc::vec::Vec, @@ -275,75 +275,6 @@ pub struct ResponseMeta { #[prost(message, optional, tag = "2")] pub ttl: ::core::option::Option<::pbjson_types::Duration>, } -/// State of the XR (XR) and any resources. -#[allow(clippy::all)] -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct State { - /// The state of the XR (XR). - #[prost(message, optional, tag = "1")] - pub composite: ::core::option::Option, - /// The state of any other resources. In composition functions these are the - /// composed resources. In operation functions they're arbitrary resources that - /// the operation wants to create or update. - #[prost(map = "string, message", tag = "2")] - pub resources: ::std::collections::HashMap<::prost::alloc::string::String, Resource>, -} -/// A Resource represents the state of a Kubernetes resource. -#[allow(clippy::all)] -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct Resource { - /// The JSON representation of the resource. - /// - /// * Crossplane will set this field in a RunFunctionRequest to the entire - /// observed state of a resource - including its metadata, spec, and status. - /// - /// * A function should set this field in a RunFunctionRequest to communicate - /// the desired state of the resource. - /// - /// * A function may only specify the desired status of a XR - not its metadata - /// or spec. A function should not return desired metadata or spec for a XR. - /// This will be ignored. - /// - /// * A function may not specify the desired status of any other resource - - /// e.g. composed resources. It may only specify their metadata and spec. - /// Status will be ignored. - #[prost(message, optional, tag = "1")] - pub resource: ::core::option::Option<::pbjson_types::Struct>, - /// The resource's connection details. - /// - /// * Crossplane will set this field in a RunFunctionRequest to communicate the - /// the observed connection details of a composite or composed resource. - /// - /// * A function should set this field in a RunFunctionResponse to indicate the - /// desired connection details of legacy XRs. For modern XRs, this will be ignored. - /// - /// * A function should not set this field in a RunFunctionResponse to indicate - /// the desired connection details of a composed resource. This will be - /// ignored. - /// - /// Connection details are only used for composition. They're ignored by - /// Operations. - #[prost(map = "string, bytes", tag = "2")] - pub connection_details: ::std::collections::HashMap< - ::prost::alloc::string::String, - ::prost::alloc::vec::Vec, - >, - /// Ready indicates whether the resource should be considered ready. - /// - /// * Crossplane will never set this field in a RunFunctionRequest. - /// - /// * A function should set this field to READY_TRUE in a RunFunctionResponse - /// to indicate that a desired resource is ready. - /// - /// * A function should set this field to READY_TRUE in a RunFunctionResponse - /// to indicate that a desired XR is ready. This overwrites the standard - /// readiness detection that determines the ready state of the composite by the - /// ready state of the composed resources. - /// - /// Ready is only used for composition. It's ignored by Operations. - #[prost(enumeration = "Ready", tag = "3")] - pub ready: i32, -} /// A Result of running a function. #[allow(clippy::all)] #[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] diff --git a/sdk/src/generated/v1.serde.rs b/sdk/src/generated/v1.serde.rs index 6e2c7d0..bb06a17 100644 --- a/sdk/src/generated/v1.serde.rs +++ b/sdk/src/generated/v1.serde.rs @@ -975,6 +975,7 @@ impl<'de> serde::Deserialize<'de> for Resource { resource: resource__, connection_details: connection_details__.unwrap_or_default(), ready: ready__.unwrap_or_default(), + unknown_fields: Default::default(), }) } } @@ -2301,6 +2302,7 @@ impl<'de> serde::Deserialize<'de> for State { Ok(State { composite: composite__, resources: resources__.unwrap_or_default(), + unknown_fields: Default::default(), }) } } diff --git a/sdk/src/proto.rs b/sdk/src/proto.rs index 887609e..f6a882c 100644 --- a/sdk/src/proto.rs +++ b/sdk/src/proto.rs @@ -2,10 +2,17 @@ //! `apiextensions.fn.proto.v1` FunctionRunnerService API. //! //! Regenerate with `cargo run -p codegen` after changing the vendored proto. +//! `State` and `Resource` are written by hand in `proto/state.rs` to keep +//! unknown fields; update them when the proto changes either message. + +mod state; + +pub use state::UnknownFields; /// Types and the gRPC service for the v1 FunctionRunnerService API. pub mod v1 { #![allow(clippy::all, clippy::pedantic)] + pub use super::state::{Resource, State}; include!("generated/v1.rs"); include!("generated/v1.serde.rs"); } diff --git a/sdk/src/proto/state.rs b/sdk/src/proto/state.rs new file mode 100644 index 0000000..0952025 --- /dev/null +++ b/sdk/src/proto/state.rs @@ -0,0 +1,323 @@ +//! The messages a function carries forward from request to response. +//! +//! prost discards fields it does not know. [`State`] and [`Resource`] are +//! written by hand instead of generated so that fields a newer Crossplane +//! adds to them survive a function built against an older proto, as they do +//! in function-sdk-go. Known fields are decoded and encoded as prost +//! generates it for these messages. + +use std::collections::HashMap; +use std::fmt; + +use prost::bytes::{Buf, BufMut}; +use prost::encoding::{self, DecodeContext, WireType}; +use prost::{DecodeError, Message}; + +use super::v1::Ready; + +/// Fields of a message that this SDK's proto does not know. +/// +/// Crossplane may add fields to a message after this SDK was built. They +/// are kept when a message is decoded and written back when it is encoded, +/// so a function passes them down the pipeline untouched. They cannot be +/// read or modified, and they have no JSON representation. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct UnknownFields(Vec); + +impl UnknownFields { + fn merge( + &mut self, + tag: u32, + wire_type: WireType, + buf: &mut impl Buf, + ctx: DecodeContext, + ) -> Result<(), DecodeError> { + let out = &mut self.0; + match wire_type { + WireType::Varint => { + let mut value = 0u64; + encoding::uint64::merge(wire_type, &mut value, buf, ctx)?; + encoding::uint64::encode(tag, &value, out); + } + WireType::SixtyFourBit => { + let mut value = 0u64; + encoding::fixed64::merge(wire_type, &mut value, buf, ctx)?; + encoding::fixed64::encode(tag, &value, out); + } + WireType::ThirtyTwoBit => { + let mut value = 0u32; + encoding::fixed32::merge(wire_type, &mut value, buf, ctx)?; + encoding::fixed32::encode(tag, &value, out); + } + WireType::LengthDelimited => { + let mut value = Vec::::new(); + encoding::bytes::merge(wire_type, &mut value, buf, ctx)?; + encoding::bytes::encode(tag, &value, out); + } + // Groups are a proto2 feature that the proto3 function API + // cannot use, so they are skipped as prost skips them. + WireType::StartGroup | WireType::EndGroup => { + encoding::skip_field(wire_type, tag, buf, ctx)?; + } + } + Ok(()) + } + + fn encode(&self, buf: &mut impl BufMut) { + buf.put_slice(&self.0); + } + + fn encoded_len(&self) -> usize { + self.0.len() + } +} + +fn in_field(message: &'static str, field: &'static str) -> impl FnOnce(DecodeError) -> DecodeError { + move |mut error| { + error.push(message, field); + error + } +} + +/// State of the XR (XR) and any resources. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct State { + /// The state of the XR (XR). + pub composite: Option, + /// The state of any other resources. In composition functions these are the + /// composed resources. In operation functions they're arbitrary resources that + /// the operation wants to create or update. + pub resources: HashMap, + /// Fields this SDK's proto does not know, kept and re-encoded. + pub unknown_fields: UnknownFields, +} + +impl Message for State { + fn encode_raw(&self, buf: &mut impl BufMut) { + if let Some(composite) = &self.composite { + encoding::message::encode(1, composite, buf); + } + encoding::hash_map::encode( + encoding::string::encode, + encoding::string::encoded_len, + encoding::message::encode, + encoding::message::encoded_len, + 2, + &self.resources, + buf, + ); + self.unknown_fields.encode(buf); + } + + fn merge_field( + &mut self, + tag: u32, + wire_type: WireType, + buf: &mut impl Buf, + ctx: DecodeContext, + ) -> Result<(), DecodeError> { + match tag { + 1 => encoding::message::merge( + wire_type, + self.composite.get_or_insert_with(Default::default), + buf, + ctx, + ) + .map_err(in_field("State", "composite")), + 2 => encoding::check_wire_type(WireType::LengthDelimited, wire_type) + .and_then(|()| { + encoding::hash_map::merge( + encoding::string::merge, + encoding::message::merge, + &mut self.resources, + buf, + ctx, + ) + }) + .map_err(in_field("State", "resources")), + _ => self.unknown_fields.merge(tag, wire_type, buf, ctx), + } + } + + fn encoded_len(&self) -> usize { + self.composite + .as_ref() + .map_or(0, |composite| encoding::message::encoded_len(1, composite)) + + encoding::hash_map::encoded_len( + encoding::string::encoded_len, + encoding::message::encoded_len, + 2, + &self.resources, + ) + + self.unknown_fields.encoded_len() + } + + fn clear(&mut self) { + *self = Self::default(); + } +} + +/// A Resource represents the state of a Kubernetes resource. +#[derive(Clone, Default, PartialEq)] +pub struct Resource { + /// The JSON representation of the resource. + /// + /// * Crossplane will set this field in a RunFunctionRequest to the entire + /// observed state of a resource - including its metadata, spec, and status. + /// + /// * A function should set this field in a RunFunctionRequest to communicate + /// the desired state of the resource. + /// + /// * A function may only specify the desired status of a XR - not its metadata + /// or spec. A function should not return desired metadata or spec for a XR. + /// This will be ignored. + /// + /// * A function may not specify the desired status of any other resource - + /// e.g. composed resources. It may only specify their metadata and spec. + /// Status will be ignored. + pub resource: Option, + /// The resource's connection details. + /// + /// * Crossplane will set this field in a RunFunctionRequest to communicate the + /// the observed connection details of a composite or composed resource. + /// + /// * A function should set this field in a RunFunctionResponse to indicate the + /// desired connection details of legacy XRs. For modern XRs, this will be ignored. + /// + /// * A function should not set this field in a RunFunctionResponse to indicate + /// the desired connection details of a composed resource. This will be + /// ignored. + /// + /// Connection details are only used for composition. They're ignored by + /// Operations. + pub connection_details: HashMap>, + /// Ready indicates whether the resource should be considered ready. + /// + /// * Crossplane will never set this field in a RunFunctionRequest. + /// + /// * A function should set this field to READY_TRUE in a RunFunctionResponse + /// to indicate that a desired resource is ready. + /// + /// * A function should set this field to READY_TRUE in a RunFunctionResponse + /// to indicate that a desired XR is ready. This overwrites the standard + /// readiness detection that determines the ready state of the composite by the + /// ready state of the composed resources. + /// + /// Ready is only used for composition. It's ignored by Operations. + pub ready: i32, + /// Fields this SDK's proto does not know, kept and re-encoded. + pub unknown_fields: UnknownFields, +} + +impl Resource { + /// Returns the enum value of `ready`, or the default if the field is set + /// to an invalid enum value. + pub fn ready(&self) -> Ready { + Ready::try_from(self.ready).unwrap_or_default() + } + + /// Sets `ready` to the provided enum value. + pub fn set_ready(&mut self, value: Ready) { + self.ready = value as i32; + } +} + +/// Prints `ready` as prost does: by variant name, or as a number when the +/// value is not a known variant. +struct ReadyDebug(i32); + +impl fmt::Debug for ReadyDebug { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match Ready::try_from(self.0) { + Ok(ready) => fmt::Debug::fmt(&ready, f), + Err(_) => fmt::Debug::fmt(&self.0, f), + } + } +} + +impl fmt::Debug for Resource { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Resource") + .field("resource", &self.resource) + .field("connection_details", &self.connection_details) + .field("ready", &ReadyDebug(self.ready)) + .field("unknown_fields", &self.unknown_fields) + .finish() + } +} + +impl Message for Resource { + fn encode_raw(&self, buf: &mut impl BufMut) { + if let Some(resource) = &self.resource { + encoding::message::encode(1, resource, buf); + } + encoding::hash_map::encode( + encoding::string::encode, + encoding::string::encoded_len, + encoding::bytes::encode, + encoding::bytes::encoded_len, + 2, + &self.connection_details, + buf, + ); + if self.ready != Ready::default() as i32 { + encoding::int32::encode(3, &self.ready, buf); + } + self.unknown_fields.encode(buf); + } + + fn merge_field( + &mut self, + tag: u32, + wire_type: WireType, + buf: &mut impl Buf, + ctx: DecodeContext, + ) -> Result<(), DecodeError> { + match tag { + 1 => encoding::message::merge( + wire_type, + self.resource.get_or_insert_with(Default::default), + buf, + ctx, + ) + .map_err(in_field("Resource", "resource")), + 2 => encoding::check_wire_type(WireType::LengthDelimited, wire_type) + .and_then(|()| { + encoding::hash_map::merge( + encoding::string::merge, + encoding::bytes::merge, + &mut self.connection_details, + buf, + ctx, + ) + }) + .map_err(in_field("Resource", "connection_details")), + 3 => encoding::int32::merge(wire_type, &mut self.ready, buf, ctx) + .map_err(in_field("Resource", "ready")), + _ => self.unknown_fields.merge(tag, wire_type, buf, ctx), + } + } + + fn encoded_len(&self) -> usize { + let ready = if self.ready != Ready::default() as i32 { + encoding::int32::encoded_len(3, &self.ready) + } else { + 0 + }; + self.resource + .as_ref() + .map_or(0, |resource| encoding::message::encoded_len(1, resource)) + + encoding::hash_map::encoded_len( + encoding::string::encoded_len, + encoding::bytes::encoded_len, + 2, + &self.connection_details, + ) + + ready + + self.unknown_fields.encoded_len() + } + + fn clear(&mut self) { + *self = Self::default(); + } +} diff --git a/sdk/src/response.rs b/sdk/src/response.rs index 4c7e1f9..4bba2a2 100644 --- a/sdk/src/response.rs +++ b/sdk/src/response.rs @@ -19,6 +19,8 @@ pub const DEFAULT_TTL: Duration = Duration::from_secs(60); /// Creates a response to the supplied request. /// /// The request's tag, desired state, and context are copied to the response. +/// The copy includes fields a newer Crossplane added to the desired state +/// that this SDK does not know, see [`crate::proto::UnknownFields`]. /// Crossplane deletes any previously desired field or resource that is not /// copied forward, so always start from this function rather than from an /// empty response. diff --git a/sdk/tests/unknown_fields.rs b/sdk/tests/unknown_fields.rs new file mode 100644 index 0000000..cf852cd --- /dev/null +++ b/sdk/tests/unknown_fields.rs @@ -0,0 +1,356 @@ +use std::collections::HashMap; + +use function_sdk_rust::proto::v1::{ + Ready, Resource, RunFunctionRequest, RunFunctionResponse, State, +}; +use function_sdk_rust::proto::{FILE_DESCRIPTOR_SET, UnknownFields}; +use function_sdk_rust::{resource, response}; +use prost::Message; +use prost::encoding; +use prost_types::FileDescriptorSet; +use prost_types::field_descriptor_proto::Type; + +/// What prost generates for `State` from the vendored proto. +#[derive(Clone, PartialEq, Message)] +struct GeneratedState { + #[prost(message, optional, tag = "1")] + composite: Option, + #[prost(map = "string, message", tag = "2")] + resources: HashMap, +} + +/// What prost generates for `Resource` from the vendored proto. +#[derive(Clone, PartialEq, Message)] +struct GeneratedResource { + #[prost(message, optional, tag = "1")] + resource: Option, + #[prost(map = "string, bytes", tag = "2")] + connection_details: HashMap>, + #[prost(enumeration = "Ready", tag = "3")] + ready: i32, +} + +/// Every map in the fixtures has one entry, because prost encodes maps in +/// hash order and the tests compare encoded bytes. +fn bucket() -> pbjson_types::Struct { + let json = serde_json::json!({"spec": {"forProvider": {"region": "eu-central-1"}}}); + resource::json_to_struct(json.as_object().expect("the fixture is an object")) +} + +fn known_resource() -> Resource { + Resource { + resource: Some(bucket()), + connection_details: HashMap::from([("password".to_string(), b"secret".to_vec())]), + ready: Ready::True as i32, + ..Default::default() + } +} + +fn known_state() -> State { + State { + composite: Some(known_resource()), + resources: HashMap::from([("bucket".to_string(), known_resource())]), + ..Default::default() + } +} + +/// Encodes one field of every wire type proto3 can produce, using field +/// numbers no message of the function API uses. +fn fields_from_a_newer_crossplane() -> Vec { + let mut buf = Vec::new(); + encoding::uint64::encode(100, &u64::MAX, &mut buf); + encoding::fixed64::encode(101, &7, &mut buf); + encoding::fixed32::encode(102, &9, &mut buf); + encoding::string::encode(103, &"from-the-future".to_string(), &mut buf); + buf +} + +fn with_unknown_fields(message: &impl Message) -> Vec { + let mut buf = message.encode_to_vec(); + buf.extend(fields_from_a_newer_crossplane()); + buf +} + +/// A desired State as a newer Crossplane might send it, with fields this +/// SDK does not know on the State and on its one composed resource. +fn newer_state() -> Vec { + let mut state = Vec::new(); + encoding::hash_map::encode( + encoding::string::encode, + encoding::string::encoded_len, + encoding::bytes::encode, + encoding::bytes::encoded_len, + 2, + &HashMap::from([("bucket".to_string(), with_unknown_fields(&known_resource()))]), + &mut state, + ); + state.extend(fields_from_a_newer_crossplane()); + state +} + +fn request_with_desired(state: &[u8]) -> RunFunctionRequest { + let mut req = Vec::new(); + encoding::bytes::encode(3, &state.to_vec(), &mut req); + RunFunctionRequest::decode(req.as_slice()).expect("the request decodes") +} + +fn desired_bytes(rsp: &RunFunctionResponse) -> Vec { + rsp.desired + .as_ref() + .expect("the response has desired state") + .encode_to_vec() +} + +#[test] +fn known_fields_encode_as_prost_generates_them() { + let state = known_state(); + let generated = GeneratedState::decode(state.encode_to_vec().as_slice()) + .expect("the generated message decodes hand-written output"); + + assert_eq!(state.encode_to_vec(), generated.encode_to_vec()); + assert_eq!(state.encoded_len(), generated.encoded_len()); + assert_eq!( + State::decode(generated.encode_to_vec().as_slice()).expect("decodes"), + state + ); +} + +#[test] +fn default_messages_encode_to_nothing() { + assert!(State::default().encode_to_vec().is_empty()); + assert!(Resource::default().encode_to_vec().is_empty()); +} + +#[test] +fn unknown_fields_round_trip_byte_for_byte() { + let resource = with_unknown_fields(&known_resource()); + let decoded = Resource::decode(resource.as_slice()).expect("decodes"); + assert_ne!(decoded.unknown_fields, UnknownFields::default()); + assert_eq!(decoded.encoded_len(), resource.len()); + assert_eq!(decoded.encode_to_vec(), resource); + + let state = newer_state(); + let decoded = State::decode(state.as_slice()).expect("decodes"); + assert_ne!(decoded.unknown_fields, UnknownFields::default()); + assert_eq!(decoded.encoded_len(), state.len()); + assert_eq!(decoded.encode_to_vec(), state); +} + +#[test] +fn unknown_fields_do_not_disturb_known_fields() { + let decoded = + Resource::decode(with_unknown_fields(&known_resource()).as_slice()).expect("decodes"); + let known = Resource { + unknown_fields: UnknownFields::default(), + ..decoded + }; + assert_eq!(known, known_resource()); +} + +#[test] +fn response_to_keeps_unknown_fields_of_desired_state() { + let req = request_with_desired(&newer_state()); + let rsp = response::to(&req, response::DEFAULT_TTL); + let rsp = RunFunctionResponse::decode(rsp.encode_to_vec().as_slice()).expect("decodes"); + + assert_eq!(desired_bytes(&rsp), newer_state()); +} + +#[test] +fn modifying_a_resource_in_place_keeps_its_unknown_fields() { + let req = request_with_desired(&newer_state()); + let mut rsp = response::to(&req, response::DEFAULT_TTL); + resource::set_ready(response::desired_composed(&mut rsp, "bucket"), Ready::False); + + let desired = State::decode(desired_bytes(&rsp).as_slice()).expect("decodes"); + let bucket = &desired.resources["bucket"]; + assert_eq!(bucket.ready(), Ready::False); + assert_ne!(bucket.unknown_fields, UnknownFields::default()); + assert_ne!(desired.unknown_fields, UnknownFields::default()); +} + +#[test] +fn replacing_a_resource_drops_its_unknown_fields() { + let req = request_with_desired(&newer_state()); + let mut rsp = response::to(&req, response::DEFAULT_TTL); + rsp.desired + .as_mut() + .expect("the response has desired state") + .resources + .insert("bucket".to_string(), known_resource()); + + let desired = State::decode(desired_bytes(&rsp).as_slice()).expect("decodes"); + assert_eq!(desired.resources["bucket"], known_resource()); + assert_ne!(desired.unknown_fields, UnknownFields::default()); +} + +#[test] +fn a_truncated_unknown_field_fails_to_decode() { + let mut resource = with_unknown_fields(&known_resource()); + resource.truncate(resource.len() - 1); + assert!(Resource::decode(resource.as_slice()).is_err()); +} + +#[test] +fn a_known_field_with_the_wrong_wire_type_fails_to_decode() { + let mut resource = Vec::new(); + encoding::fixed64::encode(3, &1, &mut resource); + let error = Resource::decode(resource.as_slice()).expect_err("ready is a varint"); + assert!(error.to_string().contains("Resource.ready"), "{error}"); +} + +/// Feeds both messages every known field number with every wire type and a +/// few payloads, and expects the same verdict as the messages prost +/// generates. The generated messages drop unknown fields, so decoding the +/// hand-written output with them compares the known fields alone. +#[test] +fn malformed_input_decodes_as_prost_generates_it() { + let payloads: [&[u8]; 6] = [ + &[], + &[0x00], + &[0x01], + &[0x04, 0x0a, 0x02, b'h', b'i'], + &[0x00, 0x00, 0x00, 0x00], + &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], + ]; + for tag in 1..=4u8 { + for wire_type in 0..=5u8 { + for payload in payloads { + let mut input = vec![tag << 3 | wire_type]; + input.extend(payload); + + let state = State::decode(input.as_slice()); + let generated = GeneratedState::decode(input.as_slice()); + assert_eq!(state.is_ok(), generated.is_ok(), "State {input:?}"); + if let (Ok(state), Ok(generated)) = (state, generated) { + let known = GeneratedState::decode(state.encode_to_vec().as_slice()) + .expect("the generated message decodes hand-written output"); + assert_eq!(known, generated, "State {input:?}"); + } + + let resource = Resource::decode(input.as_slice()); + let generated = GeneratedResource::decode(input.as_slice()); + assert_eq!(resource.is_ok(), generated.is_ok(), "Resource {input:?}"); + if let (Ok(resource), Ok(generated)) = (resource, generated) { + let known = GeneratedResource::decode(resource.encode_to_vec().as_slice()) + .expect("the generated message decodes hand-written output"); + assert_eq!(known, generated, "Resource {input:?}"); + } + } + } + } +} + +#[test] +fn a_map_field_with_the_wrong_wire_type_fails_to_decode() { + let error = Resource::decode([0x10, 0x00].as_slice()).expect_err("a map is not a varint"); + assert!( + error.to_string().contains("Resource.connection_details"), + "{error}" + ); + let error = State::decode([0x10, 0x00].as_slice()).expect_err("a map is not a varint"); + assert!(error.to_string().contains("State.resources"), "{error}"); +} + +#[test] +fn unknown_fields_of_the_composite_resource_are_kept() { + let mut state = Vec::new(); + encoding::bytes::encode(1, &with_unknown_fields(&known_resource()), &mut state); + + let decoded = State::decode(state.as_slice()).expect("decodes"); + let composite = decoded.composite.as_ref().expect("has a composite"); + assert_ne!(composite.unknown_fields, UnknownFields::default()); + assert_eq!(decoded.encode_to_vec(), state); +} + +#[test] +fn debug_prints_ready_as_prost_generates_it() { + for ready in [0, 1, 2, 7] { + let resource = Resource { + ready, + ..Default::default() + }; + let generated = GeneratedResource { + ready, + ..Default::default() + }; + let want = format!("{generated:?}") + .replace("GeneratedResource", "Resource") + .replace(" }", ", unknown_fields: UnknownFields([]) }"); + assert_eq!(format!("{resource:?}"), want); + } + let ready = Resource { + ready: Ready::True as i32, + ..Default::default() + }; + assert!(format!("{ready:?}").contains("ready: True")); +} + +#[test] +fn clear_drops_unknown_fields() { + let mut resource = + Resource::decode(with_unknown_fields(&known_resource()).as_slice()).expect("decodes"); + resource.clear(); + assert_eq!(resource, Resource::default()); +} + +#[test] +fn json_has_no_unknown_fields() { + let state = State::decode(newer_state().as_slice()).expect("decodes"); + let json = serde_json::to_value(&state).expect("serializes"); + assert_eq!( + json, + serde_json::to_value(known_state_without_composite()).expect("serializes") + ); + + let from_json: State = serde_json::from_value(json).expect("deserializes"); + assert_eq!(from_json, known_state_without_composite()); +} + +fn known_state_without_composite() -> State { + State { + composite: None, + ..known_state() + } +} + +/// The hand-written messages must be updated when the vendored proto +/// changes them. Until then a new field still passes through as an unknown +/// field, but functions cannot read or set it. +#[test] +fn hand_written_messages_cover_every_field_of_the_proto() { + let cases = [ + ( + "State", + vec![ + ("composite", 1, Type::Message), + ("resources", 2, Type::Message), + ], + ), + ( + "Resource", + vec![ + ("resource", 1, Type::Message), + ("connection_details", 2, Type::Message), + ("ready", 3, Type::Enum), + ], + ), + ]; + + let files = FileDescriptorSet::decode(FILE_DESCRIPTOR_SET).expect("the descriptor decodes"); + for (name, want) in cases { + let message = files + .file + .iter() + .filter(|file| file.package() == "apiextensions.fn.proto.v1") + .flat_map(|file| &file.message_type) + .find(|message| message.name() == name) + .unwrap_or_else(|| panic!("{name} is in the descriptor")); + let got: Vec<_> = message + .field + .iter() + .map(|field| (field.name(), field.number(), field.r#type())) + .collect(); + assert_eq!(got, want, "{name}: update sdk/src/proto/state.rs"); + } +}