in invoker/core/src/main/java/com/google/cloud/functions/invoker/GcfEvents.java [140:162]
final CloudEvent convertToCloudEvent(Event legacyEvent) {
String jsonData = GSON.toJson(legacyEvent.getData());
jsonData = maybeReshapeData(legacyEvent, jsonData);
Resource resource = Resource.from(legacyEvent.getContext().resource());
String service = Optional.ofNullable(resource.service()).orElse(defaultService);
String resourceName = resource.name();
SourceAndSubject sourceAndSubject =
convertResourceToSourceAndSubject(resourceName, legacyEvent);
URI source = URI.create("//" + service + "/" + sourceAndSubject.source());
OffsetDateTime timestamp =
Optional.ofNullable(legacyEvent.getContext().timestamp())
.map(s -> OffsetDateTime.parse(s, DateTimeFormatter.ISO_DATE_TIME))
.orElse(null);
return CloudEventBuilder.v1()
.withData(jsonData.getBytes(UTF_8))
.withDataContentType("application/json")
.withId(legacyEvent.getContext().eventId())
.withSource(source)
.withSubject(sourceAndSubject.subject())
.withTime(timestamp)
.withType(cloudEventType)
.build();
}