|
| 1 | +package dev.arcp.envelope; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 7 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 8 | +import dev.arcp.ids.IdempotencyKey; |
| 9 | +import dev.arcp.ids.JobId; |
| 10 | +import dev.arcp.ids.MessageId; |
| 11 | +import dev.arcp.ids.SessionId; |
| 12 | +import dev.arcp.ids.SpanId; |
| 13 | +import dev.arcp.ids.StreamId; |
| 14 | +import dev.arcp.ids.SubscriptionId; |
| 15 | +import dev.arcp.ids.TraceId; |
| 16 | +import java.io.IOException; |
| 17 | +import java.time.Instant; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.Map; |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Custom Jackson deserializer for {@link Envelope}. Resolves the envelope-level |
| 25 | + * {@code type} discriminator (RFC §6.1.1) to a concrete {@link MessageType} |
| 26 | + * variant via {@link MessageType#register(String, Class)}. |
| 27 | + */ |
| 28 | +final class EnvelopeDeserializer extends StdDeserializer<Envelope> { |
| 29 | + |
| 30 | + private static final long serialVersionUID = 1L; |
| 31 | + |
| 32 | + EnvelopeDeserializer() { |
| 33 | + super(Envelope.class); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Envelope deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
| 38 | + ObjectNode node = p.readValueAsTree(); |
| 39 | + String type = requireText(node, "type"); |
| 40 | + Class<? extends MessageType> payloadClass = MessageType.Registry.resolve(type); |
| 41 | + JsonNode payloadNode = node.get("payload"); |
| 42 | + if (payloadNode == null) { |
| 43 | + throw ctxt.weirdStringException(type, Envelope.class, "missing payload"); |
| 44 | + } |
| 45 | + MessageType payload = ctxt.readTreeAsValue(payloadNode, payloadClass); |
| 46 | + |
| 47 | + return new Envelope(requireText(node, "arcp"), readId(node, "id", MessageId.class, ctxt), type, |
| 48 | + readInstant(node, "timestamp", ctxt), readNullable(node, "session_id", SessionId.class, ctxt), |
| 49 | + readNullable(node, "job_id", JobId.class, ctxt), readNullable(node, "stream_id", StreamId.class, ctxt), |
| 50 | + readNullable(node, "subscription_id", SubscriptionId.class, ctxt), |
| 51 | + readNullable(node, "trace_id", TraceId.class, ctxt), readNullable(node, "span_id", SpanId.class, ctxt), |
| 52 | + readNullable(node, "parent_span_id", SpanId.class, ctxt), |
| 53 | + readNullable(node, "correlation_id", MessageId.class, ctxt), |
| 54 | + readNullable(node, "causation_id", MessageId.class, ctxt), |
| 55 | + readNullable(node, "idempotency_key", IdempotencyKey.class, ctxt), |
| 56 | + readNullable(node, "priority", Priority.class, ctxt), readText(node, "source"), |
| 57 | + readText(node, "target"), readExtensions(node), payload); |
| 58 | + } |
| 59 | + |
| 60 | + private static String requireText(ObjectNode node, String field) { |
| 61 | + JsonNode v = node.get(field); |
| 62 | + if (v == null || v.isNull()) { |
| 63 | + throw new IllegalArgumentException("envelope missing required field " + field); |
| 64 | + } |
| 65 | + return v.asText(); |
| 66 | + } |
| 67 | + |
| 68 | + private static <T> T readId(ObjectNode node, String field, Class<T> cls, DeserializationContext ctxt) |
| 69 | + throws IOException { |
| 70 | + JsonNode v = node.get(field); |
| 71 | + if (v == null || v.isNull()) { |
| 72 | + throw new IllegalArgumentException("envelope missing required field " + field); |
| 73 | + } |
| 74 | + return ctxt.readTreeAsValue(v, cls); |
| 75 | + } |
| 76 | + |
| 77 | + private static Instant readInstant(ObjectNode node, String field, DeserializationContext ctxt) throws IOException { |
| 78 | + JsonNode v = node.get(field); |
| 79 | + if (v == null || v.isNull()) { |
| 80 | + throw new IllegalArgumentException("envelope missing required field " + field); |
| 81 | + } |
| 82 | + return ctxt.readTreeAsValue(v, Instant.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + private static <T> T readNullable(ObjectNode node, String field, Class<T> cls, DeserializationContext ctxt) |
| 87 | + throws IOException { |
| 88 | + JsonNode v = node.get(field); |
| 89 | + if (v == null || v.isNull()) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + return ctxt.readTreeAsValue(v, cls); |
| 93 | + } |
| 94 | + |
| 95 | + @Nullable |
| 96 | + private static String readText(ObjectNode node, String field) { |
| 97 | + JsonNode v = node.get(field); |
| 98 | + return (v == null || v.isNull()) ? null : v.asText(); |
| 99 | + } |
| 100 | + |
| 101 | + @Nullable |
| 102 | + private static Map<String, JsonNode> readExtensions(ObjectNode node) { |
| 103 | + JsonNode v = node.get("extensions"); |
| 104 | + if (v == null || v.isNull()) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + if (!v.isObject()) { |
| 108 | + throw new IllegalArgumentException("extensions must be an object"); |
| 109 | + } |
| 110 | + Map<String, JsonNode> out = new HashMap<>(); |
| 111 | + Iterator<Map.Entry<String, JsonNode>> it = v.properties().iterator(); |
| 112 | + while (it.hasNext()) { |
| 113 | + Map.Entry<String, JsonNode> e = it.next(); |
| 114 | + out.put(e.getKey(), e.getValue()); |
| 115 | + } |
| 116 | + return out; |
| 117 | + } |
| 118 | +} |
0 commit comments