Skip to content

Commit

Permalink
re-add boxes for oneOfs, and add builder helpers for optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzou committed Feb 1, 2024
1 parent 6168504 commit 7a1f178
Show file tree
Hide file tree
Showing 1,410 changed files with 8,791 additions and 6,189 deletions.
10 changes: 6 additions & 4 deletions .generator/src/generator/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ def get_type_for_attribute(schema, attribute, current_name=None):
return type_to_rust(child_schema, alternative_name=alternative_name)


def get_type_for_parameter(parameter, version=None):
def get_type_for_parameter(parameter, version=None, render_option=None):
"""Return Rust type name for the parameter."""
render_option = True
if "required" in parameter:
render_option = not parameter["required"]
if render_option is None:
if "required" in parameter:
render_option = not parameter["required"]
else:
render_option = True
if "content" in parameter:
assert "in" not in parameter
for content in parameter["content"].values():
Expand Down
28 changes: 23 additions & 5 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use serde::{Serialize, Deserialize};
use crate::datadog::*;

{%- set structName = name.replace(" ", "")+"API" %}
{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=True) %}
{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=true) %}
{%- set httpMethod = method.upper() %}
{%- set returnType = operation|return_type(version) %}
{%- set formParameter = operation|form_parameter %}
{%- set optionalBody = False if "required" in operation.requestBody and operation.requestBody.required else True %}
{%- set optionalBody = false if "required" in operation.requestBody and operation.requestBody.required else true %}

{%- for name, parameter in operation|parameters if parameter.required != true %}
{%- if loop.first %}
/// {{operation.operationId}}OptionalParams is a struct for passing parameters to the method [`{{ structName }}::{{operation.operationId | snake_case}}`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct {{operation.operationId}}OptionalParams {
{%- endif %}
{%- if parameter.description is defined %}
Expand All @@ -24,9 +24,27 @@ pub struct {{operation.operationId}}OptionalParams {
}
{% endif %}
{%- endfor %}
{%- for name, parameter in operation|parameters if parameter.required != true %}
{%- if loop.first %}
impl {{operation.operationId}}OptionalParams {
{%- endif %}
{%- if parameter.description is defined %}
{{parameter.description | block_comment}}
{%- endif %}
{%- if get_deprecated(model) %}
#[allow(deprecated)]
{%- endif %}
pub fn {{name|variable_name}}(&mut self, value: {{get_type_for_parameter(parameter, version, render_option=false)}}) -> &mut Self {
self.{{name|variable_name}} = Some(value);
self
}
{%- if loop.last %}
}
{% endif %}
{%- endfor %}
{%- endfor %}

{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=True) %}
{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=true) %}
{%- set httpMethod = method.upper() %}
{%- set returnType = operation|return_type(version) %}
{%- set formParameter = operation|form_parameter %}
Expand Down Expand Up @@ -65,7 +83,7 @@ impl {{ structName }} {
Self { config }
}

