-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayRecord.scala
548 lines (495 loc) · 17.4 KB
/
ArrayRecord.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* Copyright 2023 record4s authors
*
* 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 com.github.tarao.record4s
import scala.deriving.Mirror
import scala.language.dynamics
import typing.ArrayRecord.{Aux, Concat, Lookup}
import typing.Record.{Select, Unselect}
import util.SeqOps.deduped
/** Base record class compatible with Product. */
abstract class ProductRecord extends Record with Product {
private[record4s] def __fields: IndexedSeq[(String, Any)]
override def productPrefix: String = "ProductRecord"
override def productArity: Int = __fields.size
override def productElement(n: Int): Any =
if (n >= 0 && n < productArity)
__fields(n)._2
else
throw new IndexOutOfBoundsException(
s"$n is out of bounds (min 0, max ${productArity - 1})",
)
override def productElementName(n: Int): String =
if (n >= 0 && n < productArity)
__fields(n)._1
else
throw new IndexOutOfBoundsException(
s"$n is out of bounds (min 0, max ${productArity - 1})",
)
/** Stringify the record.
*
* The order of key-value pairs is exactly the same as that of the static
* field types.
*/
override def toString(): String =
__fields
.iterator
.map { case (k, v) => s"$k = $v" }
.mkString(s"${productPrefix}(", ", ", ")")
override def canEqual(that: Any): Boolean =
that.isInstanceOf[ProductRecord]
override def equals(other: Any): Boolean =
other match {
case other: ProductRecord =>
__fields == other.__fields
case _ =>
false
}
}
object ProductRecord {
import ArrayRecord.newArrayRecord
class MirrorOfProductRecord[
ElemLabels <: Tuple,
ElemTypes <: Tuple,
P <: ProductRecord,
](elemLabels: Seq[String])
extends Mirror.Product {
type MirroredMonoType = P
type MirroredType = P
type MirroredLabel = "ProductRecord"
type MirroredElemTypes = ElemTypes
type MirroredElemLabels = ElemLabels
def fromProduct(p: Product): P =
newArrayRecord[P](elemLabels.zip(p.productIterator).toVector)
}
inline given [R](using
r: RecordLike[ArrayRecord[R]],
): MirrorOfProductRecord[r.ElemLabels, r.ElemTypes, ArrayRecord[R]] =
new MirrorOfProductRecord(RecordLike.seqOfLabels[r.ElemLabels])
given [Elems, R, P <: Product](using
from: Mirror.ProductOf[ArrayRecord[R]],
to: Mirror.ProductOf[P],
ev: from.MirroredElemTypes <:< to.MirroredElemTypes,
): Converter[ArrayRecord[R], P] =
new Converter {
def apply(record: ArrayRecord[R]): P = to.fromProduct(record)
}
}
/** Record class with constant-time field access.
*
* It provides extensible records in a different way than `%`. There are
* following differences compared to `%`.
*
* - Field access is faster (constant time)
* - Creation is faster
* - Update time grows significantly when record size grows
* - Compilation is a bit slower
* - No implicit upcast
*
* Example
* ```
* val r = ArrayRecord(name = "tarao", age = 3)
* // val r: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* r.name
* // val res0: String = tarao
* r.age
* // val res1: Int = 3
* ```
*/
abstract class ArrayRecord[R] extends ProductRecord with Dynamic {
override def productPrefix: String = "ArrayRecord"
transparent inline def selectDynamic[L <: String & Singleton](name: L) =
${ ArrayRecordMacros.selectImpl('this, 'name) }
}
object ArrayRecord
extends ArrayRecord.Extensible[EmptyTuple]
with ArrayRecordPlatformSpecific {
import scala.compiletime.{constValue, erasedValue, summonInline}
import typing.withPotentialTypingError
protected def record: ArrayRecord[EmptyTuple] = empty
/** An empty record */
val empty: ArrayRecord[EmptyTuple] =
newArrayRecord[ArrayRecord[EmptyTuple]](Vector.empty)
/** Get the field value of specified label.
*
* It is essentially the same as `record.{label}` but it can access to fields
* hidden by own methods of `class ArrayRecord`.
*
* @example
* ```
* val r = ArrayRecord(value = 3, toString = 10)
*
* r.value
* // val res0: Int = 3
* r.toString
* // val res1: String = ArrayRecord(value = 3, toString = 10)
*
* ArrayRecord.lookup(r, "value")
* // val res2: Int = 3
* ArrayRecord.lookup(r, "toString")
* // val res3: Int = 10
* ```
*
* @param record
* a record
* @param label
* a string literal field name
* @return
* the value of the field named by `label`
*/
inline def lookup[R, L <: String & Singleton, Index <: Int, Out](
record: ArrayRecord[R],
label: L,
)(using Lookup.Aux[R, L, Index, Out]): Out =
record.__fields(constValue[Index])._2.asInstanceOf[Out]
/** Construct a record from something else.
*
* @example
* ```
* case class Person(name: String, age: Int)
* val p = Person("tarao", 3)
* val r = ArrayRecord.from(p)
* // val r: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* ```
*
* @tparam T
* some type given `RecordLike[T]`
* @param x
* something that is record like
* @return
* a record
*/
inline def from[T: RecordLike, RR <: ProductRecord](x: T)(using
Aux[T, RR],
): RR = withPotentialTypingError {
empty ++ x
}
extension [R](record: ArrayRecord[R]) {
/** Extend the record by fields.
*
* If a new field has the same name as the existing field, then the new
* field overrides the old one.
*
* @example
* ```
* val r = ArrayRecord(name = "tarao") + (age = 3, email = "tarao@example.com")
* // val r: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int), ("email", String))] = ArrayRecord(name = tarao, age = 3, email = tarao@example.com)
* ```
*
* @return
* an object to define new fields
*/
def updated: Extensible[R] = new Extensible.Appender(record)
/** Alias for `updated` */
inline def + : Extensible[R] = updated
/** Concatenate this record and another record.
*
* If the both record has a field of the same name, then it takes the field
* from the latter record.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3)
* val r2 = ArrayRecord(age = 4, email = "tarao@example.com")
* val r3 = r1 ++ r2
* // val r3: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int), ("email", String))] = ArrayRecord(name = tarao, age = 4, email = tarao@example.com)
* ```
*
* @tparam R2
* a record type (given `RecordLike[R2]`)
* @param other
* a record to concatenate
* @return
* a new record which has the both fields from this record and `other`
*/
inline def concat[R2: RecordLike, RR <: ProductRecord](
other: R2,
)(using c: Concat[R, R2]): c.Out =
withPotentialTypingError {
summon[typing.Concrete[R2]]
val vec = record
.__fields
.toVector
val otherIt = summon[RecordLike[R2]].orderedIterableOf(other)
inline erasedValue[c.NeedDedup] match {
case _: false =>
newArrayRecord[c.Out](vec.concat(otherIt))
case _ =>
newArrayRecord[c.Out](unsafeConcat(vec, otherIt))
}
}
/** Alias for `concat` */
inline def ++[R2: RecordLike, RR <: ProductRecord](
other: R2,
)(using c: Concat[R, R2]): c.Out = concat(other)
/** Give a type tag to this record.
*
* @example
* ```
* trait Person; object Person {
* extension [T <: Tuple](p: ArrayRecord[(("name", String) *: T) & Tag[Person]]) {
* def firstName: String = p.name.split(" ").head
* }
* }
*
* val r = ArrayRecord(name = "tarao fuguta", age = 3).tag[Person]
* r.firstName
* // val res0: String = tarao
* ```
*
* @tparam T
* an arbitrary type used as a tag
* @return
* the same record with a tag type
*/
def tag[T]: ArrayRecord[R & Tag[T]] =
record.asInstanceOf[ArrayRecord[R & Tag[T]]]
/** Return values of this record as a `Tuple`.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3)
* r1.values
* // val res0: (String, Int) = (tarao,3)
* ```
*
* @return
* values of the record as a tuple
*/
def values(using
r: RecordLike[ArrayRecord[R]],
conv: Converter[ArrayRecord[R], r.ElemTypes],
): r.ElemTypes = record.to[r.ElemTypes]
/** Upcast the record to specified type.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3, email = "tarao@example.com")
* // val r1: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int), ("email", String))] = ArrayRecord(name = tarao, age = 3, email = tarao@example.com)
* val r2 = r1.upcast[(("name", String), ("age", Int))]
* // val r2: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* ```
*
* @tparam R2
* target type
* @return
* a record containing only fields in the target type
*/
transparent inline def upcast[R2] =
${ ArrayRecordMacros.upcastImpl[R, R2]('record) }
private[record4s] inline def shrinkTo[R2](using
r: RecordLike[ArrayRecord[R2]],
): ArrayRecord[R2] =
newArrayRecord[ArrayRecord[R2]](
// This is quite hacky: `forceOrderedIterableOf` makes a Map from
// `record.__fields` and collect values according to `R2`. It is unsafe to do
// `record.asInstanceOf[ArrayRecord[R2]]` in most cases but it's OK here because
// `forceOrderedIterableOf` doesn't depend on the static type of the argument.
r.forceOrderedIterableOf(record.asInstanceOf[ArrayRecord[R2]]).toVector,
)
/** Convert this record to a `Tuple`.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3)
* r1.toTuple
* // val res0: (("name", String), ("age", Int)) = ((name,tarao),(age,3))
* ```
*
* @return
* fields of label-value pairs as a tuple
*/
inline def toTuple(using
r: RecordLike[ArrayRecord[R]],
conv: Converter[ArrayRecord[R], r.ElemTypes],
): Tuple.Zip[r.ElemLabels, r.ElemTypes] =
r.elemLabels
.zip(record.productIterator)
.foldRight(EmptyTuple: Tuple) { (zipped, tuple) =>
zipped *: tuple
}
.asInstanceOf[Tuple.Zip[r.ElemLabels, r.ElemTypes]]
/** Convert to a record of type `%`.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3)
* // val r1: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* r1.toRecord
* // val res0: com.github.tarao.record4s.%{val name: String; val age: Int} = %(name = tarao, age = 3)
* ```
*
* @return
* a record of type `%`
*/
inline def toRecord[RR <: %](using
typing.Record.Aux[ArrayRecord[R], RR],
): RR =
withPotentialTypingError {
Record.from(record)
}
}
// - Putting `apply` in the extension breaks `ArrayRecord.applyDynamicNamed`.
// - Defining `to` in the extension breaks JMH.
implicit class OpsCompat[R](private val record: ArrayRecord[R])
extends AnyVal {
/** Create a new record by selecting some fields of an existing record.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3, email = "tarao@example.com")
* val r2 = r1(select.name.age)
* // val r2: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* val r3 = r1(select.name(rename = "nickname").age)
* // val r3: com.github.tarao.record4s.ArrayRecord[(("nickname", String), ("age", Int))] = ArrayRecord(nickname = tarao, age = 3)
* ```
*
* @tparam S
* list of selected field as a Tuple
* @tparam RR
* type of the new record
* @param s
* selection of fields created by `select`
* @return
* a new record with the selected fields
*/
inline def apply[S <: Tuple, RR <: %](s: Selector[S])(using
r: RecordLike[ArrayRecord[R]],
ev: Select.Aux[R, S, RR],
rr: RecordLike[RR],
): ArrayRecord[rr.TupledFieldTypes] = withPotentialTypingError {
val sel = selection[S]
val m = r.iterableOf(record).toMap
newArrayRecord[ArrayRecord[rr.TupledFieldTypes]](
sel
.map((label, newLabel) => (newLabel, m(label)))
.deduped
.iterator
.toVector,
)
}
private inline def selection[S <: Tuple]: Seq[(String, String)] = {
inline erasedValue[S] match {
case _: ((label, newLabel) *: tail) =>
val st1 = summonInline[label <:< String]
val st2 = summonInline[newLabel <:< String]
(st1(valueOf[label]), st2(valueOf[newLabel])) +: selection[tail]
case _: (label *: tail) =>
val st = summonInline[label <:< String]
val labelStr = st(valueOf[label])
(labelStr, labelStr) +: selection[tail]
case _: EmptyTuple =>
Seq.empty
}
}
/** Create a new record by unselecting some fields of an existing record.
*
* @example
* ```
* val r1 = ArrayRecord(name = "tarao", age = 3, email = "tarao@example.com")
* val r2 = r1(unselect.email)
* // val r2: com.github.tarao.record4s.ArrayRecord[(("name", String), ("age", Int))] = ArrayRecord(name = tarao, age = 3)
* ```
*
* @tparam U
* list of unselected field as a Tuple
* @tparam RR
* type of the new record
* @param u
* unselection of fields created by `unselect`
* @return
* a new record without the unselected fields
*/
inline def apply[U <: Tuple, R2 <: %, RR <: %](u: Unselector[U])(using
r: typing.Record.Aux[ArrayRecord[R], R2],
ev: Unselect.Aux[R2, U, RR],
rr: RecordLike[RR],
): ArrayRecord[Tuple.Zip[rr.ElemLabels, rr.ElemTypes]] =
withPotentialTypingError {
withPotentialTypingError {
record.shrinkTo[Tuple.Zip[rr.ElemLabels, rr.ElemTypes]]
}(using ev)
}(using r)
/** Convert this record to a `To`.
*
* @example
* ```
* case class Person(name: String, age: Int)
* val r = ArrayRecord(name = "tarao", age = 3)
* r.to[Person]
* // val res0: Person = Person(tarao,3)
* ```
*
* @tparam To
* a type to which the record is converted
* @return
* a new product instance
*/
def to[To](using conv: Converter[ArrayRecord[R], To]): To = conv(record)
}
given canEqualReflexive[R]: CanEqual[ArrayRecord[R], ArrayRecord[R]] =
CanEqual.derived
final class RecordLikeArrayRecord[R] extends RecordLike[ArrayRecord[R]] {
def iterableOf(r: ArrayRecord[R]): Iterable[(String, Any)] = r.__fields
}
transparent inline given recordLike[R]: RecordLike[ArrayRecord[R]] =
${ ArrayRecordMacros.derivedRecordLikeImpl }
private[record4s] def newArrayRecord[R](
fields: IndexedSeq[(String, Any)],
): R =
new VectorRecord(fields.toVector).asInstanceOf[R]
// the same as `(record ++ tidiedIterable).deduped` but use `Vector.updated` for
// optimization
private[record4s] def unsafeConcat(
record: Vector[(String, Any)],
tidiedIterable: Iterable[(String, Any)],
): Vector[(String, Any)] = {
val m = tidiedIterable.toMap
val updates = record.zipWithIndex.filter { case ((key, _), _) =>
m.contains(key)
}
val dups = updates.map(_._1._1).toSet
updates
.foldLeft(record) { case (vec, ((key, _), index)) =>
vec.updated(index, (key, m(key)))
}
.concat(tidiedIterable.filterNot(f => dups.contains(f._1)))
}
trait Extensible[R] extends Any with Dynamic {
protected def record: ArrayRecord[R]
transparent inline def applyDynamic(method: String)(
inline fields: Any*,
) =
${ ArrayRecordMacros.applyImpl('record, 'method, 'fields) }
transparent inline def applyDynamicNamed(method: String)(
inline fields: (String, Any)*,
) =
${ ArrayRecordMacros.applyImpl('record, 'method, 'fields) }
}
object Extensible {
class Appender[R](override protected val record: ArrayRecord[R])
extends AnyVal
with Extensible[R]
}
}
/** A concrete record class.
*
* This class is exposed due to inlining but not intended to be used directly.
*/
final class VectorRecord private[record4s] (fields: IndexedSeq[(String, Any)])
extends ArrayRecord[EmptyTuple] {
override private[record4s] val __fields: Vector[(String, Any)] =
fields.toVector
}