Skip to content

Commit b44f91e

Browse files
authored
add support for unions in generated service serialization (#1414)
1 parent 1f66c69 commit b44f91e

File tree

359 files changed

+74182
-180025
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

359 files changed

+74182
-180025
lines changed

services/autorust/codegen/src/codegen.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,25 @@ pub struct CodeGen<'a> {
2929
optional_properties: HashSet<PropertyName>,
3030
fix_case_properties: HashSet<&'a str>,
3131
invalid_types: HashSet<PropertyName>,
32+
33+
union_types: HashSet<String>,
3234
}
3335

3436
impl<'a> CodeGen<'a> {
37+
pub fn add_union_type(&mut self, type_name: String) {
38+
self.union_types.insert(type_name);
39+
}
40+
41+
pub fn is_union_type(&self, type_name: &TypeNameCode) -> bool {
42+
self.union_types.contains(&type_name.type_path.to_token_stream().to_string())
43+
}
44+
45+
pub fn set_if_union_type(&self, type_name: &mut TypeNameCode) {
46+
if self.is_union_type(type_name) {
47+
type_name.union(true);
48+
}
49+
}
50+
3551
pub fn new(
3652
crate_config: &'a CrateConfig,
3753
box_properties: HashSet<PropertyName>,
@@ -47,6 +63,7 @@ impl<'a> CodeGen<'a> {
4763
optional_properties,
4864
fix_case_properties,
4965
invalid_types,
66+
union_types: HashSet::new(),
5067
})
5168
}
5269

@@ -115,14 +132,15 @@ pub fn parse_query_params(uri: &str) -> Result<HashSet<String>> {
115132
pub struct TypeNameCode {
116133
type_path: TypePath,
117134
force_value: bool,
118-
pub optional: bool,
135+
optional: bool,
119136
vec_count: i32,
120137
impl_into: bool,
121138
allow_impl_into: bool,
122139
boxed: bool,
123140
qualify_models: bool,
124141
allow_qualify_models: bool,
125142
type_name: Option<TypeName>,
143+
union: bool,
126144
}
127145

128146
impl TypeNameCode {
@@ -180,6 +198,9 @@ impl TypeNameCode {
180198
self.optional = optional;
181199
self
182200
}
201+
pub fn union(&mut self, union: bool) {
202+
self.union = union;
203+
}
183204
pub fn incr_vec_count(mut self) -> Self {
184205
self.vec_count += 1;
185206
self
@@ -210,6 +231,12 @@ impl TypeNameCode {
210231

211232
fn to_type(&self) -> Type {
212233
let mut tp = self.type_path.clone();
234+
if self.union {
235+
if let Some(last) = tp.path.segments.last_mut() {
236+
last.ident = Ident::new(&format!("{}Union", last.ident), last.ident.span());
237+
}
238+
}
239+
213240
if self.allow_qualify_models && self.qualify_models {
214241
tp.path.segments.insert(0, id_models().into());
215242
}
@@ -246,6 +273,13 @@ impl TypeNameCode {
246273
}
247274
tp
248275
}
276+
277+
pub fn is_optional(&self) -> bool {
278+
self.optional
279+
}
280+
pub fn is_union(&self) -> bool {
281+
self.union
282+
}
249283
}
250284

251285
impl ToString for TypeNameCode {
@@ -291,6 +325,7 @@ impl From<TypePath> for TypeNameCode {
291325
qualify_models: false,
292326
allow_qualify_models: false,
293327
type_name: None,
328+
union: false,
294329
}
295330
}
296331
}
@@ -503,4 +538,12 @@ mod tests {
503538
assert_eq!("bytes :: Bytes", tp.to_string());
504539
Ok(())
505540
}
541+
542+
#[test]
543+
fn test_with_union() -> Result<()> {
544+
let mut tp = TypeNameCode::try_from("farm::Animal")?;
545+
tp.union(true);
546+
assert_eq!("farm :: AnimalUnion", tp.to_string());
547+
Ok(())
548+
}
506549
}

0 commit comments

Comments
 (0)