Skip to content

Commit f276843

Browse files
authored
rename ParameterType to ParameterIn in autorust (#1419)
1 parent b1d0dbe commit f276843

File tree

7 files changed

+240
-71
lines changed

7 files changed

+240
-71
lines changed

services/autorust/codegen/src/codegen.rs

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syn::{
1616
punctuated::Punctuated,
1717
token::{Gt, Impl, Lt},
1818
AngleBracketedGenericArguments, GenericArgument, Path, PathArguments, PathSegment, TraitBound, TraitBoundModifier, Type, TypeImplTrait,
19-
TypeParamBound, TypePath,
19+
TypeParamBound, TypePath, TypeReference,
2020
};
2121

2222
/// code generation context
@@ -130,6 +130,8 @@ pub fn parse_query_params(uri: &str) -> Result<HashSet<String>> {
130130

131131
#[derive(Clone)]
132132
pub struct TypeNameCode {
133+
/// Whether or not to pass a type as a reference.
134+
reference: bool,
133135
type_path: TypePath,
134136
force_value: bool,
135137
optional: bool,
@@ -148,16 +150,42 @@ impl TypeNameCode {
148150
let mut type_name_code = match type_name {
149151
TypeName::Reference(name) => {
150152
let idt = parse_ident(&name.to_pascal_case())?;
151-
TypeNameCode::from(idt).allow_qualify_models(true)
153+
let mut tn = TypeNameCode::from(idt);
154+
tn.allow_qualify_models(true);
155+
tn
156+
}
157+
TypeName::Array(vec_items_typ) => {
158+
let mut tn = TypeNameCode::new(vec_items_typ)?;
159+
tn.incr_vec_count();
160+
tn
152161
}
153-
TypeName::Array(vec_items_typ) => TypeNameCode::new(vec_items_typ)?.incr_vec_count(),
154162
TypeName::Value => TypeNameCode::from(tp_json_value()),
155163
TypeName::Bytes => TypeNameCode::from(tp_bytes()),
156-
TypeName::Int32 => TypeNameCode::from(tp_i32()).allow_impl_into(false),
157-
TypeName::Int64 => TypeNameCode::from(tp_i64()).allow_impl_into(false),
158-
TypeName::Float32 => TypeNameCode::from(tp_f32()).allow_impl_into(false),
159-
TypeName::Float64 => TypeNameCode::from(tp_f64()).allow_impl_into(false),
160-
TypeName::Boolean => TypeNameCode::from(tp_bool()).allow_impl_into(false),
164+
TypeName::Int32 => {
165+
let mut tn = TypeNameCode::from(tp_i32());
166+
tn.allow_impl_into(false);
167+
tn
168+
}
169+
TypeName::Int64 => {
170+
let mut tn = TypeNameCode::from(tp_i64());
171+
tn.allow_impl_into(false);
172+
tn
173+
}
174+
TypeName::Float32 => {
175+
let mut tn = TypeNameCode::from(tp_f32());
176+
tn.allow_impl_into(false);
177+
tn
178+
}
179+
TypeName::Float64 => {
180+
let mut tn = TypeNameCode::from(tp_f64());
181+
tn.allow_impl_into(false);
182+
tn
183+
}
184+
TypeName::Boolean => {
185+
let mut tn = TypeNameCode::from(tp_bool());
186+
tn.allow_impl_into(false);
187+
tn
188+
}
161189
TypeName::String => TypeNameCode::from(tp_string()),
162190
TypeName::DateTime => TypeNameCode::from(tp_date_time()),
163191
TypeName::DateTimeRfc1123 => TypeNameCode::from(tp_date_time()),
@@ -166,71 +194,94 @@ impl TypeNameCode {
166194
Ok(type_name_code)
167195
}
168196

197+
pub fn reference(&mut self, reference: bool) {
198+
self.reference = reference;
199+
}
200+
169201
pub fn is_string(&self) -> bool {
170202
self.type_name == Some(TypeName::String)
171203
}
204+
205+
pub fn is_reference(&self) -> bool {
206+
self.reference
207+
}
208+
172209
pub fn is_bytes(&self) -> bool {
173210
self.type_name == Some(TypeName::Bytes)
174211
}
212+
175213
pub fn set_as_bytes(&mut self) {
176214
self.force_value = false;
177215
self.type_name = Some(TypeName::Bytes);
178216
self.type_path = tp_bytes();
179217
}
218+
180219
pub fn is_value(&self) -> bool {
181220
self.type_name == Some(TypeName::Value)
182221
}
222+
183223
pub fn is_date_time(&self) -> bool {
184224
self.type_name == Some(TypeName::DateTime)
185225
}
226+
186227
pub fn is_date_time_rfc1123(&self) -> bool {
187228
self.type_name == Some(TypeName::DateTimeRfc1123)
188229
}
230+
189231
pub fn is_vec(&self) -> bool {
190232
self.vec_count > 0 && !self.force_value
191233
}
234+
192235
/// Forces the type to be `serde_json::Value`
193-
pub fn force_value(mut self, force_value: bool) -> Self {
236+
pub fn force_value(&mut self, force_value: bool) {
194237
self.force_value = force_value;
195-
self
196238
}
197-
pub fn optional(mut self, optional: bool) -> Self {
239+
240+
pub fn optional(&mut self, optional: bool) {
198241
self.optional = optional;
199-
self
200242
}
243+
201244
pub fn union(&mut self, union: bool) {
202245
self.union = union;
203246
}
204-
pub fn incr_vec_count(mut self) -> Self {
247+
248+
pub fn incr_vec_count(&mut self) {
205249
self.vec_count += 1;
206-
self
207250
}
208-
pub fn impl_into(mut self, impl_into: bool) -> Self {
251+
252+
pub fn impl_into(&mut self, impl_into: bool) {
209253
self.impl_into = impl_into;
210-
self
211254
}
255+
212256
pub fn has_impl_into(&self) -> bool {
213257
self.allow_impl_into && self.impl_into
214258
}
215-
fn allow_impl_into(mut self, allow_impl_into: bool) -> Self {
259+
260+
fn allow_impl_into(&mut self, allow_impl_into: bool) {
216261
self.allow_impl_into = allow_impl_into;
217-
self
218262
}
219-
pub fn boxed(mut self, boxed: bool) -> Self {
263+
264+
pub fn boxed(&mut self, boxed: bool) {
220265
self.boxed = boxed;
221-
self
222266
}
223-
pub fn qualify_models(mut self, qualify_models: bool) -> Self {
267+
268+
pub fn qualify_models(&mut self, qualify_models: bool) {
224269
self.qualify_models = qualify_models;
225-
self
226270
}
227-
fn allow_qualify_models(mut self, allow_qualify_models: bool) -> Self {
271+
272+
fn allow_qualify_models(&mut self, allow_qualify_models: bool) {
228273
self.allow_qualify_models = allow_qualify_models;
229-
self
274+
}
275+
276+
fn type_path(&self) -> TypePath {
277+
if self.is_string() && self.is_reference() {
278+
return tp_str();
279+
}
280+
self.type_path.clone()
230281
}
231282

232283
fn to_type(&self) -> Type {
233-
let mut tp = self.type_path.clone();
284+
let mut tp = self.type_path();
234285
if self.union {
235286
if let Some(last) = tp.path.segments.last_mut() {
236287
last.ident = Ident::new(&format!("{}Union", last.ident), last.ident.span());
@@ -247,6 +298,15 @@ impl TypeNameCode {
247298
if self.force_value {
248299
tp = Type::from(tp_json_value())
249300
}
301+
if self.is_reference() {
302+
let tr = TypeReference {
303+
and_token: Default::default(),
304+
lifetime: Default::default(),
305+
mutability: Default::default(),
306+
elem: Box::new(tp),
307+
};
308+
tp = Type::from(tr);
309+
}
250310
if self.boxed {
251311
tp = generic_type(tp_box(), tp);
252312
}
@@ -277,6 +337,7 @@ impl TypeNameCode {
277337
pub fn is_optional(&self) -> bool {
278338
self.optional
279339
}
340+
280341
pub fn is_union(&self) -> bool {
281342
self.union
282343
}
@@ -315,6 +376,7 @@ fn generic_type(mut wrap_tp: TypePath, tp: Type) -> Type {
315376
impl From<TypePath> for TypeNameCode {
316377
fn from(type_path: TypePath) -> Self {
317378
Self {
379+
reference: false,
318380
type_path,
319381
force_value: false,
320382
optional: false,
@@ -434,6 +496,10 @@ fn tp_box() -> TypePath {
434496
parse_type_path("Box").unwrap() // std::boxed::Box
435497
}
436498

499+
fn tp_str() -> TypePath {
500+
parse_type_path("str").unwrap() // std::str
501+
}
502+
437503
fn tp_date_time() -> TypePath {
438504
parse_type_path("time::OffsetDateTime").unwrap()
439505
}
@@ -475,18 +541,28 @@ mod tests {
475541
Ok(())
476542
}
477543

544+
#[test]
545+
fn test_reference() -> Result<()> {
546+
let mut tp = TypeNameCode::try_from("farm::Goat")?;
547+
tp.reference(true);
548+
assert_eq!("& farm :: Goat", tp.to_string());
549+
Ok(())
550+
}
551+
478552
#[test]
479553
fn test_type_path_code_vec() -> Result<()> {
480-
let mut tp = TypeNameCode::try_from("farm::Goat")?.incr_vec_count();
554+
let mut tp = TypeNameCode::try_from("farm::Goat")?;
555+
tp.incr_vec_count();
481556
assert_eq!("Vec < farm :: Goat >", tp.to_string());
482-
tp = tp.incr_vec_count();
557+
tp.incr_vec_count();
483558
assert_eq!("Vec < Vec < farm :: Goat > >", tp.to_string());
484559
Ok(())
485560
}
486561

487562
#[test]
488563
fn test_type_path_code_option() -> Result<()> {
489-
let tp = TypeNameCode::try_from("farm::Goat")?.optional(true);
564+
let mut tp = TypeNameCode::try_from("farm::Goat")?;
565+
tp.optional(true);
490566
assert_eq!("Option < farm :: Goat >", tp.to_string());
491567
Ok(())
492568
}
@@ -507,7 +583,8 @@ mod tests {
507583

508584
#[test]
509585
fn test_with_add_into() -> Result<()> {
510-
let tp = TypeNameCode::try_from("farm::Goat")?.impl_into(true);
586+
let mut tp = TypeNameCode::try_from("farm::Goat")?;
587+
tp.impl_into(true);
511588
assert_eq!("impl Into < farm :: Goat >", tp.to_string());
512589
Ok(())
513590
}
@@ -523,7 +600,7 @@ mod tests {
523600
#[test]
524601
fn test_disallow_impl_into() -> Result<()> {
525602
let mut tp = TypeNameCode::new(&TypeName::Int32)?;
526-
tp = tp.impl_into(true);
603+
tp.impl_into(true);
527604
assert!(!tp.has_impl_into());
528605
assert_eq!("i32", tp.to_string());
529606
Ok(())
@@ -532,7 +609,7 @@ mod tests {
532609
#[test]
533610
fn test_set_as_bytes() -> Result<()> {
534611
let mut tp = TypeNameCode::new(&TypeName::Int32)?;
535-
tp = tp.force_value(true);
612+
tp.force_value(true);
536613
tp.set_as_bytes();
537614
assert!(tp.is_bytes());
538615
assert_eq!("bytes :: Bytes", tp.to_string());

services/autorust/codegen/src/codegen_models.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,15 +917,15 @@ fn create_struct(
917917
}
918918

919919
if cg.should_force_obj(prop_nm) {
920-
type_name = type_name.force_value(true);
920+
type_name.force_value(true);
921921
}
922922

923923
let is_required = required.contains(property_name) && !cg.should_force_optional(prop_nm);
924924

925925
field_names.insert(format!("{field_name}"), is_required);
926926

927927
if !type_name.is_vec() && !is_required {
928-
type_name = type_name.optional(true);
928+
type_name.optional(true);
929929
}
930930

931931
let mut serde = SerdeCode::default();
@@ -974,7 +974,7 @@ fn create_struct(
974974
if cg.should_box_property(prop_nm) {
975975
boxed = true;
976976
}
977-
type_name = type_name.boxed(boxed);
977+
type_name.boxed(boxed);
978978

979979
doc_comments.push(DocCommentCode::from(&property.schema.schema.common.description));
980980

0 commit comments

Comments
 (0)