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

#297 support optional parameters for thrift functions #298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -662,6 +662,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
Loading