-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,12 @@ internal DynamicObjectApi(ManagedAccessor managedAccessor) | |
/// object, casting to <see cref="IRealmObjectBase"/> is always valid. | ||
/// </remarks> | ||
public T Get<T>(string propertyName) | ||
{ | ||
return Get(propertyName).As<T>(); | ||
} | ||
|
||
//TODO Add docs | ||
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Verify TODOsRealm/Realm/Dynamic/DynamicObjectApi.cs#L60
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Source Generation
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Code Coverage
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Analyze C#
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Analyze C#
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Package NuGet
Check warning on line 60 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Package NuGet
|
||
public RealmValue Get(string propertyName) | ||
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Source Generation
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Test Code Coverage
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Analyze C#
Check warning on line 61 in Realm/Realm/Dynamic/DynamicObjectApi.cs GitHub Actions / Package NuGet
|
||
{ | ||
if (GetProperty(propertyName) is Property property) | ||
{ | ||
|
@@ -76,11 +82,11 @@ public T Get<T>(string propertyName) | |
$"{_managedAccessor.ObjectSchema.Name}.{propertyName} is {property.GetDotnetTypeName()} and can't be accessed using {nameof(Dynamic)}.{nameof(Get)}. Use {collectionMethodName} instead."); | ||
} | ||
|
||
return _managedAccessor.GetValue(propertyName).As<T>(); | ||
return _managedAccessor.GetValue(propertyName); | ||
} | ||
else | ||
{ | ||
return _managedAccessor.GetValue(propertyName).As<T>(); | ||
return _managedAccessor.GetValue(propertyName); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,9 @@ private static class NativeMethods | |
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "object_set_collection_value", CallingConvention = CallingConvention.Cdecl)] | ||
public static extern IntPtr set_collection_value(ObjectHandle handle, IntPtr propertyIndex, RealmValueType type, out NativeException ex); | ||
|
||
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "object_set_collection_additional_property", CallingConvention = CallingConvention.Cdecl)] | ||
public static extern IntPtr set_collection_additional_property(ObjectHandle handle, StringValue propertyName, RealmValueType type, out NativeException ex); | ||
|
||
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "object_create_embedded", CallingConvention = CallingConvention.Cdecl)] | ||
public static extern IntPtr create_embedded_link(ObjectHandle handle, IntPtr propertyIndex, out NativeException ex); | ||
|
||
|
@@ -269,17 +272,50 @@ public void SetValue(string propertyName, Metadata metadata, in RealmValue value | |
} | ||
else | ||
{ | ||
//TODO Eventually merge with the previous case | ||
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Verify TODOsRealm/Realm/Handles/ObjectHandle.cs#L275
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Source Generation
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Code Coverage
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Analyze C#
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Analyze C#
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Package NuGet
Check warning on line 275 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Package NuGet
|
||
|
||
//TODO Probably this can be improved | ||
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Verify TODOsRealm/Realm/Handles/ObjectHandle.cs#L277
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (ubuntu-latest, linux-x64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (macos-14, osx-arm64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Source Generation
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Weaver (windows-latest, win-x64)
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Test Code Coverage
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Analyze C#
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Analyze C#
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Package NuGet
Check warning on line 277 in Realm/Realm/Handles/ObjectHandle.cs GitHub Actions / Package NuGet
|
||
using Arena arena = new(); | ||
var propertyNameNative = StringValue.AllocateFrom(propertyName, arena); | ||
|
||
if (value.Type == RealmValueType.Object) | ||
{ | ||
switch (value.AsIRealmObject()) | ||
{ | ||
case IRealmObject realmObj when !realmObj.IsManaged: | ||
realm.Add(realmObj); | ||
break; | ||
case IEmbeddedObject: | ||
throw new NotSupportedException($"A RealmValue cannot contain an embedded object. Attempted to set {value} to {metadata.Schema.Name}.{propertyName}"); | ||
case IAsymmetricObject: | ||
throw new NotSupportedException($"Asymmetric objects cannot be linked to and cannot be contained in a RealmValue. Attempted to set {value} to {metadata.Schema.Name}.{propertyName}"); | ||
} | ||
} | ||
else if (value.Type.IsCollection()) | ||
{ | ||
var collectionPtr = NativeMethods.set_collection_additional_property(this, propertyNameNative, value.Type, out var collNativeException); | ||
collNativeException.ThrowIfNecessary(); | ||
|
||
switch (value.Type) | ||
{ | ||
case RealmValueType.List: | ||
CollectionHelpers.PopulateCollection(realm, new ListHandle(Root!, collectionPtr), value); | ||
break; | ||
case RealmValueType.Dictionary: | ||
CollectionHelpers.PopulateCollection(realm, new DictionaryHandle(Root!, collectionPtr), value); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return; | ||
} | ||
|
||
var (primitive, handles) = value.ToNative(); | ||
NativeMethods.set_additional_property(this, propertyNameNative, primitive, out var nativeException); | ||
handles?.Dispose(); | ||
nativeException.ThrowIfNecessary(); | ||
} | ||
|
||
|
||
} | ||
|
||
public long AddInt64(IntPtr propertyIndex, long value) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
//////////////////////////////////////////////////////////////////////////// | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.CSharp.RuntimeBinder; | ||
|
@@ -983,21 +984,41 @@ public void GetDictionary_WhenCastToWrongValue_Throws() | |
[Test] | ||
public void FlexibleSchema_BaseTest() | ||
{ | ||
var testObj = _realm.Write(() => | ||
var person = _realm.Write(() => | ||
{ | ||
return _realm.Add(new IntPropertyObject()); | ||
return _realm.Add(new Person()); | ||
}); | ||
|
||
var testObj = new Person { FirstName = "Luigi" }; | ||
var testList = new List<RealmValue> { 1, "test", true }; | ||
|
||
_realm.Write(() => | ||
{ | ||
testObj.DynamicApi.Set("prop1", "testval"); | ||
testObj.DynamicApi.Set("prop2", 10); | ||
person.DynamicApi.Set("propString", "testval"); | ||
person.DynamicApi.Set("propInt", 10); | ||
person.DynamicApi.Set("propObj", testObj); | ||
person.DynamicApi.Set("propList", testList); | ||
|
||
}); | ||
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Code Coverage
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, linux-x64
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, osx-x64
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Woven Classes
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, win-x64
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test .NET Framework
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.macOS
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test UWP
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.tvOS
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.iOS
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.Android
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.Android
Check warning on line 1002 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net8.0, osx-arm64
|
||
|
||
Assert.That(testObj.DynamicApi.Get<string>("prop1"), Is.EqualTo("testval")); | ||
Assert.That(testObj.DynamicApi.Get<int>("prop2"), Is.EqualTo(10)); | ||
Assert.That(person.DynamicApi.Get<string>("propString"), Is.EqualTo("testval")); | ||
Assert.That(person.DynamicApi.Get<int>("propInt"), Is.EqualTo(10)); | ||
Assert.That(person.DynamicApi.Get<Person>("propObj"), Is.EqualTo(testObj)); | ||
Assert.That(person.DynamicApi.Get<IList<RealmValue>>("propList"), Is.EqualTo(testList)); | ||
} | ||
|
||
/** To test | ||
* - add and retrieve objects | ||
* - add and retrieve collections | ||
* - set on same property changes | ||
* - set null value | ||
* - retrieve property that does not exist raises exception | ||
* - retrieve additional properties | ||
* - erase a property | ||
* | ||
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Code Coverage
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, linux-x64
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, osx-x64
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Woven Classes
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, win-x64
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test .NET Framework
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.macOS
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test UWP
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.tvOS
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.iOS
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.Android
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.Android
Check warning on line 1018 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net8.0, osx-arm64
|
||
* | ||
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Code Coverage
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, linux-x64
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, osx-x64
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Woven Classes
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, win-x64
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test .NET Framework
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.macOS
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test UWP
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.tvOS
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.iOS
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.Android
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.Android
Check warning on line 1019 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net8.0, osx-arm64
|
||
*/ | ||
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Code Coverage
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, linux-x64
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, osx-x64
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Woven Classes
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net6.0, win-x64
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test .NET Framework
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.macOS
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test UWP
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.tvOS
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.iOS
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.MacCatalyst
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Xamarin.Android
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.iOS
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test Maui.Android
Check warning on line 1020 in Tests/Realm.Tests/Database/DynamicAccessTests.cs GitHub Actions / Test net8.0, osx-arm64
|
||
|
||
#endregion | ||
|
||
[Test] | ||
|