-
Notifications
You must be signed in to change notification settings - Fork 222
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
Add a MappedAbstractType
that will transform the input value
#401
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,10 @@ sealed trait AbstractType extends Type with Named { | |
schema.possibleTypes get name flatMap (_.find(_ isInstanceOf value).asInstanceOf[Option[ObjectType[Ctx, _]]]) | ||
} | ||
|
||
sealed trait MappedAbstractType[T] extends Type with AbstractType with OutputType[T] { | ||
def contraMap(value: T): Any | ||
} | ||
|
||
sealed trait NullableType | ||
sealed trait UnmodifiedType | ||
|
||
|
@@ -295,6 +299,9 @@ case class UnionType[Ctx]( | |
astNodes: Vector[ast.AstNode] = Vector.empty) extends OutputType[Any] with CompositeType[Any] with AbstractType with NullableType with UnmodifiedType with HasAstInfo { | ||
def rename(newName: String) = copy(name = newName).asInstanceOf[this.type] | ||
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this) | ||
def map[T](func: T => Any): OutputType[T] with MappedAbstractType[T] = new UnionType[Ctx](name, description, types, astDirectives, astNodes) with MappedAbstractType[T] { | ||
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. I also a bit concerned about the name... I wonder whether "map" might be confusing for some people since its role here is a bit different from collections and other places where map often shows up. But maybe it's fine, I'm not 100% sure. WDYT? Maybe something more specific (like 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. mapValue is definitely better, since this stuff doesn't really follow laws or anything and especially since map here doesn't really change the type param of UnionType. Your call on it again. 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. fixed in f589aea |
||
override def contraMap(value: T): Any = func(value) | ||
}.asInstanceOf[OutputType[T] with MappedAbstractType[T]] | ||
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. Not 100% sure how I feel about this part. On one hand, I can understand practical reasons behind it, but on the other hand I'm not a big fan of such structures especially for case classes. Though it provides quite non intrusive way to enable this feature. I think this is nice, especially considering that it is not yet clear how scala 3 union types will affect the design. 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. Yeah so you nailed my thought process here, I did not way to break the UnionType contract. Honestly, I just wasn't sure what you'd think, so i'd be looking for your advice on this. I also dislike doing this to case classes. |
||
} | ||
|
||
case class Field[Ctx, Val]( | ||
|
@@ -760,7 +767,7 @@ case class Schema[Ctx, Val]( | |
|
||
def renderPretty: String = toAst.renderPretty | ||
def renderPretty(filter: SchemaFilter): String = toAst(filter).renderPretty | ||
|
||
def renderCompact: String = toAst.renderCompact | ||
def renderCompact(filter: SchemaFilter): String = toAst(filter).renderCompact | ||
|
||
|
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.
I think it would be safer to extract value after
isUndefinedValue
to avoid accidental NPEs. It checks for anull
(even thoughnull
is not supposed to be used,None
should be used to represent it, but code still should handle it gracefully).Maybe something like this? (this will also avoid duplication)
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.
Yeah definitely better, and good point about after isUndefined...
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.
Fixed in f589aea