{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=True) %}
{% for path, method, operation in operations|sort(attribute="2.operationId", case_sensitive=true) %}
{%- set httpMethod = method.upper() %}
{%- set returnType = operation|return_type(version) %}
{%- set formParameter = operation|form_parameter %}
Expand Down
6 changes: 5 additions & 1 deletion .generator/src/generator/templates/model_oneof.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use serde::{Deserialize, Serialize};
#[serde(untagged)]
pub enum {{name}} {
{%- for oneOf in model.oneOf %}
{%- set dataType = get_type(oneOf, render_nullable=False, render_option=False, render_box=oneOf|is_primitive or oneOf.type == "array", version=version) %}
{%- set dataType = get_type(oneOf, render_nullable=false, render_option=false, render_box=false, version=version) %}
{%- set attributeName = (get_name(oneOf) or dataType)|upperfirst %}
{%- if oneOf | is_primitive or oneOf.type == "array" %}
{{attributeName}}({{dataType}}),
{%- else %}
{{attributeName}}(Box<{{dataType}}>),
{%- endif %}
{%- endfor%}
}
14 changes: 7 additions & 7 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct {{ name }} {
{%- set propertyName = attr|variable_name %}
{%- set required = attr in model.required %}
{%- set nullable = schema.get("nullable", False)%}
{%- set dataType = get_type(schema, alternative_name=name + propertyName, render_nullable=nullable, render_option=not required, render_box=True, version=version) %}
{%- set dataType = get_type(schema, alternative_name=name + propertyName, render_nullable=nullable, render_option=not required, render_box=false, version=version) %}
{%- if schema.description is defined %}
{{ schema.description | block_comment }}
{%- endif %}
Expand All @@ -20,22 +20,22 @@ pub struct {{ name }} {
pub {{propertyName}}: {{dataType}},
{%- endfor %}
{%- if model.additionalProperties is defined and model.additionalProperties != False %}
{%- set dataType = get_type(model.additionalProperties, alternative_name=None, render_nullable=False, render_option=False, render_box=True, version=version) %}
{%- set dataType = get_type(model.additionalProperties, alternative_name=None, render_nullable=False, render_option=False, render_box=false, version=version) %}
#[serde(flatten)]
pub additional_properties: std::collections::BTreeMap<String, {{ dataType }}>,
{%- endif %}
}

impl {{ name }} {
pub fn new({% for attr, schema in model.get("properties", {}).items() if attr in model.required %}{%- set nullable = schema.get("nullable", False)%}{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=False, render_box=True, version=version) %}{{attr|variable_name}}: {{ dataType }}{%- if not loop.last %}, {% endif %}{% endfor %}) -> {{ name }} {
pub fn new({% for attr, schema in model.get("properties", {}).items() if attr in model.required %}{%- set nullable = schema.get("nullable", False)%}{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=False, render_box=false, version=version) %}{{attr|variable_name}}: {{ dataType }}{%- if not loop.last %}, {% endif %}{% endfor %}) -> {{ name }} {
{%- if get_deprecated(model) %}
#[allow(deprecated)]
{%- endif %}
{{ name }} {
{%- for attr, schema in model.get("properties", {}).items() %}
{%- set required = attr in model.required %}
{%- set nullable = schema.get("nullable", False)%}
{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=not required, render_box=True, version=version) %}
{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=not required, render_box=false, version=version) %}
{%- if attr in model.get("required", []) %}
{{ attr|variable_name }},
{%- else %}
Expand All @@ -49,18 +49,18 @@ impl {{ name }} {
}
{% for attr, schema in model.get("properties", {}).items() if attr not in model.required %}
{%- set nullable = schema.get("nullable", False)%}
{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=False, render_box=True, version=version) %}
{%- set dataType = get_type(schema, alternative_name=name + attr|variable_name, render_nullable=nullable, render_option=False, render_box=false, version=version) %}
{%- if get_deprecated(model) %}
#[allow(deprecated)]
{%- endif %}
pub fn with_{{attr|variable_name}}(&mut self, value: {{dataType}}) -> &mut Self {
pub fn {{attr|variable_name}}(&mut self, value: {{dataType}}) -> &mut Self {
self.{{attr|variable_name}} = Some(value);
self
}
{% endfor %}
}

{%- if not model.required %}
{% if not model.required %}
impl Default for {{ name }} {
fn default() -> Self {
Self::new()
Expand Down
42 changes: 40 additions & 2 deletions src/datadogV1/api/api_aws_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest;
use serde::{Deserialize, Serialize};

/// ListAWSAccountsOptionalParams is a struct for passing parameters to the method [`AWSIntegrationAPI::list_aws_accounts`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct ListAWSAccountsOptionalParams {
/// Only return AWS accounts that matches this `account_id`.
pub account_id: Option<String>,
Expand All @@ -16,8 +16,26 @@ pub struct ListAWSAccountsOptionalParams {
pub access_key_id: Option<String>,
}

impl ListAWSAccountsOptionalParams {
/// Only return AWS accounts that matches this `account_id`.
pub fn account_id(&mut self, value: String) -> &mut Self {
self.account_id = Some(value);
self
}
/// Only return AWS accounts that matches this role_name.
pub fn role_name(&mut self, value: String) -> &mut Self {
self.role_name = Some(value);
self
}
/// Only return AWS accounts that matches this `access_key_id`.
pub fn access_key_id(&mut self, value: String) -> &mut Self {
self.access_key_id = Some(value);
self
}
}

/// UpdateAWSAccountOptionalParams is a struct for passing parameters to the method [`AWSIntegrationAPI::update_aws_account`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct UpdateAWSAccountOptionalParams {
/// Only return AWS accounts that matches this `account_id`.
pub account_id: Option<String>,
Expand All @@ -29,6 +47,26 @@ pub struct UpdateAWSAccountOptionalParams {
pub access_key_id: Option<String>,
}

impl UpdateAWSAccountOptionalParams {
/// Only return AWS accounts that matches this `account_id`.
pub fn account_id(&mut self, value: String) -> &mut Self {
self.account_id = Some(value);
self
}
/// Only return AWS accounts that match this `role_name`.
/// Required if `account_id` is specified.
pub fn role_name(&mut self, value: String) -> &mut Self {
self.role_name = Some(value);
self
}
/// Only return AWS accounts that matches this `access_key_id`.
/// Required if none of the other two options are specified.
pub fn access_key_id(&mut self, value: String) -> &mut Self {
self.access_key_id = Some(value);
self
}
}

/// CreateAWSAccountError is a struct for typed errors of method [`AWSIntegrationAPI::create_aws_account`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
42 changes: 40 additions & 2 deletions src/datadogV1/api/api_dashboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ use reqwest;
use serde::{Deserialize, Serialize};

/// GetPublicDashboardInvitationsOptionalParams is a struct for passing parameters to the method [`DashboardsAPI::get_public_dashboard_invitations`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct GetPublicDashboardInvitationsOptionalParams {
/// The number of records to return in a single request.
pub page_size: Option<i64>,
/// The page to access (base 0).
pub page_number: Option<i64>,
}

impl GetPublicDashboardInvitationsOptionalParams {
/// The number of records to return in a single request.
pub fn page_size(&mut self, value: i64) -> &mut Self {
self.page_size = Some(value);
self
}
/// The page to access (base 0).
pub fn page_number(&mut self, value: i64) -> &mut Self {
self.page_number = Some(value);
self
}
}

/// ListDashboardsOptionalParams is a struct for passing parameters to the method [`DashboardsAPI::list_dashboards`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct ListDashboardsOptionalParams {
/// When `true`, this query only returns shared custom created
/// or cloned dashboards.
Expand All @@ -29,6 +42,31 @@ pub struct ListDashboardsOptionalParams {
pub start: Option<i64>,
}

impl ListDashboardsOptionalParams {
/// When `true`, this query only returns shared custom created
/// or cloned dashboards.
pub fn filter_shared(&mut self, value: bool) -> &mut Self {
self.filter_shared = Some(value);
self
}
/// When `true`, this query returns only deleted custom-created
/// or cloned dashboards. This parameter is incompatible with `filter[shared]`.
pub fn filter_deleted(&mut self, value: bool) -> &mut Self {
self.filter_deleted = Some(value);
self
}
/// The maximum number of dashboards returned in the list.
pub fn count(&mut self, value: i64) -> &mut Self {
self.count = Some(value);
self
}
/// The specific offset to use as the beginning of the returned response.
pub fn start(&mut self, value: i64) -> &mut Self {
self.start = Some(value);
self
}
}

/// CreateDashboardError is a struct for typed errors of method [`DashboardsAPI::create_dashboard`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
15 changes: 14 additions & 1 deletion src/datadogV1/api/api_downtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ use reqwest;
use serde::{Deserialize, Serialize};

/// ListDowntimesOptionalParams is a struct for passing parameters to the method [`DowntimesAPI::list_downtimes`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct ListDowntimesOptionalParams {
/// Only return downtimes that are active when the request is made.
pub current_only: Option<bool>,
/// Return creator information.
pub with_creator: Option<bool>,
}

