Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use time::OffsetDateTime in services #979

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion services/autorust/codegen/src/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ serde = {{ version = "1.0", features = ["derive"] }}
serde_json = "1.0"
bytes = "1.0"
futures = "0.3"
time = "0.3"

[dev-dependencies]
azure_identity = {{ path = "../../../sdk/identity" }}
tokio = {{ version = "1.0", features = ["macros"] }}
env_logger = "0.9"
time = "0.3"

[package.metadata.docs.rs]
all-features = true
Expand Down
26 changes: 21 additions & 5 deletions services/autorust/codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn id_models() -> Ident {
}

pub fn type_name_gen(type_name: &TypeName) -> Result<TypeNameCode> {
Ok(match type_name {
let mut type_name_code = match type_name {
TypeName::Reference(name) => {
let idt = parse_ident(&name.to_pascal_case())?;
TypeNameCode::from(idt).allow_qualify_models(true)
Expand All @@ -100,7 +100,11 @@ pub fn type_name_gen(type_name: &TypeName) -> Result<TypeNameCode> {
TypeName::Float64 => TypeNameCode::from(tp_f64()).allow_impl_into(false),
TypeName::Boolean => TypeNameCode::from(tp_bool()).allow_impl_into(false),
TypeName::String => TypeNameCode::from(tp_string()),
})
TypeName::DateTime => TypeNameCode::from(tp_date_time()),
TypeName::DateTimeRfc1123 => TypeNameCode::from(tp_date_time()),
};
type_name_code.type_name = Some(type_name.clone());
Ok(type_name_code)
}

pub fn create_mod() -> TokenStream {
Expand Down Expand Up @@ -145,15 +149,23 @@ pub struct TypeNameCode {
boxed: bool,
qualify_models: bool,
allow_qualify_models: bool,
type_name: Option<TypeName>,
}

impl TypeNameCode {
pub fn is_string(&self) -> bool {
self.type_path.to_token_stream().to_string() == TP_STRING
self.type_name == Some(TypeName::String)
}
pub fn is_date_time(&self) -> bool {
self.type_name == Some(TypeName::DateTime)
}
pub fn is_date_time_rfc1123(&self) -> bool {
self.type_name == Some(TypeName::DateTimeRfc1123)
}
pub fn is_vec(&self) -> bool {
self.vec_count > 0 && !self.force_value
}
/// Forces the type to be `serde_json::Value`
pub fn force_value(mut self, force_value: bool) -> Self {
self.force_value = force_value;
self
Expand Down Expand Up @@ -272,6 +284,7 @@ impl From<TypePath> for TypeNameCode {
boxed: false,
qualify_models: false,
allow_qualify_models: false,
type_name: None,
}
}
}
Expand Down Expand Up @@ -372,15 +385,18 @@ fn tp_bool() -> TypePath {
parse_type_path("bool").unwrap()
}

const TP_STRING: &str = "String";
fn tp_string() -> TypePath {
parse_type_path(TP_STRING).unwrap() // std::string::String
parse_type_path("String").unwrap() // std::string::String
}

fn tp_box() -> TypePath {
parse_type_path("Box").unwrap() // std::boxed::Box
}

fn tp_date_time() -> TypePath {
parse_type_path("time::OffsetDateTime").unwrap()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 12 additions & 2 deletions services/autorust/codegen/src/codegen_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,18 @@ fn create_struct(cg: &CodeGen, schema: &SchemaGen, struct_name: &str, pageable:
if field_name != property_name {
serde_attrs.push(quote! { rename = #property_name });
}
if !is_required {
if type_name.is_vec() {
if is_required {
if type_name.is_date_time() {
serde_attrs.push(quote! { with = "azure_core::date::rfc3339"});
} else if type_name.is_date_time_rfc1123() {
serde_attrs.push(quote! { with = "azure_core::date::rfc1123"});
}
} else {
if type_name.is_date_time() {
serde_attrs.push(quote! { with = "azure_core::date::rfc3339::option"});
} else if type_name.is_date_time_rfc1123() {
serde_attrs.push(quote! { with = "azure_core::date::rfc1123::option"});
} else if type_name.is_vec() {
serde_attrs.push(quote! { default, skip_serializing_if = "Vec::is_empty"});
} else {
serde_attrs.push(quote! { default, skip_serializing_if = "Option::is_none"});
Expand Down
6 changes: 6 additions & 0 deletions services/autorust/codegen/src/codegen_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,12 @@ impl FunctionParam {
fn is_string(&self) -> bool {
self.type_name.is_string()
}
fn is_date_time(&self) -> bool {
self.type_name.is_date_time()
}
fn is_date_time_rfc1123(&self) -> bool {
self.type_name.is_date_time_rfc1123()
}
}

#[derive(Clone)]
Expand Down
13 changes: 12 additions & 1 deletion services/autorust/codegen/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ fn add_references_for_schema(list: &mut Vec<TypedReference>, schema: &Schema) {
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum TypeName {
Reference(String),
Array(Box<TypeName>),
Expand All @@ -695,6 +696,8 @@ pub enum TypeName {
Float64,
Boolean,
String,
DateTime,
DateTimeRfc1123,
}

pub fn get_type_name_for_schema(schema: &SchemaCommon) -> Result<TypeName> {
Expand All @@ -720,7 +723,15 @@ pub fn get_type_name_for_schema(schema: &SchemaCommon) -> Result<TypeName> {
TypeName::Float64
}
}
DataType::String => TypeName::String,
DataType::String => {
if format == Some("date-time") {
TypeName::DateTime
} else if format == Some("date-time-rfc1123") {
TypeName::DateTimeRfc1123
} else {
TypeName::String
}
}
DataType::Boolean => TypeName::Boolean,
DataType::Object => TypeName::Value,
DataType::File => TypeName::Bytes,
Expand Down
2 changes: 1 addition & 1 deletion services/mgmt/activedirectory/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/mgmt/addons/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion services/mgmt/adhybridhealthservice/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading