Repository navigation
Expand file tree
/
Copy pathruntime_integration_example.rs
More file actions
471 lines (398 loc) · 14.7 KB
/
Copy pathruntime_integration_example.rs
File metadata and controls
471 lines (398 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Hiver Annotations Runtime Integration Test
//! Hiver 注解运行时集成测试
//!
//! This example demonstrates the runtime integration of all annotation features:
//! - Query execution with @Query, @Insert, @Update, @Delete
//! - Validation with @Valid and validation annotations
//! - AOP with @Aspect, @Before, @After, @Around
//! - Transactions with @Transactional
//!
//! 此示例演示了所有注解功能的运行时集成:
//! - 使用 @Query、@Insert、@Update、@Delete 进行查询执行
//! - 使用 @Valid 和验证注解进行验证
//! - 使用 @Aspect、@Before、@After、@Around 进行 AOP
//! - 使用 @Transactional 进行事务管理
use std::collections::HashMap;
use std::sync::Arc;
// ============================================================================
// Part 1: Query Runtime / 查询运行时
// ============================================================================
#[allow(dead_code)]
fn demo_query_runtime() {
use hiver_data_rdbc::{QueryMetadata, ParamStyle, QueryType};
// Define query metadata (normally extracted from @Query annotation)
// 定义查询元数据(通常从 @Query 注解中提取)
let metadata = QueryMetadata {
sql: "SELECT * FROM users WHERE id = :id".to_string(),
param_style: ParamStyle::Named,
param_names: vec!["id".to_string()],
query_type: QueryType::SelectOne,
};
println!("✅ Query metadata created");
println!(" SQL: {}", metadata.sql);
println!(" Params: {:?}", metadata.param_names);
}
// ============================================================================
// Part 2: Validation Runtime / 验证运行时
// ============================================================================
#[allow(dead_code)]
fn demo_validation_runtime() {
use hiver_http::validation::{ValidationHelpers, ValidationErrors};
// Create validation errors
// 创建验证错误
let mut errors = ValidationErrors::new();
// Test validation helpers
// 测试验证辅助函数
let username = "";
if let Some(error) = ValidationHelpers::require_non_empty("username", username) {
errors.add(error);
}
let email = "invalid-email";
if let Some(error) = ValidationHelpers::require_email_format("email", email) {
errors.add(error);
}
let password = "short";
if let Some(error) = ValidationHelpers::require_min_length("password", password, 8) {
errors.add(error);
}
let age = 15;
if let Some(error) = ValidationHelpers::require_min("age", age, 18) {
errors.add(error);
}
println!("✅ Validation runtime test");
println!(" Errors found: {}", errors.error_count());
for error in errors.iter() {
println!(" - {}: {}", error.field(), error.message());
}
}
// ============================================================================
// Part 3: AOP Runtime / AOP 运行时
// ============================================================================
#[allow(dead_code)]
fn demo_aop_runtime() {
use hiver_aop::runtime::{JoinPoint, PointcutExpression, global_registry};
use std::any::Any;
// Create a join point
// 创建连接点
let target: Arc<dyn Any + Send + Sync> = Arc::new("UserService");
let args: Vec<Arc<dyn Any + Send + Sync>> = vec![Arc::new(42), Arc::new("alice")];
let join_point = JoinPoint::new(
target,
"find_by_id".to_string(),
args,
"find_by_id(i64)".to_string(),
"UserService".to_string(),
);
println!("✅ AOP runtime test");
println!(" Join point: {}", join_point.method_name());
println!(" Target class: {}", join_point.target_class());
// Create a pointcut expression
// 创建切点表达式
let pointcut = PointcutExpression::new("execution(* com.example..*.*(..))".to_string());
println!(" Pointcut expression: {}", pointcut.expression());
// Check if pointcut matches
// 检查切点是否匹配
let matches = pointcut.matches(&join_point);
println!(" Pointcut matches: {}", matches);
// Access global registry
// 访问全局注册表
let registry = global_registry();
println!(" Global registry: accessible");
}
// ============================================================================
// Part 4: Transactional Runtime / 事务运行时
// ============================================================================
#[allow(dead_code)]
fn demo_transactional_runtime() {
use hiver_data_annotations::transactional::{
TransactionalConfig, IsolationLevel, Propagation,
};
// Create transactional configuration
// 创建事务配置
let config = TransactionalConfig::new()
.isolation(IsolationLevel::ReadCommitted)
.timeout(30)
.propagation(Propagation::Required)
.read_only(false)
.max_retries(3);
println!("✅ Transactional runtime test");
println!(" Isolation: {:?}", config.isolation);
println!(" Timeout: {:?}", config.timeout);
println!(" Propagation: {:?}", config.propagation);
println!(" Read-only: {}", config.read_only);
println!(" Max retries: {}", config.max_retries);
}
// ============================================================================
// Part 5: Integrated Example / 集成示例
// ============================================================================
/// User entity with all annotations
/// 带有所有注解的用户实体
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct User {
id: i64,
username: String,
email: String,
age: i32,
}
/// Validation implementation for User
/// User 的验证实现
impl hiver_http::validation::Validatable for User {
fn validate(&self) -> Result<(), hiver_http::validation::ValidationErrors> {
let mut errors = hiver_http::validation::ValidationErrors::new();
// Validate username
// 验证用户名
if let Some(error) = hiver_http::validation::ValidationHelpers::require_non_empty(
"username",
&self.username,
) {
errors.add(error);
}
if let Some(error) = hiver_http::validation::ValidationHelpers::require_min_length(
"username",
&self.username,
3,
) {
errors.add(error);
}
// Validate email
// 验证邮箱
if let Some(error) = hiver_http::validation::ValidationHelpers::require_email_format(
"email",
&self.email,
) {
errors.add(error);
}
// Validate age
// 验证年龄
if let Some(error) = hiver_http::validation::ValidationHelpers::require_min("age", self.age, 18)
{
errors.add(error);
}
if errors.has_errors() {
Err(errors)
} else {
Ok(())
}
}
}
/// Service class with transactional methods
/// 带有事务方法的服务类
struct UserService {
users: Vec<User>,
}
impl UserService {
fn new() -> Self {
Self {
users: vec![
User {
id: 1,
username: "alice".to_string(),
email: "alice@example.com".to_string(),
age: 25,
},
User {
id: 2,
username: "bob".to_string(),
email: "bob@example.com".to_string(),
age: 30,
},
],
}
}
/// Find user by ID (would use @Query in real implementation)
/// 通过 ID 查找用户(实际实现中会使用 @Query)
#[allow(dead_code)]
fn find_by_id(&self, id: i64) -> Option<User> {
self.users.iter().find(|u| u.id == id).cloned()
}
/// Create user (would use @Transactional in real implementation)
/// 创建用户(实际实现中会使用 @Transactional)
#[allow(dead_code)]
fn create_user(&mut self, user: User) -> Result<(), hiver_http::validation::ValidationErrors> {
// Validate user
// 验证用户
user.validate()?;
// In real implementation, this would be in a transaction
// 在实际实现中,这将在事务中执行
self.users.push(user);
Ok(())
}
/// Update user email (would use @Update and @Transactional)
/// 更新用户邮箱(实际实现中会使用 @Update 和 @Transactional)
#[allow(dead_code)]
fn update_email(&mut self, id: i64, email: String) -> Result<(), String> {
if let Some(user) = self.users.iter_mut().find(|u| u.id == id) {
user.email = email;
Ok(())
} else {
Err("User not found".to_string())
}
}
/// Delete user (would use @Delete and @Transactional)
/// 删除用户(实际实现中会使用 @Delete 和 @Transactional)
#[allow(dead_code)]
fn delete_user(&mut self, id: i64) -> Result<(), String> {
let original_len = self.users.len();
self.users.retain(|u| u.id != id);
if self.users.len() < original_len {
Ok(())
} else {
Err("User not found".to_string())
}
}
}
#[allow(dead_code)]
fn demo_integrated_example() {
println!("🚀 Integrated Example\n");
// Create service
// 创建服务
let mut service = UserService::new();
// Find user
// 查找用户
if let Some(user) = service.find_by_id(1) {
println!("✅ Found user: {:?}", user);
}
// Create user - validation should pass
// 创建用户 - 验证应该通过
let new_user = User {
id: 3,
username: "charlie".to_string(),
email: "charlie@example.com".to_string(),
age: 28,
};
match service.create_user(new_user.clone()) {
Ok(_) => println!("✅ User created: {:?}", new_user),
Err(errors) => println!("❌ Validation failed: {}", errors.error_count()),
}
// Create user - validation should fail
// 创建用户 - 验证应该失败
let invalid_user = User {
id: 4,
username: "".to_string(), // Too short / 太短
email: "invalid-email".to_string(), // Invalid format / 格式无效
age: 15, // Too young / 太小
};
match service.create_user(invalid_user) {
Ok(_) => println!("✅ User created"),
Err(errors) => {
println!("❌ Validation failed:");
for error in errors.iter() {
println!(" - {}: {}", error.field(), error.message());
}
}
}
// Update email
// 更新邮箱
match service.update_email(1, "alice.new@example.com".to_string()) {
Ok(_) => println!("✅ Email updated"),
Err(e) => println!("❌ Update failed: {}", e),
}
// Delete user
// 删除用户
match service.delete_user(2) {
Ok(_) => println!("✅ User deleted"),
Err(e) => println!("❌ Delete failed: {}", e),
}
}
// ============================================================================
// Main / 主函数
// ============================================================================
fn main() {
println!("╔════════════════════════════════════════════════════════════╗");
println!("║ Hiver Annotations Runtime Integration Test ║");
println!("║ Hiver 注解运行时集成测试 ║");
println!("╚════════════════════════════════════════════════════════════╝\n");
// Part 1: Query runtime
// 第一部分:查询运行时
println!("📊 Part 1: Query Runtime");
demo_query_runtime();
println!();
// Part 2: Validation runtime
// 第二部分:验证运行时
println!("✓ Part 2: Validation Runtime");
demo_validation_runtime();
println!();
// Part 3: AOP runtime
// 第三部分:AOP 运行时
println!("✓ Part 3: AOP Runtime");
demo_aop_runtime();
println!();
// Part 4: Transactional runtime
// 第四部分:事务运行时
println!("✓ Part 4: Transactional Runtime");
demo_transactional_runtime();
println!();
// Part 5: Integrated example
// 第五部分:集成示例
println!("✓ Part 5: Integrated Example");
demo_integrated_example();
println!();
println!("╔════════════════════════════════════════════════════════════╗");
println!("║ All runtime components integrated successfully! ║");
println!("║ 所有运行时组件集成成功! ║");
println!("╚════════════════════════════════════════════════════════════╝");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_runtime() {
demo_query_runtime();
}
#[test]
fn test_validation_runtime() {
demo_validation_runtime();
}
#[test]
fn test_aop_runtime() {
demo_aop_runtime();
}
#[test]
fn test_transactional_runtime() {
demo_transactional_runtime();
}
#[test]
fn test_integrated_example() {
demo_integrated_example();
}
#[test]
fn test_user_validation() {
let valid_user = User {
id: 1,
username: "alice".to_string(),
email: "alice@example.com".to_string(),
age: 25,
};
assert!(valid_user.validate().is_ok());
let invalid_user = User {
id: 2,
username: "".to_string(),
email: "invalid".to_string(),
age: 15,
};
assert!(invalid_user.validate().is_err());
}
#[test]
fn test_user_service() {
let mut service = UserService::new();
// Test find
// 测试查找
assert!(service.find_by_id(1).is_some());
assert!(service.find_by_id(999).is_none());
// Test create
// 测试创建
let new_user = User {
id: 3,
username: "bob".to_string(),
email: "bob@example.com".to_string(),
age: 30,
};
assert!(service.create_user(new_user).is_ok());
// Test update
// 测试更新
assert!(service.update_email(1, "new@example.com".to_string()).is_ok());
// Test delete
// 测试删除
assert!(service.delete_user(2).is_ok());
}
}