-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework of the concept of collection in the Schema abstraction #290
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fe1d613
Schema refactor in core
daddykotex 52daa2d
Fix errors throughout in other modules
daddykotex c9e2c03
Merge remote-tracking branch 'origin/main' into dfrancoeur/collections
daddykotex 6b6ecb8
Improve CollectionTag
daddykotex 465829f
Add Vector and ArraySeq implementation
daddykotex 147f728
Remove ArraySeq for now
daddykotex 9a4a772
Use tag.name in string representation
daddykotex df05035
Rework implementation for jcodec collections
daddykotex ec50c65
Add IndexedSeq to CollectionTag
Baccata eefd49c
pre-compute builder-making function
Baccata c6f454e
Fix Scala 3
Baccata 26aa019
Fix cast
Baccata b364081
Regenerated examples
Baccata e2d5e2e
Make Newtype.Make package private
Baccata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2021-2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* 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 smithy4s | ||
package schema | ||
|
||
import scala.reflect.ClassTag | ||
|
||
sealed trait CollectionTag[C[_]] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
def name: String | ||
|
||
def iterator[A](c: C[A]): Iterator[A] | ||
def build[A](put: (A => Unit) => Unit): C[A] | ||
|
||
def fromIterator[A](it: Iterator[A]): C[A] = build(put => it.foreach(put(_))) | ||
def empty[A]: C[A] = build(_ => ()) | ||
} | ||
|
||
object CollectionTag { | ||
import scala.collection.compat.{immutable => cols} | ||
import scala.collection.{mutable => mut} | ||
|
||
case object ListTag extends CollectionTag[List] { | ||
override def name: String = "List" | ||
|
||
override def iterator[A](c: List[A]): Iterator[A] = c.iterator | ||
|
||
override def build[A](put: (A => Unit) => Unit): List[A] = { | ||
val builder = mut.ListBuffer.newBuilder[A] | ||
put(builder += (_)) | ||
builder.result().toList | ||
} | ||
|
||
} | ||
|
||
case object SetTag extends CollectionTag[Set] { | ||
override def name: String = "Set" | ||
override def iterator[A](c: Set[A]): Iterator[A] = c.iterator | ||
|
||
override def build[A](put: (A => Unit) => Unit): Set[A] = { | ||
val builder = Set.newBuilder[A] | ||
put(builder.+=(_)) | ||
builder.result() | ||
} | ||
} | ||
|
||
case object VectorTag extends CollectionTag[Vector] { | ||
override def name: String = "Vector" | ||
override def iterator[A](c: Vector[A]): Iterator[A] = c.iterator | ||
|
||
override def build[A](put: (A => Unit) => Unit): Vector[A] = { | ||
val builder = Vector.newBuilder[A] | ||
put(builder.+=(_)) | ||
builder.result() | ||
} | ||
} | ||
|
||
case object IndexedSeqTag extends CollectionTag[IndexedSeq] { | ||
|
||
override def name: String = "IndexedSeq" | ||
override def iterator[A](c: IndexedSeq[A]): Iterator[A] = c.iterator | ||
|
||
override def build[A](put: (A => Unit) => Unit): Vector[A] = { | ||
val builder = Vector.newBuilder[A] | ||
put(builder.+=(_)) | ||
builder.result() | ||
} | ||
|
||
/** | ||
* Returns a builder that may be able to store the elements in an unboxed | ||
* fashion | ||
*/ | ||
private[smithy4s] def compactBuilder[A]( | ||
schema: Schema[A] | ||
): ((A => Unit) => Unit) => IndexedSeq[A] = | ||
schema.compile(CTSchemaVisitor) match { | ||
case Some(ct) => { (put: (A => Unit) => Unit) => | ||
val builder = cols.ArraySeq.newBuilder(ct) | ||
put(builder.+=(_)) | ||
builder.result() | ||
} | ||
case None => { (put: (A => Unit) => Unit) => | ||
val builder = IndexedSeq.newBuilder[A] | ||
put(builder.+=(_)) | ||
builder.result() | ||
} | ||
} | ||
} | ||
|
||
private[this] type MaybeCT[A] = Option[ClassTag[A]] | ||
|
||
/** | ||
* Retrieves a ClassTag whenever possible. | ||
*/ | ||
// format: off | ||
private[this] object CTSchemaVisitor extends SchemaVisitor[MaybeCT]{ | ||
val primitiveCT = Primitive.deriving[ClassTag] | ||
def primitive[P](shapeId: ShapeId, hints: Hints, tag: Primitive[P]): MaybeCT[P] = Some(primitiveCT(tag)) | ||
def collection[C[_], A](shapeId: ShapeId, hints: Hints, tag: CollectionTag[C], member: Schema[A]): MaybeCT[C[A]] = tag match { | ||
case ListTag => Some(implicitly[ClassTag[List[A]]]) | ||
case SetTag => Some(implicitly[ClassTag[Set[A]]]) | ||
case VectorTag => Some(implicitly[ClassTag[Vector[A]]]) | ||
case IndexedSeqTag => Some(implicitly[ClassTag[IndexedSeq[A]]]) | ||
} | ||
def map[K, V](shapeId: ShapeId, hints: Hints, key: Schema[K], value: Schema[V]): MaybeCT[Map[K,V]] = Some(implicitly[ClassTag[Map[K, V]]]) | ||
def enumeration[E](shapeId: ShapeId, hints: Hints, values: List[EnumValue[E]], total: E => EnumValue[E]): MaybeCT[E] = None | ||
def struct[S](shapeId: ShapeId, hints: Hints, fields: Vector[SchemaField[S, _]], make: IndexedSeq[Any] => S): MaybeCT[S] = None | ||
def union[U](shapeId: ShapeId, hints: Hints, alternatives: Vector[SchemaAlt[U, _]], dispatch: U => Alt.SchemaAndValue[U, _]): MaybeCT[U] = None | ||
def biject[A, B](schema: Schema[A], to: A => B, from: B => A): MaybeCT[B] = { | ||
if (to.isInstanceOf[Newtype.Make[_, _]]) apply(schema).asInstanceOf[MaybeCT[B]] | ||
else None | ||
} | ||
def surject[A, B](schema: Schema[A], to: Refinement[A,B], from: B => A): MaybeCT[B] = None | ||
def lazily[A](suspend: Lazy[Schema[A]]): MaybeCT[A] = None | ||
} | ||
// format: off | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, is there ever a case where someone should use the apply method rather than make? If not, should the apply method be private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users should very much keep using the apply method to construct instances of newtypes. This is very much a kind of hack to ensure we have a way of detecting whether a bijection is actually coming from a newtype. I'll try to hide it further and make the
Make
construct package private.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e2d5e2e