Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

support event input and output as optional to generate dart code

appflowy6f3a03fa07d9
diff --git a/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart b/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart
index 8091ac05b9..af156dbce5 100644
--- a/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart
+++ b/app_flowy/packages/flowy_sdk/lib/dispatch/code_gen.dart
@@ -2,47 +2,75 @@
/// Auto gen code from rust ast, do not edit
part of 'dispatch.dart';
+class UserEventGetStatus {
+ UserEventGetStatus();
+
+ Future<Either<UserDetail, FlowyError>> send() {
+ final request = FFIRequest.create()
+ ..event = UserEvent.GetStatus.toString();
+
+ return Dispatch.asyncRequest(request).then((bytesResult) => bytesResult.fold(
+ (bytes) => left(UserDetail.fromBuffer(bytes)),
+ (error) => right(error),
+ ));
+ }
+}
+
class UserEventSignIn {
- SignInRequest request;
- UserEventSignIn(this.request);
+ SignInRequest request;
+ UserEventSignIn(this.request);
Future<Either<UserDetail, FlowyError>> send() {
- return requestToBytes(request).fold(
- (bytes) {
- final request = FFIRequest.create()
- ..event = UserEvent.SignIn.toString()
- ..payload = bytes;
-
- return Dispatch.asyncRequest(request)
- .then((bytesResult) => bytesResult.fold(
- (bytes) => left(UserDetail.fromBuffer(bytes)),
- (error) => right(error),
- ));
- },
- (err) => Future(() => right(err)),
- );
+ return requestToBytes(request).fold(
+ (bytes) {
+ final request = FFIRequest.create()
+ ..event = UserEvent.SignIn.toString()
+ ..payload = bytes;
+
+ return Dispatch.asyncRequest(request)
+ .then((bytesResult) => bytesResult.fold(
+ (bytes) => left(UserDetail.fromBuffer(bytes)),
+ (error) => right(error),
+ ));
+ },
+ (err) => Future(() => right(err)),
+ );
}
}
class UserEventSignUp {
- SignUpRequest request;
- UserEventSignUp(this.request);
+ SignUpRequest request;
+ UserEventSignUp(this.request);
Future<Either<UserDetail, FlowyError>> send() {
- return requestToBytes(request).fold(
- (bytes) {
- final request = FFIRequest.create()
- ..event = UserEvent.SignUp.toString()
- ..payload = bytes;
-
- return Dispatch.asyncRequest(request)
- .then((bytesResult) => bytesResult.fold(
- (bytes) => left(UserDetail.fromBuffer(bytes)),
- (error) => right(error),
- ));
- },
- (err) => Future(() => right(err)),
- );
+ return requestToBytes(request).fold(
+ (bytes) {
+ final request = FFIRequest.create()
+ ..event = UserEvent.SignUp.toString()
+ ..payload = bytes;
+
+ return Dispatch.asyncRequest(request)
+ .then((bytesResult) => bytesResult.fold(
+ (bytes) => left(UserDetail.fromBuffer(bytes)),
+ (error) => right(error),
+ ));
+ },
+ (err) => Future(() => right(err)),
+ );
+ }
+}
+
+class UserEventSignOut {
+ UserEventSignOut();
+
+ Future<Either<Uint8List, FlowyError>> send() {
+ final request = FFIRequest.create()
+ ..event = UserEvent.SignOut.toString();
+
+ return Dispatch.asyncRequest(request).then((bytesResult) => bytesResult.fold(
+ (bytes) => left(bytes),
+ (error) => right(error),
+ ));
}
}
diff --git a/rust-lib/flowy-infra/Flowy.toml b/rust-lib/flowy-infra/Flowy.toml
index 32812aa4d5..49670b944a 100644
--- a/rust-lib/flowy-infra/Flowy.toml
+++ b/rust-lib/flowy-infra/Flowy.toml
@@ -1,2 +1,2 @@
proto_crates = ["src/kv"]
-event_files = [""]
\ No newline at end of file
+event_files = []
\ No newline at end of file
diff --git a/rust-lib/flowy-user/src/event.rs b/rust-lib/flowy-user/src/event.rs
index 2683d41d4c..502ec1d566 100644
--- a/rust-lib/flowy-user/src/event.rs
+++ b/rust-lib/flowy-user/src/event.rs
@@ -4,6 +4,7 @@ use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
pub enum UserEvent {
#[display(fmt = "GetStatus")]
+ #[event(output = "UserDetail")]
GetStatus = 0,
#[display(fmt = "SignIn")]
#[event(input = "SignInRequest", output = "UserDetail")]
@@ -12,5 +13,6 @@ pub enum UserEvent {
#[event(input = "SignUpRequest", output = "UserDetail")]
SignUp = 2,
#[display(fmt = "SignOut")]
+ #[event()]
SignOut = 3,
}
diff --git a/scripts/flowy-tool/src/dart_event/dart_event.rs b/scripts/flowy-tool/src/dart_event/dart_event.rs
index 536667f161..c3ab1db07a 100644
--- a/scripts/flowy-tool/src/dart_event/dart_event.rs
+++ b/scripts/flowy-tool/src/dart_event/dart_event.rs
@@ -100,24 +100,23 @@ pub fn parse_event_crate(event_crate: &DartEventCrate) -> Vec<EventASTContext> {
pub fn ast_to_event_render_ctx(ast: &Vec<EventASTContext>) -> Vec<EventRenderContext> {
ast.iter()
- .filter(|event_ast| event_ast.event_input.is_some() && event_ast.event_output.is_some())
- .map(|event_ast| EventRenderContext {
- input_deserializer: event_ast
- .event_input
- .as_ref()
- .unwrap()
- .get_ident()
- .unwrap()
- .to_string(),
- output_deserializer: event_ast
- .event_output
- .as_ref()
- .unwrap()
- .get_ident()
- .unwrap()
- .to_string(),
- event: event_ast.event.to_string(),
- event_ty: event_ast.event_ty.to_string(),
+ .map(|event_ast| {
+ let input_deserializer = match event_ast.event_input {
+ Some(ref event_input) => Some(event_input.get_ident().unwrap().to_string()),
+ None => None,
+ };
+
+ let output_deserializer = match event_ast.event_output {
+ Some(ref event_output) => Some(event_output.get_ident().unwrap().to_string()),
+ None => None,
+ };
+
+ return EventRenderContext {
+ input_deserializer,
+ output_deserializer,
+ event: event_ast.event.to_string(),
+ event_ty: event_ast.event_ty.to_string(),
+ };
})
.collect::<Vec<EventRenderContext>>()
}
diff --git a/scripts/flowy-tool/src/dart_event/event_template.rs b/scripts/flowy-tool/src/dart_event/event_template.rs
index dfaa830088..194ae22c6c 100644
--- a/scripts/flowy-tool/src/dart_event/event_template.rs
+++ b/scripts/flowy-tool/src/dart_event/event_template.rs
@@ -11,8 +11,8 @@ part of 'dispatch.dart';
"#;
pub struct EventRenderContext {
- pub input_deserializer: String,
- pub output_deserializer: String,
+ pub input_deserializer: Option<String>,
+ pub output_deserializer: Option<String>,
pub event: String,
pub event_ty: String,
}
@@ -31,16 +31,24 @@ impl EventTemplate {
.insert("imported_dart_files", DART_IMPORTED)
}
self.tera_context.insert("index", &index);
-
let dart_class_name = format!("{}{}", ctx.event_ty, ctx.event);
let event = format!("{}.{}", ctx.event_ty, ctx.event);
-
self.tera_context.insert("event_class", &dart_class_name);
self.tera_context.insert("event", &event);
+
self.tera_context
- .insert("input_deserializer", &ctx.input_deserializer);
+ .insert("has_input", &ctx.input_deserializer.is_some());
+ match ctx.input_deserializer {
+ None => self.tera_context.insert("input_deserializer", "Uint8List"),
+ Some(ref input) => self.tera_context.insert("input_deserializer", input),
+ }
+
self.tera_context
- .insert("output_deserializer", &ctx.output_deserializer);
+ .insert("has_output", &ctx.output_deserializer.is_some());
+ match ctx.output_deserializer {
+ None => self.tera_context.insert("output_deserializer", "Uint8List"),
+ Some(ref output) => self.tera_context.insert("output_deserializer", output),
+ }
let tera = get_tera("dart_event");
match tera.render("event_template.tera", &self.tera_context) {
diff --git a/scripts/flowy-tool/src/dart_event/event_template.tera b/scripts/flowy-tool/src/dart_event/event_template.tera
index 476b5e93ef..a88f116401 100644
--- a/scripts/flowy-tool/src/dart_event/event_template.tera
+++ b/scripts/flowy-tool/src/dart_event/event_template.tera
@@ -3,24 +3,46 @@
{%- endif -%}
class {{ event_class }} {
- {{ input_deserializer }} request;
- {{ event_class }}(this.request);
+{%- if has_input %}
+ {{ input_deserializer }} request;
+ {{ event_class }}(this.request);
+{%- else %}
+ {{ event_class }}();
+{%- endif %}
Future<Either<{{ output_deserializer }}, FlowyError>> send() {
- return requestToBytes(request).fold(
- (bytes) {
- final request = FFIRequest.create()
- ..event = {{ event }}.toString()
- ..payload = bytes;
- return Dispatch.asyncRequest(request)
- .then((bytesResult) => bytesResult.fold(
- (bytes) => left({{ output_deserializer }}.fromBuffer(bytes)),
- (error) => right(error),
- ));
- },
- (err) => Future(() => right(err)),
- );
+{%- if has_input %}
+ return requestToBytes(request).fold(
+ (bytes) {
+ final request = FFIRequest.create()
+ ..event = {{ event }}.toString()
+ ..payload = bytes;
+
+ return Dispatch.asyncRequest(request)
+ .then((bytesResult) => bytesResult.fold(
+ (bytes) => left({{ output_deserializer }}.fromBuffer(bytes)),
+ (error) => right(error),
+ ));
+ },
+ (err) => Future(() => right(err)),
+ );
+{%- else %}
+ final request = FFIRequest.create()
+ ..event = {{ event }}.toString();
+ {%- if has_input %}
+ ..payload = bytes;
+ {%- endif %}
+
+ return Dispatch.asyncRequest(request).then((bytesResult) => bytesResult.fold(
+ {%- if has_output %}
+ (bytes) => left({{ output_deserializer }}.fromBuffer(bytes)),
+ {%- else %}
+ (bytes) => left(bytes),
+ {%- endif %}
+ (error) => right(error),
+ ));
+{%- endif %}
}
}