impl ListDowntimesOptionalParams {
/// Only return downtimes that are active when the request is made.
pub fn current_only(&mut self, value: bool) -> &mut Self {
self.current_only = Some(value);
self
}
/// Return creator information.
pub fn with_creator(&mut self, value: bool) -> &mut Self {
self.with_creator = Some(value);
self
}
}

/// CancelDowntimeError is a struct for typed errors of method [`DowntimesAPI::cancel_downtime`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
39 changes: 38 additions & 1 deletion src/datadogV1/api/api_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest;
use serde::{Deserialize, Serialize};

/// ListEventsOptionalParams is a struct for passing parameters to the method [`EventsAPI::list_events`]
#[derive(Clone, Debug)]
#[derive(Clone, Default, Debug)]
pub struct ListEventsOptionalParams {
/// Priority of your events, either `low` or `normal`.
pub priority: Option<crate::datadogV1::model::EventPriority>,
Expand All @@ -26,6 +26,43 @@ pub struct ListEventsOptionalParams {
pub page: Option<i32>,
}

impl ListEventsOptionalParams {
/// Priority of your events, either `low` or `normal`.
pub fn priority(&mut self, value: crate::datadogV1::model::EventPriority) -> &mut Self {
self.priority = Some(value);
self
}
/// A comma separated string of sources.
pub fn sources(&mut self, value: String) -> &mut Self {
self.sources = Some(value);
self
}
/// A comma separated list indicating what tags, if any, should be used to filter the list of events.
pub fn tags(&mut self, value: String) -> &mut Self {
self.tags = Some(value);
self
}
/// Set unaggregated to `true` to return all events within the specified [`start`,`end`] timeframe.
/// Otherwise if an event is aggregated to a parent event with a timestamp outside of the timeframe,
/// it won't be available in the output. Aggregated events with `is_aggregate=true` in the response will still be returned unless exclude_aggregate is set to `true.`
pub fn unaggregated(&mut self, value: bool) -> &mut Self {
self.unaggregated = Some(value);
self
}
/// Set `exclude_aggregate` to `true` to only return unaggregated events where `is_aggregate=false` in the response. If the `exclude_aggregate` parameter is set to `true`,
/// then the unaggregated parameter is ignored and will be `true` by default.
pub fn exclude_aggregate(&mut self, value: bool) -> &mut Self {
self.exclude_aggregate = Some(value);
self
}
/// By default 1000 results are returned per request. Set page to the number of the page to return with `0` being the first page. The page parameter can only be used
/// when either unaggregated or exclude_aggregate is set to `true.`
pub fn page(&mut self, value: i32) -> &mut Self {
self.page = Some(value);
self
}
}

/// CreateEventError is a struct for typed errors of method [`EventsAPI::create_event`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
Loading

0 comments on commit 7a1f178

Please sign in to comment.