private static Context contextFromCloudEvent()

in invoker/core/src/main/java/com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java [198:215]


  private static Context contextFromCloudEvent(CloudEvent cloudEvent) {
    OffsetDateTime timestamp =
        Optional.ofNullable(cloudEvent.getTime()).orElse(OffsetDateTime.now());
    String timestampString = DateTimeFormatter.ISO_INSTANT.format(timestamp);
    // We don't have an obvious replacement for the Context.resource field, which with legacy events
    // corresponded to a value present for some proprietary Google event types.
    String resource = "{}";
    Map<String, String> attributesMap =
        cloudEvent.getAttributeNames().stream()
            .collect(toMap(a -> a, a -> String.valueOf(cloudEvent.getAttribute(a))));
    return CloudFunctionsContext.builder()
        .setEventId(cloudEvent.getId())
        .setEventType(cloudEvent.getType())
        .setResource(resource)
        .setTimestamp(timestampString)
        .setAttributes(attributesMap)
        .build();
  }