main function

void main()

Implementation

void main() {
  print('šŸŽÆ AnyOf with Sealed Classes Example\n');

  // Example 1: Manual AnyOf schema for UserId
  final userIdSchema = Ack.anyOf([Ack.string(), Ack.integer()]);

  print('1ļøāƒ£ Simple UserId AnyOf:');

  // Valid string ID
  try {
    final stringId = userIdSchema.parse('user_123');
    print('   āœ… String ID valid: $stringId');
  } catch (e) {
    print('   āŒ Error: $e');
  }

  // Valid numeric ID
  try {
    final numericId = userIdSchema.parse(456789);
    print('   āœ… Numeric ID valid: $numericId');
  } catch (e) {
    print('   āŒ Error: $e');
  }

  // Invalid ID (wrong type)
  try {
    userIdSchema.parse(true);
  } catch (e) {
    print('   āŒ Boolean ID rejected (as expected)');
  }

  // Example 2: Complex nested AnyOf
  print('\n2ļøāƒ£ API Response with AnyOf data:');

  // For now, we'd need to manually create the schema
  // In the future, the generator could do this automatically
  final responseDataSchema = Ack.anyOf([
    Ack.object({
      'id': Ack.string(),
      'name': Ack.string(),
      'email': Ack.string(),
    }),
    Ack.object({
      'code': Ack.string(),
      'message': Ack.string(),
      'details': Ack.object(
        {},
        additionalProperties: true,
      ).optional().nullable(),
    }),
    Ack.object({
      'items': Ack.list(Ack.string()),
      'total': Ack.integer(),
      'page': Ack.integer(),
    }),
  ]);

  final apiResponseSchema = Ack.object({
    'status': Ack.string(),
    'data': responseDataSchema,
  });

  // Test different response types
  final successResponse = {
    'status': 'success',
    'data': {'id': 'user_123', 'name': 'John Doe', 'email': 'john@example.com'},
  };

  final errorResponse = {
    'status': 'error',
    'data': {
      'code': 'AUTH_FAILED',
      'message': 'Invalid credentials',
      'details': {'attempts': 3},
    },
  };

  final listResponse = {
    'status': 'success',
    'data': {
      'items': ['item1', 'item2', 'item3'],
      'total': 50,
      'page': 1,
    },
  };

  for (final response in [successResponse, errorResponse, listResponse]) {
    try {
      final result = apiResponseSchema.parse(response);
      print(
        '   āœ… Valid response: ${response['status']} - ${(result as Map<String, dynamic>)['data']}',
      );
    } catch (e) {
      print('   āŒ Invalid response: $e');
    }
  }

  print('\nšŸ’” How AnyOf could work with code generation:');
  print('   1. Detect sealed class hierarchies');
  print('   2. Generate AnyOf schema with each subclass schema');
  print('   3. In createFromMap, use discriminator or type checking');
  print('   4. Return appropriate subclass instance');

  print('\nšŸš€ Proposed annotation:');
  print('   @AnyOf() on sealed class fields');
  print('   @Discriminator("type") for explicit discriminator fields');
  print('   Automatic detection of sealed class patterns');
}