Skip to content

Commit

Permalink
Merge pull request #1860 from fzyzcjy/feat/11953
Browse files Browse the repository at this point in the history
Support inserting arbitrary Dart code; Support disabling default Hash/Eq generation
  • Loading branch information
fzyzcjy authored Apr 8, 2024
2 parents 8805811 + 27acac3 commit 5cda35a
Show file tree
Hide file tree
Showing 51 changed files with 4,157 additions and 2,149 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::codegen::ir::ty::IrType;
use crate::library::codegen::ir::ty::IrTypeTrait;
use std::collections::HashMap;

pub(crate) fn generate_class_extra_body(
ir_type: IrType,
dart_code_of_type: &HashMap<String, String>,
) -> String {
dart_code_of_type
.get(&ir_type.safe_ident())
.cloned()
.unwrap_or_default()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::Serialize;

pub(crate) mod field;
pub(crate) mod method;
pub(super) mod misc;
pub(crate) mod ty;

#[derive(Debug, Serialize, Default)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::codegen::generator::api_dart::spec_generator::class::field::{
generate_field_default, generate_field_required_modifier,
};
use crate::codegen::generator::api_dart::spec_generator::class::method::generate_api_methods;
use crate::codegen::generator::api_dart::spec_generator::class::misc::generate_class_extra_body;
use crate::codegen::generator::api_dart::spec_generator::class::ApiDartGeneratedClass;
use crate::codegen::generator::api_dart::spec_generator::misc::{
generate_dart_comments, generate_dart_maybe_implements_exception,
Expand Down Expand Up @@ -33,6 +34,8 @@ impl<'a> EnumRefApiDartGenerator<'a> {
generate_dart_maybe_implements_exception(self.ir.is_exception);

let methods_str = generate_api_methods(&src.name, self.context).join("\n");
let extra_body =
generate_class_extra_body(self.ir_type(), &self.context.ir_pack.dart_code_of_type);

Some(ApiDartGeneratedClass {
namespace: src.name.namespace.clone(),
Expand All @@ -44,6 +47,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
{variants}
{methods_str}
{extra_body}
}}",
),
needs_freezed: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::codegen::generator::api_dart::spec_generator::class::method::generate_api_methods;
use crate::codegen::generator::api_dart::spec_generator::class::misc::generate_class_extra_body;
use crate::codegen::generator::api_dart::spec_generator::class::ty::ApiDartGeneratorClassTrait;
use crate::codegen::generator::api_dart::spec_generator::class::ApiDartGeneratedClass;
use crate::codegen::ir::namespace::NamespacedName;
Expand All @@ -25,6 +26,8 @@ impl<'a> ApiDartGeneratorClassTrait for RustOpaqueApiDartGenerator<'a> {
self.context,
)
.join("\n");
let extra_body =
generate_class_extra_body(self.ir_type(), &self.context.ir_pack.dart_code_of_type);

Some(ApiDartGeneratedClass {
namespace: self.ir.namespace.clone(),
Expand All @@ -44,6 +47,7 @@ impl<'a> ApiDartGeneratorClassTrait for RustOpaqueApiDartGenerator<'a> {
);
{methods}
{extra_body}
}}"
),
needs_freezed: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::codegen::generator::api_dart::spec_generator::class::method::{
dart_constructor_postfix, generate_api_methods,
};
use crate::codegen::generator::api_dart::spec_generator::class::misc::generate_class_extra_body;
use crate::codegen::generator::api_dart::spec_generator::class::ty::ApiDartGeneratorClassTrait;
use crate::codegen::generator::api_dart::spec_generator::class::ApiDartGeneratedClass;
use crate::codegen::generator::api_dart::spec_generator::misc::{
Expand All @@ -15,20 +16,30 @@ impl<'a> ApiDartGeneratorClassTrait for StructRefApiDartGenerator<'a> {
let metadata = generate_dart_metadata(&src.dart_metadata);

let methods = generate_api_methods(&src.name, self.context);
let extra_body =
generate_class_extra_body(self.ir_type(), &self.context.ir_pack.dart_code_of_type);

let constructor_postfix = dart_constructor_postfix(&src.name, &self.context.ir_pack.funcs);

Some(ApiDartGeneratedClass {
namespace: src.name.namespace.clone(),
code: if src.using_freezed() {
self.generate_mode_freezed(src, &comments, &metadata, &methods, constructor_postfix)
self.generate_mode_freezed(
src,
&comments,
&metadata,
&methods,
constructor_postfix,
&extra_body,
)
} else {
self.generate_mode_non_freezed(
src,
&comments,
&metadata,
&methods,
constructor_postfix,
&extra_body,
)
},
needs_freezed: src.using_freezed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl<'a> StructRefApiDartGenerator<'a> {
metadata: &str,
methods: &[String],
constructor_postfix: &str,
extra_body: &str,
) -> String {
let private_constructor = if !methods.is_empty() {
format!("const {}._();", self.ir.ident.0.name)
Expand All @@ -33,6 +34,7 @@ impl<'a> StructRefApiDartGenerator<'a> {
{private_constructor}
const factory {name_str}{constructor_postfix}({{{constructor_params}}}) = _{name_str};
{methods_str}
{extra_body}
}}",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl<'a> StructRefApiDartGenerator<'a> {
metadata: &str,
methods: &[String],
constructor_postfix: &str,
extra_body: &str,
) -> String {
let field_declarations = self.generate_field_declarations(src);
let constructor_params = self.generate_mode_non_freezed_constructor_params(src);
Expand All @@ -28,8 +29,16 @@ impl<'a> StructRefApiDartGenerator<'a> {
let implements_exception = generate_dart_maybe_implements_exception(self.ir.is_exception);
let methods_str = methods.join("\n");

let hashcode = generate_hashcode(&src.fields);
let equals = generate_equals(&src.fields, name_str);
let hashcode = if src.generate_hash {
generate_hashcode(&src.fields)
} else {
"".to_owned()
};
let equals = if src.generate_eq {
generate_equals(&src.fields, name_str)
} else {
"".to_owned()
};

format!(
"{comments}{metadata}class {name_str} {implements_exception} {{
Expand All @@ -38,6 +47,7 @@ impl<'a> StructRefApiDartGenerator<'a> {
{maybe_const}{name_str}{constructor_postfix}({constructor_params});
{methods_str}
{extra_body}
{hashcode}
Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/ir/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct IrPack {
pub funcs: Vec<IrFunc>,
pub struct_pool: IrStructPool,
pub enum_pool: IrEnumPool,
pub dart_code_of_type: HashMap<String, String>,
pub existing_handler: Option<NamespacedName>,
pub unused_types: Vec<NamespacedName>,
}
Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/codegen/ir/ty/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct IrStruct {
pub is_fields_named: bool,
pub dart_metadata: Vec<IrDartAnnotation>,
pub ignore: bool,
pub generate_hash: bool,
pub generate_eq: bool,
pub comments: Vec<IrComment>,
}
}
Expand Down
Loading

0 comments on commit 5cda35a

Please sign in to comment.