Skip to content

Commit 26f1f87

Browse files
committed
go
1 parent 0647078 commit 26f1f87

File tree

1 file changed

+154
-12
lines changed

1 file changed

+154
-12
lines changed

lib/src/database/database_namespace.dart

Lines changed: 154 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,62 @@ class DatabaseNamespace extends FunctionsNamespace {
325325

326326
return Response.ok('');
327327
} else {
328-
return Response(
329-
501,
330-
body:
331-
'Structured CloudEvent mode not yet supported for onValueUpdated',
328+
// Structured content mode: full CloudEvent in JSON body
329+
final bodyString = await request.readAsString();
330+
final json = parseCloudEventJson(bodyString);
331+
validateCloudEvent(json);
332+
333+
if (!_isUpdatedEvent(json['type'] as String)) {
334+
return Response(
335+
400,
336+
body:
337+
'Invalid event type for Database onValueUpdated: ${json['type']}',
338+
);
339+
}
340+
341+
final eventData = json['data'] as Map<String, dynamic>?;
342+
final beforeData = eventData?['data'];
343+
final deltaData = eventData?['delta'];
344+
final afterData = _applyDelta(beforeData, deltaData);
345+
346+
final refPath = json['ref'] as String? ?? '';
347+
final instanceName = json['instance'] as String? ?? instance;
348+
349+
final beforeSnapshot = DataSnapshot(
350+
instance: instanceName,
351+
ref: refPath,
352+
data: beforeData,
353+
);
354+
final afterSnapshot = DataSnapshot(
355+
instance: instanceName,
356+
ref: refPath,
357+
data: afterData,
358+
);
359+
final change = Change<DataSnapshot>(
360+
before: beforeSnapshot,
361+
after: afterSnapshot,
362+
);
363+
364+
final params = _extractParams(ref, refPath);
365+
366+
final event = DatabaseEvent<Change<DataSnapshot>?>(
367+
data: change,
368+
id: json['id'] as String,
369+
source: json['source'] as String,
370+
specversion: json['specversion'] as String,
371+
subject: json['subject'] as String?,
372+
time: DateTime.parse(json['time'] as String),
373+
type: json['type'] as String,
374+
firebaseDatabaseHost:
375+
json['firebasedatabasehost'] as String? ?? '',
376+
instance: instanceName,
377+
ref: refPath,
378+
location: json['location'] as String? ?? 'us-central1',
379+
params: params,
332380
);
381+
382+
await handler(event);
383+
return Response.ok('');
333384
}
334385
} catch (e, stackTrace) {
335386
return Response(
@@ -450,11 +501,51 @@ class DatabaseNamespace extends FunctionsNamespace {
450501

451502
return Response.ok('');
452503
} else {
453-
return Response(
454-
501,
455-
body:
456-
'Structured CloudEvent mode not yet supported for onValueDeleted',
504+
// Structured content mode: full CloudEvent in JSON body
505+
final bodyString = await request.readAsString();
506+
final json = parseCloudEventJson(bodyString);
507+
validateCloudEvent(json);
508+
509+
if (!_isDeletedEvent(json['type'] as String)) {
510+
return Response(
511+
400,
512+
body:
513+
'Invalid event type for Database onValueDeleted: ${json['type']}',
514+
);
515+
}
516+
517+
final eventData = json['data'] as Map<String, dynamic>?;
518+
final deletedData = eventData?['data'];
519+
520+
final refPath = json['ref'] as String? ?? '';
521+
final instanceName = json['instance'] as String? ?? instance;
522+
523+
final snapshot = DataSnapshot(
524+
instance: instanceName,
525+
ref: refPath,
526+
data: deletedData,
527+
);
528+
529+
final params = _extractParams(ref, refPath);
530+
531+
final event = DatabaseEvent<DataSnapshot?>(
532+
data: snapshot,
533+
id: json['id'] as String,
534+
source: json['source'] as String,
535+
specversion: json['specversion'] as String,
536+
subject: json['subject'] as String?,
537+
time: DateTime.parse(json['time'] as String),
538+
type: json['type'] as String,
539+
firebaseDatabaseHost:
540+
json['firebasedatabasehost'] as String? ?? '',
541+
instance: instanceName,
542+
ref: refPath,
543+
location: json['location'] as String? ?? 'us-central1',
544+
params: params,
457545
);
546+
547+
await handler(event);
548+
return Response.ok('');
458549
}
459550
} catch (e, stackTrace) {
460551
return Response(
@@ -609,11 +700,62 @@ class DatabaseNamespace extends FunctionsNamespace {
609700

610701
return Response.ok('');
611702
} else {
612-
return Response(
613-
501,
614-
body:
615-
'Structured CloudEvent mode not yet supported for onValueWritten',
703+
// Structured content mode: full CloudEvent in JSON body
704+
final bodyString = await request.readAsString();
705+
final json = parseCloudEventJson(bodyString);
706+
validateCloudEvent(json);
707+
708+
if (!_isWrittenEvent(json['type'] as String)) {
709+
return Response(
710+
400,
711+
body:
712+
'Invalid event type for Database onValueWritten: ${json['type']}',
713+
);
714+
}
715+
716+
final eventData = json['data'] as Map<String, dynamic>?;
717+
final beforeData = eventData?['data'];
718+
final deltaData = eventData?['delta'];
719+
final afterData = _applyDelta(beforeData, deltaData);
720+
721+
final refPath = json['ref'] as String? ?? '';
722+
final instanceName = json['instance'] as String? ?? instance;
723+
724+
final beforeSnapshot = DataSnapshot(
725+
instance: instanceName,
726+
ref: refPath,
727+
data: beforeData,
616728
);
729+
final afterSnapshot = DataSnapshot(
730+
instance: instanceName,
731+
ref: refPath,
732+
data: afterData,
733+
);
734+
final change = Change<DataSnapshot>(
735+
before: beforeSnapshot,
736+
after: afterSnapshot,
737+
);
738+
739+
final params = _extractParams(ref, refPath);
740+
741+
final event = DatabaseEvent<Change<DataSnapshot>?>(
742+
data: change,
743+
id: json['id'] as String,
744+
source: json['source'] as String,
745+
specversion: json['specversion'] as String,
746+
subject: json['subject'] as String?,
747+
time: DateTime.parse(json['time'] as String),
748+
type: json['type'] as String,
749+
firebaseDatabaseHost:
750+
json['firebasedatabasehost'] as String? ?? '',
751+
instance: instanceName,
752+
ref: refPath,
753+
location: json['location'] as String? ?? 'us-central1',
754+
params: params,
755+
);
756+
757+
await handler(event);
758+
return Response.ok('');
617759
}
618760
} catch (e, stackTrace) {
619761
return Response(

0 commit comments

Comments
 (0)