/// <summary> /// Read value from <see cref="System.Protobuf.Object"/> /// </summary> /// <param name="obj"><see cref="System.Protobuf.Object"/> to read from</param> /// <returns>Value in the correct type - null if not capable of converting</returns> public static object ToCLR(this System.Protobuf.Object obj) { var type = (Types)obj.Type; object value = null; using (var stream = new CodedInputStream(obj.Content.ToByteArray())) { switch (type) { case Types.String: value = stream.ReadString(); break; case Types.Int32: value = stream.ReadInt32(); break; case Types.Int64: value = stream.ReadInt64(); break; case Types.UInt32: value = stream.ReadUInt32(); break; case Types.UInt64: value = stream.ReadUInt64(); break; case Types.Float: value = stream.ReadFloat(); break; case Types.Double: value = stream.ReadDouble(); break; case Types.Boolean: value = stream.ReadBool(); break; case Types.DateTime: value = stream.ReadInt64().ToDateTime(); break; case Types.DateTimeOffset: value = stream.ReadInt64().ToDateTimeOffset(); break; case Types.Guid: value = new Guid(stream.ReadBytes().ToByteArray()); break; } } return(value); }
/// <summary> /// Converts a <see cref="object"/> to <see cref="System.Protobuf.Value"/>. /// </summary> /// <remarks> /// This is primarily used when converting propertybags to protobuf messages and scenarios when we don't /// know the actual type of obj. /// </remarks> /// <param name="obj"></param> /// <returns></returns> public static System.Protobuf.Value ToProtobuf(this object obj) { var value = new System.Protobuf.Value(); var objType = obj.GetType(); if (objType.IsEnumerable() && !objType.IsDictionary()) { value.ListValue = ((System.Collections.IEnumerable)obj).ToProtobuf(); } else if (objType == typeof(Dolittle.PropertyBags.PropertyBag)) { value.DictionaryValue = ((Dolittle.PropertyBags.PropertyBag)obj).ToProtobuf().AsDictionaryValue(); } else { var protobufObj = new System.Protobuf.Object(); var protobufType = obj.GetProtobufType(); protobufObj.Type = (int)protobufType; var stream = new MemoryStream(); using (var outputStream = new CodedOutputStream(stream)) { obj.WriteWithTypeTo(protobufType, outputStream); outputStream.Flush(); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); protobufObj.Content = ByteString.CopyFrom(stream.ToArray()); } value.ObjectValue = protobufObj; } return(value); }
/// <summary> /// Convert from <see cref="PropertyBag"/> to a <see cref="MapField{key,value}"/> /// </summary> /// <param name="propertyBag"><see cref="PropertyBag"/> to convert from</param> /// <returns>Converted <see cref="MapField{key,value}"/></returns> public static MapField <string, System.Protobuf.Object> ToProtobuf(this PropertyBag propertyBag) { var mapField = new MapField <string, System.Protobuf.Object>(); propertyBag.ForEach(keyValue => { var obj = new System.Protobuf.Object(); var type = keyValue.Value.GetProtobufType(); obj.Type = (int)type; var stream = new MemoryStream(); using (var outputStream = new CodedOutputStream(stream)) { keyValue.Value.WriteWithTypeTo(type, outputStream); outputStream.Flush(); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); obj.Content = ByteString.CopyFrom(stream.ToArray()); } mapField.Add(keyValue.Key, obj); }); return(mapField); }