public override byte[] EncodeStructuredModeMessage(CloudEvent cloudEvent, out ContentType contentType)
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));

            contentType = new ContentType(CloudEvent.MediaType + MediaTypeSuffix);

            // We expect the Avro encoded to detect data types that can't be represented in the schema.
            GenericRecord record = new GenericRecord(avroSchema);

            record.Add(DataName, cloudEvent.Data);
            var recordAttributes = new Dictionary <string, object>();

            recordAttributes[CloudEventsSpecVersion.SpecVersionAttribute.Name] = cloudEvent.SpecVersion.VersionId;

            foreach (var keyValuePair in cloudEvent.GetPopulatedAttributes())
            {
                var attribute = keyValuePair.Key;
                var value     = keyValuePair.Value;
                // TODO: Create a mapping method in each direction, to have this logic more clearly separated.
                var avroValue = value is bool || value is int || value is byte[] || value is string
                                ?value
                                : attribute.Format(value);
                recordAttributes[attribute.Name] = avroValue;
            }
            record.Add(AttributeName, recordAttributes);
            MemoryStream  memStream = new MemoryStream();
            BinaryEncoder encoder   = new BinaryEncoder(memStream);

            avroWriter.Write(record, encoder);
            return(memStream.ToArray());
        }
Exemple #2
0
        public byte[] EncodeStructuredEvent(CloudEvent cloudEvent, out ContentType contentType)
        {
            contentType = new ContentType(CloudEvent.MediaType + AvroEventFormatter.MediaTypeSuffix);

            GenericRecord record = new GenericRecord(avroSchema);

            record.Add(DataName, SerializeData(cloudEvent.Data));
            var recordAttributes = new Dictionary <string, object>();

            recordAttributes[CloudEventsSpecVersion.SpecVersionAttribute.Name] = cloudEvent.SpecVersion.VersionId;

            foreach (var keyValuePair in cloudEvent.GetPopulatedAttributes())
            {
                var attribute = keyValuePair.Key;
                var value     = keyValuePair.Value;
                // TODO: Create a mapping method in each direction, to have this logic more clearly separated.
                var avroValue = value is bool || value is int || value is byte[] || value is string
                                ?value
                                : attribute.Format(value);
                recordAttributes[attribute.Name] = avroValue;
            }
            record.Add("attribute", recordAttributes);
            MemoryStream  memStream = new MemoryStream();
            BinaryEncoder encoder   = new BinaryEncoder(memStream);

            avroWriter.Write(record, encoder);
            return(new Span <byte>(memStream.GetBuffer(), 0, (int)memStream.Length).ToArray());
        }