@@ -120,7 +120,7 @@ extension DatastoreDescriptor {
120120 )
121121
122122 /// If the type is identifiable, skip the `id` index as we always make one based on `id`
123- if indexDescriptor. key == " $id " && sampleInstance is any Identifiable {
123+ if ( indexDescriptor. key == " $id " || indexDescriptor . key == " id " ) && sampleInstance is any Identifiable {
124124 continue
125125 }
126126
@@ -135,11 +135,17 @@ extension DatastoreDescriptor {
135135 guard let label = child. label else { continue }
136136 guard let childValue = child. value as? any _IndexedProtocol else { continue }
137137
138+ let proposedKey = childValue. projectedValue. key
139+
138140 let actualKey : String
139- if label. prefix ( 1 ) == " _ " {
140- actualKey = " $ \( label. dropFirst ( ) ) "
141+ if proposedKey. isEmpty {
142+ if label. prefix ( 1 ) == " _ " {
143+ actualKey = " $ \( label. dropFirst ( ) ) "
144+ } else {
145+ actualKey = label
146+ }
141147 } else {
142- actualKey = label
148+ actualKey = proposedKey
143149 }
144150
145151 let indexDescriptor = IndexDescriptor (
@@ -149,7 +155,7 @@ extension DatastoreDescriptor {
149155 )
150156
151157 /// If the type is identifiable, skip the `id` index as we always make one based on `id`
152- if indexDescriptor. key == " $id " && sampleInstance is any Identifiable {
158+ if ( indexDescriptor. key == " $id " || indexDescriptor . key == " id " ) && sampleInstance is any Identifiable {
153159 continue
154160 }
155161
@@ -180,11 +186,18 @@ extension DatastoreDescriptor.IndexDescriptor {
180186 sampleInstance: CodedType ,
181187 keypath: KeyPath < CodedType , _AnyIndexed >
182188 ) {
183- let indexType = sampleInstance [ keyPath: keypath] . indexedType
189+ let sampleIndexValue = sampleInstance [ keyPath: keypath]
190+ let indexType = sampleIndexValue. indexedType
191+ let indexKey = sampleIndexValue. key
184192
185- let fullKeyPath = String ( describing: keypath)
186- let rootComponent = " \\ \( String ( describing: CodedType . self) ) . "
187- let path = String ( fullKeyPath. dropFirst ( rootComponent. count) )
193+ let path : String
194+ if indexKey. isEmpty {
195+ path = keypath. keyName
196+ } else {
197+ var components = keypath. keyName. components ( separatedBy: " . " )
198+ components [ components. count- 1 ] = indexKey
199+ path = components. joined ( separator: " . " )
200+ }
188201
189202 self . init (
190203 version: version,
@@ -193,3 +206,11 @@ extension DatastoreDescriptor.IndexDescriptor {
193206 )
194207 }
195208}
209+
210+ extension PartialKeyPath {
211+ fileprivate var keyName : String {
212+ let fullKeyPath = String ( describing: self )
213+ let rootComponent = " \\ \( String ( describing: Root . self) ) . "
214+ return String ( fullKeyPath. dropFirst ( rootComponent. count) )
215+ }
216+ }
0 commit comments