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

Support inserting arbitrary Dart code; Support disabling default Hash/Eq generation #1860

Merged
merged 50 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
b7e5383
refactor: simp
fzyzcjy Apr 7, 2024
225313e
refactor: more
fzyzcjy Apr 7, 2024
13635f3
fix: more
fzyzcjy Apr 7, 2024
3194f68
fix: more
fzyzcjy Apr 7, 2024
6c7dde4
chore: keyword
fzyzcjy Apr 7, 2024
5ea47f5
feat: test
fzyzcjy Apr 7, 2024
71aaf09
chore: fmt
fzyzcjy Apr 7, 2024
516eb8e
feat: dart test
fzyzcjy Apr 7, 2024
e9b63cc
feat: parse
fzyzcjy Apr 7, 2024
722c26a
feat: getter
fzyzcjy Apr 7, 2024
32a9872
feat: more tests
fzyzcjy Apr 7, 2024
d9c35b5
feat: fill dart_code_of_type
fzyzcjy Apr 7, 2024
ec2d633
fix: compile
fzyzcjy Apr 7, 2024
948cde2
chore: rename
fzyzcjy Apr 7, 2024
af6662b
feat: insert dart_code
fzyzcjy Apr 7, 2024
a2526e0
chore: extract
fzyzcjy Apr 7, 2024
37f3517
feat: use extra_body
fzyzcjy Apr 7, 2024
7c69717
feat: pass aorund
fzyzcjy Apr 7, 2024
214147e
chore: arg
fzyzcjy Apr 7, 2024
0c784a3
feat: impl
fzyzcjy Apr 7, 2024
bee48f9
fix: compile
fzyzcjy Apr 7, 2024
92d58c4
feat: usage
fzyzcjy Apr 7, 2024
874a8c0
feat: setter
fzyzcjy Apr 7, 2024
05103a4
feat: attrs
fzyzcjy Apr 7, 2024
e24d853
feat: simp test
fzyzcjy Apr 7, 2024
348ba8e
fix: compile
fzyzcjy Apr 7, 2024
7c53c01
chore: lint
fzyzcjy Apr 7, 2024
f046194
chore: codegen
fzyzcjy Apr 7, 2024
8ad484a
chore: update goldens
fzyzcjy Apr 7, 2024
5dc7f82
feat: more test
fzyzcjy Apr 7, 2024
37126a5
chore: codegen
fzyzcjy Apr 7, 2024
77ad8d8
chore: doc
fzyzcjy Apr 7, 2024
d34c33d
chore: fix err
fzyzcjy Apr 7, 2024
c789971
chore: codegen
fzyzcjy Apr 7, 2024
48a95cd
Merge branch 'master' into feat/11953
fzyzcjy Apr 8, 2024
2df965e
chore: codegen
fzyzcjy Apr 8, 2024
8158724
chore: fix test
fzyzcjy Apr 8, 2024
20ba160
chore: more
fzyzcjy Apr 8, 2024
9f94dbb
chore: codegen
fzyzcjy Apr 8, 2024
41d79f5
fix: simple
fzyzcjy Apr 8, 2024
755ce08
chore: codegen
fzyzcjy Apr 8, 2024
74fd834
Merge branch 'master' into feat/11953
fzyzcjy Apr 8, 2024
9d5532e
chore: test
fzyzcjy Apr 8, 2024
23b5aa9
chore: codegen
fzyzcjy Apr 8, 2024
67c4e25
chore: fix
fzyzcjy Apr 8, 2024
ed22938
feat: more keys
fzyzcjy Apr 8, 2024
c38b6f3
fix: compile
fzyzcjy Apr 8, 2024
1bf9b5e
fix: typo
fzyzcjy Apr 8, 2024
63d9ef7
chore: codegen
fzyzcjy Apr 8, 2024
27acac3
Merge branch 'master' into feat/11953
fzyzcjy Apr 8, 2024
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
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
Loading