-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,303 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/library-base/src/commonMain/kotlin/io/realm/kotlin/annotations/DynamicAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2023 Realm Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package io.realm.kotlin.annotations | ||
|
||
@MustBeDocumented | ||
@Target( | ||
AnnotationTarget.FUNCTION, | ||
) | ||
@RequiresOptIn(level = RequiresOptIn.Level.ERROR) | ||
public annotation class DynamicAPI |
50 changes: 50 additions & 0 deletions
50
...brary-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/getgeneric/BaseRealmObjectExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2024 Realm Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.realm.kotlin.dynamic.getgeneric | ||
|
||
import io.realm.kotlin.annotations.DynamicAPI | ||
import io.realm.kotlin.internal.RealmObjectHelper | ||
import io.realm.kotlin.internal.runIfManagedOrThrow | ||
import io.realm.kotlin.types.BaseRealmObject | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.typeOf | ||
|
||
// Proposal 1 of generic/dynamic API | ||
// This seems like the most promising | ||
@DynamicAPI | ||
public inline operator fun <reified T> BaseRealmObject.get(propertyName: String): T { | ||
return get(propertyName, typeOf<T>()) | ||
} | ||
|
||
@DynamicAPI | ||
public operator fun <T> BaseRealmObject.set(name: String, value: T) { | ||
return this.runIfManagedOrThrow { | ||
RealmObjectHelper.setValueByName(this, name, value) | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal fun <T> BaseRealmObject.get(propertyName: String, type: KType): T { | ||
return this.runIfManagedOrThrow { | ||
RealmObjectHelper.dynamicGetFromKType( | ||
obj = this, | ||
propertyName = propertyName, | ||
type = type | ||
) | ||
} | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
...ary-base/src/commonMain/kotlin/io/realm/kotlin/dynamic/getinterface/BaseRealmObjectExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2024 Realm Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.realm.kotlin.dynamic.getinterface | ||
|
||
import io.realm.kotlin.internal.RealmObjectHelper | ||
import io.realm.kotlin.internal.runIfManagedOrThrow | ||
import io.realm.kotlin.types.BaseRealmObject | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.typeOf | ||
|
||
// Proposal 2 of generic/dynamic API | ||
// Hiding things a bit by only exposing it on a specific type. This is a bit cumbersome as you | ||
// cannot use a single entry point to get into the dynamic domain (requires to use .relaxed | ||
// everywhere) | ||
public interface RelaxedRealmObject | ||
|
||
// FIXME Naming | ||
// - Extras? | ||
public val BaseRealmObject.relaxed: RelaxedRealmObject | ||
get() = this as RelaxedRealmObject | ||
|
||
|
||
public inline operator fun <reified T> RelaxedRealmObject.get(propertyName: String): T { | ||
return get(propertyName, typeOf<T>()) | ||
} | ||
|
||
public operator fun <T> RelaxedRealmObject.set(name: String, value: T) { | ||
return (this as BaseRealmObject).runIfManagedOrThrow { | ||
RealmObjectHelper.setValueByName(this, name, value) | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal fun <T> RelaxedRealmObject.get(propertyName: String, type: KType): T { | ||
return (this as BaseRealmObject).runIfManagedOrThrow { | ||
RealmObjectHelper.dynamicGetFromKType( | ||
obj = this, | ||
propertyName = propertyName, | ||
type = type | ||
) | ||
} | ||
} |
Oops, something went wrong.