|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.query |
| 17 | + |
| 18 | +import kotlin.reflect.KProperty |
| 19 | +import kotlin.reflect.KProperty1 |
| 20 | + |
| 21 | +/** |
| 22 | + * Refer to a field in an embedded/nested document. |
| 23 | + * @author Tjeu Kayim |
| 24 | + * @since 2.2 |
| 25 | + */ |
| 26 | +class NestedProperty<T, U>( |
| 27 | + internal val parent: KProperty<U>, |
| 28 | + internal val child: KProperty1<U, T> |
| 29 | +) : KProperty<T> by child |
| 30 | + |
| 31 | +/** |
| 32 | + * Recursively construct field name for a nested property. |
| 33 | + * @author Tjeu Kayim |
| 34 | + */ |
| 35 | +internal fun nestedFieldName(property: KProperty<*>): String { |
| 36 | + return when (property) { |
| 37 | + is NestedProperty<*, *> -> |
| 38 | + "${nestedFieldName(property.parent)}.${property.child.name}" |
| 39 | + else -> property.name |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Builds [NestedProperty] from Property References. |
| 45 | + * Refer to a field in an embedded/nested document. |
| 46 | + * |
| 47 | + * For example, referring to the field "book.author": |
| 48 | + * ``` |
| 49 | + * Book::author / Author::name isEqualTo "Herman Melville" |
| 50 | + * ``` |
| 51 | + * @author Tjeu Kayim |
| 52 | + * @since 2.2 |
| 53 | + */ |
| 54 | +operator fun <T, U> KProperty<T>.div(other: KProperty1<T, U>) = |
| 55 | + NestedProperty(this, other) |
0 commit comments