static void TestWriteExt <T>() where T : IExtTest, IExtensible, new() { const float SOME_VALUE = 987.65F; T obj = new T(); var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility); Extensible.AppendValue <float>(tm, obj, 3, SOME_VALUE); byte[] raw = GetExtensionBytes(obj); Assert.AreEqual(5, raw.Length, "Extension Length"); Assert.AreEqual((3 << 3) | 5, raw[0], "Prefix (3 Fixed32)"); byte[] tmp = BitConverter.GetBytes(SOME_VALUE); if (!BitConverter.IsLittleEndian) { Array.Reverse(tmp); } Assert.AreEqual(tmp[0], raw[1], "Float32 Byte 0"); Assert.AreEqual(tmp[1], raw[2], "Float32 Byte 1"); Assert.AreEqual(tmp[2], raw[3], "Float32 Byte 2"); Assert.AreEqual(tmp[3], raw[4], "Float32 Byte 3"); float readBack = Extensible.GetValue <float>(obj, 3); Assert.AreEqual(SOME_VALUE, readBack, "read back"); BiggerObject big = tm.ChangeType <T, BiggerObject>(obj); Assert.AreEqual(SOME_VALUE, big.SomeFloat, "deserialize"); }
static void TestReadInvalidTag <T>() where T : IExtTest, IExtensible, new() { T obj = new T { Bof = "hi" }; string hi = Extensible.GetValue <string>(obj, 0); }
private void OnMulticastingReceived(string chat_channel, object data) { KeyValuePair <string, OnChannelMessage> p; lock (channel_lock_) { if (!channel_info_.TryGetValue(chat_channel, out p)) { DebugUtils.Log("You are not in the chat channel: {0}", chat_channel); return; } } if (encoding_ == FunEncoding.kJson) { DebugUtils.Assert(data is Dictionary <string, object>); Dictionary <string, object> mcast_msg = data as Dictionary <string, object>; p.Value(chat_channel, mcast_msg["sender"] as string, mcast_msg["text"] as string); } else { DebugUtils.Assert(data is FunMulticastMessage); FunMulticastMessage mcast_msg = data as FunMulticastMessage; FunChatMessage chat_msg = Extensible.GetValue <FunChatMessage> (mcast_msg, (int)MulticastMessageType.chat); p.Value(chat_channel, chat_msg.sender, chat_msg.text); } }
static void TestReadShouldUseProperty <T>() where T : IExtTest, IExtensible, new() { T obj = new T { Bof = "hi" }; string hi = Extensible.GetValue <string>(obj, 1); }
static void TestWriteExt <T>() where T : IExtTest, IExtensible, new() { const float SOME_VALUE = 987.65F; T obj = new T(); Extensible.AppendValue <float>(obj, 3, SOME_VALUE); byte[] raw = GetExtensionBytes(obj); Assert.Equal(5, raw.Length); //, "Extension Length"); Assert.Equal((3 << 3) | 5, raw[0]); //, "Prefix (3 Fixed32)"); byte[] tmp = BitConverter.GetBytes(SOME_VALUE); if (!BitConverter.IsLittleEndian) { Array.Reverse(tmp); } Assert.Equal(tmp[0], raw[1]); //, "Float32 Byte 0"); Assert.Equal(tmp[1], raw[2]); //, "Float32 Byte 1"); Assert.Equal(tmp[2], raw[3]); //, "Float32 Byte 2"); Assert.Equal(tmp[3], raw[4]); //, "Float32 Byte 3"); float readBack = Extensible.GetValue <float>(obj, 3); Assert.Equal(SOME_VALUE, readBack); //, "read back"); BiggerObject big = Serializer.ChangeType <T, BiggerObject>(obj); Assert.Equal(SOME_VALUE, big.SomeFloat); //, "deserialize"); }
static void TestReadShouldUseProperty <T>() where T : IExtTest, IExtensible, new() { T obj = new T { Bof = "hi" }; string hi = Extensible.GetValue <string>(obj, 1); Assert.Null(hi); // this is the current behaviour when reading against a tag that is a regular field, not an extension field }
public void OnDeserialized() { var productsListIsEmpty = Extensible.GetValue <bool>(this, 101); if (productsListIsEmpty) { this.Products = new List <string>(); } }
public void TestGroupAsExtension() { NoddyExtends ne = Program.Build <NoddyExtends>(0x1B, 0x08, 0x96, 0x01, 0x1C);// [start group 3] [test1] [end group 3] Assert.True(Program.CheckBytes(ne, 0x1B, 0x08, 0x96, 0x01, 0x1C), "Round trip"); Test1 t1 = Extensible.GetValue <Test1>(ne, 3); Assert.NotNull(t1); //, "Got an object?"); Assert.Equal(150, t1.A); //, "Value"); }
public void TestGroupAsExtension() { NoddyExtends ne = Program.Build <NoddyExtends>(0x1B, 0x08, 0x96, 0x01, 0x1C);// [start group 3] [test1] [end group 3] Assert.IsTrue(Program.CheckBytes(ne, 0x1B, 0x08, 0x96, 0x01, 0x1C), "Round trip"); var tm = TypeModel.Create(false, ProtoCompatibilitySettingsValue.FullCompatibility); tm.SkipCompiledVsNotCheck = true; Test1 t1 = Extensible.GetValue <Test1>(tm, ne, 3); Assert.IsNotNull(t1, "Got an object?"); Assert.AreEqual(150, t1.A, "Value"); }
public void RuntimeMessageContract() { var adhoc = new AdHoc(); Extensible.AppendValue <double>(adhoc, 1, 0.0); Extensible.AppendValue <int>(adhoc, 2, 1); Extensible.AppendValue <string>(adhoc, 3, "2"); Extensible.AppendValue <bool>(adhoc, 4, true); var clone = Serializer.DeepClone(adhoc); Assert.AreNotSame(clone, adhoc); Assert.AreEqual(0.0, Extensible.GetValue <double>(clone, 1)); Assert.AreEqual(1, Extensible.GetValue <int>(clone, 2)); Assert.AreEqual("2", Extensible.GetValue <string>(clone, 3)); Assert.AreEqual(true, Extensible.GetValue <bool>(clone, 4)); }
private void DumpOptionsFieldRecursive(FieldDescriptorProto field, IExtensible options, IDictionary <string, string> optionsKv, string path) { var key = string.IsNullOrEmpty(path) ? $"({field.name})" : $"{path}.{field.name}"; if (IsNamedType(field.type) && !string.IsNullOrEmpty(field.type_name)) { var fieldData = _protobufTypeMap[field.type_name].Source; switch (fieldData) { case EnumDescriptorProto enumProto: { if (Extensible.TryGetValue(options, field.number, out int idx)) { var value = enumProto.value.Find(x => x.number == idx); optionsKv.Add(key, value.name); } break; } case DescriptorProto messageProto: { var extension = Extensible.GetValue <ExtensionPlaceholder>(options, field.number); if (extension != null) { foreach (var subField in messageProto.field) { DumpOptionsFieldRecursive(subField, extension, optionsKv, key); } } break; } } } else { if (ExtractType(options, field, out var value)) { optionsKv.Add(key, value); } } }
public void TestMethod1() { FileDescriptorSet descriptor; using (var descriptorStream = File.OpenRead(GetFilePath("AddressBook.pb"))) { descriptor = Serializer.Deserialize <FileDescriptorSet>(descriptorStream); } var contactType = descriptor.Files[0].MessageTypes[0]; var addressBookType = descriptor.Files[0].MessageTypes[1]; using (var messageStream = File.OpenRead(GetFilePath("AddressBook.bin"))) { var model = Serializer.Deserialize <Base>(messageStream); var addr = Extensible.GetValue <AddressBook>(model, 1); // Assert.Equal("Luke Skywalker", output.Name); } }
private void GetReferences(FileStream stream, ItemContainer itemContainer) { if (stream == null) { throw new ArgumentNullException("stream"); } if (itemContainer == null) { throw new ArgumentNullException("itemContainer"); } var idx = 2; foreach (var info in _propertyManager.GetInfos(itemContainer.Item).Where(x => x.InfoType == PropertyManagerInfoType.Reference)) { var type = _domainMapper.Tag2Type(Extensible.GetValue <int>(itemContainer, idx++)); var guid = Extensible.GetValue <Guid>(itemContainer, idx++); info.SetValue(itemContainer.Item, GetItem(type, guid)); } }
static void TestReadNull <T>() where T : IExtTest, IExtensible, new() { string hi = Extensible.GetValue <string>(null, 1); }