Skip to content

Commit

Permalink
support optional parameters for thrift functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kubroid authored and akubrinsky-zetaglobal committed Feb 25, 2025
1 parent e3e0666 commit 587fd35
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 3 deletions.
1 change: 1 addition & 0 deletions pilota-build/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct Arg {
pub name: Ident,
pub id: i32,
pub tags: Arc<Tags>,
pub attribute: FieldKind,
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions pilota-build/src/middle/rir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct Arg {
pub name: Ident,
pub id: i32,
pub tags_id: TagId,
pub kind: FieldKind,
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions pilota-build/src/parser/protobuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl Lower {
&Default::default(),
),
tags: Arc::new(Tags::default()),
attribute: FieldKind::Required,
}],
oneway: false,
ret: self.lower_ty(None, m.output_type.as_deref(), &Default::default()),
Expand Down
5 changes: 5 additions & 0 deletions pilota-build/src/parser/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ impl ThriftLower {
id: a.id,
name: self.lower_ident(&a.name),
tags: Arc::new(self.extract_tags(&a.annotations)),
attribute: match a.attribute {
pilota_thrift_parser::Attribute::Required => FieldKind::Required,
pilota_thrift_parser::Attribute::Optional
| pilota_thrift_parser::Attribute::Default => FieldKind::Optional,
},
})
.collect(),
ret: self.lower_ty(&method.result_type),
Expand Down
4 changes: 4 additions & 0 deletions pilota-build/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,10 @@ impl Resolver {
name: a.name.clone(),
id: a.id,
tags_id,
kind: match a.attribute {
ir::FieldKind::Required => FieldKind::Required,
ir::FieldKind::Optional => FieldKind::Optional,
},
});
self.nodes.insert(
def_id,
Expand Down
1 change: 0 additions & 1 deletion pilota-build/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ impl FromStr for KeepUnknownFields {
}

pub mod protobuf {

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum ProstType {
SInt32,
Expand Down
722 changes: 722 additions & 0 deletions pilota-build/test_data/thrift/optional_parameter.rs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions pilota-build/test_data/thrift/optional_parameter.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service Test {
string test(
1: required string required_param
2: optional string optional_param
3: string default_param
)
}
2 changes: 1 addition & 1 deletion pilota-thrift-parser/src/descriptor/field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Annotations, ConstValue, Ident, Type};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub enum Attribute {
Optional,
Required,
Expand Down
4 changes: 3 additions & 1 deletion pilota-thrift-parser/src/parser/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl Parser for Function {
|(oneway, r#type, _, name, _, _, arguments, _, _, _, throws, _, annotations, _)| {
let mut args = arguments.unwrap_or_default();
args.iter_mut().for_each(|f| {
f.attribute = Attribute::Required;
if f.attribute == Attribute::Default {
f.attribute = Attribute::Required
}
});
Function {
name,
Expand Down

0 comments on commit 587fd35

Please sign in to comment.