Skip to main content

unitycatalog_common/models/
functions_ext.rs

1//! Hand-written ergonomic constructors for the generated function models.
2//!
3//! `CreateFunctionRequest` wraps its payload in a `function_info` envelope (a
4//! [`CreateFunction`]) to match the Unity Catalog wire contract, so the generated
5//! `CreateFunctionBuilder` only exposes a single `function_info` setter. These helpers
6//! restore fluent construction of the payload; they complement the `with_*` setters
7//! buffa-codegen already emits on `CreateFunction` (`with_routine_definition`,
8//! `with_routine_body_language`, `with_comment`) by adding a required-field constructor
9//! and setters for the message- and map-typed fields codegen omits.
10
11use super::functions::v1::{
12    CreateFunction, FunctionParameterInfos, ParameterStyle, RoutineBody, SecurityType,
13    SqlDataAccess,
14};
15
16impl CreateFunction {
17    /// Create a `CreateFunction` payload from the required function attributes.
18    ///
19    /// Optional fields (input params, routine definition, comment, properties, …) are
20    /// left unset and can be added with the `with_*` methods.
21    #[allow(clippy::too_many_arguments)]
22    pub fn new(
23        name: impl Into<String>,
24        catalog_name: impl Into<String>,
25        schema_name: impl Into<String>,
26        data_type: impl Into<String>,
27        full_data_type: impl Into<String>,
28        parameter_style: ParameterStyle,
29        is_deterministic: bool,
30        sql_data_access: SqlDataAccess,
31        is_null_call: bool,
32        security_type: SecurityType,
33        routine_body: RoutineBody,
34    ) -> Self {
35        Self {
36            name: name.into(),
37            catalog_name: catalog_name.into(),
38            schema_name: schema_name.into(),
39            data_type: data_type.into(),
40            full_data_type: full_data_type.into(),
41            parameter_style: buffa::EnumValue::Known(parameter_style),
42            is_deterministic,
43            sql_data_access: buffa::EnumValue::Known(sql_data_access),
44            is_null_call,
45            security_type: buffa::EnumValue::Known(security_type),
46            routine_body: buffa::EnumValue::Known(routine_body),
47            ..Default::default()
48        }
49    }
50
51    /// Set the array of function parameter infos.
52    #[must_use = "with_* setters return `self` by value; assign or chain the result"]
53    pub fn with_input_params(
54        mut self,
55        input_params: impl Into<Option<FunctionParameterInfos>>,
56    ) -> Self {
57        self.input_params = buffa::MessageField::from(input_params.into());
58        self
59    }
60
61    /// Set the key-value properties attached to the securable.
62    #[must_use = "with_* setters return `self` by value; assign or chain the result"]
63    pub fn with_properties<I, K, V>(mut self, properties: I) -> Self
64    where
65        I: IntoIterator<Item = (K, V)>,
66        K: Into<String>,
67        V: Into<String>,
68    {
69        self.properties = properties
70            .into_iter()
71            .map(|(k, v)| (k.into(), v.into()))
72            .collect();
73        self
74    }
75}