-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ObjectStep using a Function1 (#2031)
* Implement ObjectStep using a Function1 * Add collection compat import for Scala 2.12 * Resolve unknown fields to `NullStep`
- Loading branch information
1 parent
1062138
commit dd29946
Showing
8 changed files
with
56 additions
and
62 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
45 changes: 17 additions & 28 deletions
45
core/src/main/scala/caliban/schema/ObjectFieldResolver.scala
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 |
---|---|---|
@@ -1,36 +1,25 @@ | ||
package caliban.schema | ||
|
||
import caliban.execution.Field | ||
import caliban.schema.Step.{ MetadataFunctionStep, ObjectStep } | ||
import caliban.schema.Step.{ NullStep, ObjectStep } | ||
|
||
import scala.collection.compat._ | ||
import scala.collection.mutable | ||
|
||
final private class ObjectFieldResolver[R, A]( | ||
objectName: String, | ||
fields: Iterable[(String, A => Step[R])] | ||
final private class ObjectFieldResolver[R, A] private ( | ||
name: String, | ||
fields: mutable.HashMap[String, A => Step[R]] | ||
) { | ||
|
||
private val fieldsMap: java.util.HashMap[String, A => Step[R]] = { | ||
val map = new java.util.HashMap[String, A => Step[R]]() | ||
fields.foreach { case (name, resolve) => map.put(name, resolve) } | ||
map | ||
} | ||
|
||
def resolve(value: A): Step[R] = MetadataFunctionStep(resolveForField(value, _)) | ||
|
||
private def resolveForField( | ||
value: A, | ||
field: Field | ||
): Step[R] = { | ||
val fieldsBuilder = new mutable.HashMap[String, Step[R]]() | ||
|
||
var remaining = field.distinctFieldNames | ||
while (!remaining.isEmpty) { | ||
val name = remaining.head | ||
val resolve = fieldsMap.get(name) | ||
if (resolve ne null) fieldsBuilder.update(name, resolve(value)) | ||
remaining = remaining.tail | ||
def resolve(value: A): Step[R] = ObjectStep( | ||
name, | ||
fields.get(_) match { | ||
case Some(f) => f(value) | ||
case None => NullStep | ||
} | ||
ObjectStep(objectName, fieldsBuilder) | ||
} | ||
) | ||
} | ||
|
||
private object ObjectFieldResolver { | ||
def apply[R, A](objectName: String, fields: Iterable[(String, A => Step[R])]): ObjectFieldResolver[R, A] = | ||
// NOTE: mutable.HashMap is about twice as fast than immutable.HashMap for .get | ||
new ObjectFieldResolver(objectName, mutable.HashMap.from(fields)) | ||
} |
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