Skip to content

Commit

Permalink
Merge pull request #45 from kubukoz/enum-names
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Jul 12, 2022
2 parents 940b88b + 236b665 commit 0c798b4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
28 changes: 18 additions & 10 deletions core/src/main/scala/playground/QueryCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ sealed trait CompilationErrorDetails extends Product with Serializable {

def render: String =
this match {
case InvalidUUID => "Invalid UUID"
case InvalidUUID => "Invalid UUID"
case EnumFallback(enumName) =>
s"""Matching enums by value is deprecated and may be removed in the future. Use $enumName instead.""".stripMargin
case DuplicateItem => "Duplicate item - some entries will be dropped to fit in a set shape."
case AmbiguousService(matching) =>
s"""Multiple services are available. Add a use clause to specify the service you want to use.
Expand Down Expand Up @@ -206,6 +208,7 @@ object CompilationErrorDetails {
final case class UnsupportedNode(tag: String) extends CompilationErrorDetails

case object DuplicateItem extends CompilationErrorDetails
final case class EnumFallback(enumName: String) extends CompilationErrorDetails
}

object QueryCompiler extends SchemaVisitor[PartialCompiler] {
Expand Down Expand Up @@ -499,16 +502,21 @@ object QueryCompiler extends SchemaVisitor[PartialCompiler] {
values: List[EnumValue[E]],
total: E => EnumValue[E],
): PartialCompiler[E] = (string, PartialCompiler.pos).tupled.emap { case (name, range) =>
values
val byValue = values
.find(_.stringValue == name)
.map(_.value)
.toRightIor(
CompilationError(
UnknownEnumValue(name, values.map(_.stringValue)),
range,
)
)
.toIorNec

val byName = values
.find(_.name == name)

(byName, byValue) match {
case (Some(v), _) => v.value.pure[PartialCompiler.Result]

case (None, Some(v)) => Ior.bothNec(CompilationError(EnumFallback(v.name), range), v.value)

case (None, None) =>
Ior.leftNec(CompilationError(UnknownEnumValue(name, values.map(_.name)), range))
}

}

private def listWithPos[S](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ object CompletionVisitor extends SchemaVisitor[CompletionResolver] {
.map { enumValue =>
CompletionItem.fromHints(
CompletionItemKind.EnumMember,
enumValue.stringValue,
InsertText.JustString(transformString(enumValue.stringValue)),
enumValue.name,
InsertText.JustString(transformString(enumValue.name)),
Schema.enumeration(total, values).addHints(hints).withId(shapeId),
)
}
Expand Down
30 changes: 26 additions & 4 deletions core/src/test/scala/playground/smithyql/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,41 @@ object CompilationTests extends SimpleIOSuite with Checkers {
}

pureTest("enum - OK") {
val result = compile[Power](WithSource.liftId("WIND".mapK(WithSource.liftId)))

assert(
result == Ior.right(Power.WIND)
)
}

pureTest("enum - fallback to string value") {
val aRange = SourceRange(Position(10), Position(20))

val result = compile[Power](WithSource.liftId("Wind".mapK(WithSource.liftId)).withRange(aRange))

val expected: PartialCompiler.Result[Power] = Ior.both(
NonEmptyChain.one(
CompilationError(
CompilationErrorDetails.EnumFallback("WIND"),
aRange,
)
),
Power.WIND,
)

assert(
compile[Power](WithSource.liftId("Wind".mapK(WithSource.liftId))) == Ior.right(Power.WIND)
result == expected
)
}

pureTest("enum - failure") {
assert(
compile[Power](WithSource.liftId("Poison".mapK(WithSource.liftId))) == Ior.left(
compile[Power](WithSource.liftId("POISON".mapK(WithSource.liftId))) == Ior.left(
NonEmptyChain.of(
CompilationError(
CompilationErrorDetails.UnknownEnumValue(
"Poison",
List("Ice", "Fire", "Lightning", "Wind"),
"POISON",
List("ICE", "FIRE", "LIGHTNING", "WIND"),
),
SourceRange(Position(0), Position(0)),
)
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/playground/smithyql/CompletionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ object CompletionTests extends FunSuite {
val completions = getCompletions(Power.schema, Nil)

val inserts = completions.map(_.insertText)
val expectedInserts = List("Ice", "Fire", "Lightning", "Wind")
val expectedInserts = List("ICE", "FIRE", "LIGHTNING", "WIND")
.map(s => s"\"$s\"")
.map(InsertText.JustString(_))

Expand All @@ -141,7 +141,7 @@ object CompletionTests extends FunSuite {
val completions = getCompletions(Power.schema, List(Quotes))

val inserts = completions.map(_.insertText)
val expectedInserts = List("Ice", "Fire", "Lightning", "Wind")
val expectedInserts = List("ICE", "FIRE", "LIGHTNING", "WIND")
.map(InsertText.JustString(_))

assert(completions.map(_.kind).forall(_ == CompletionItemKind.EnumMember)) &&
Expand All @@ -161,7 +161,7 @@ object CompletionTests extends FunSuite {

val inserts = completions.map(_.insertText)

val expectedInserts = List("Ice", "Fire", "Lightning", "Wind")
val expectedInserts = List("ICE", "FIRE", "LIGHTNING", "WIND")
.map(_ + " = ")
.map(InsertText.JustString(_))

Expand Down
3 changes: 2 additions & 1 deletion vscode-extension/src/main/scala/playground/highlight.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import typings.vscode.mod.DiagnosticSeverity
import types._
import cats.data.Ior
import playground.CompilationErrorDetails.DuplicateItem
import playground.CompilationErrorDetails.EnumFallback

object highlight {

Expand Down Expand Up @@ -117,7 +118,7 @@ object highlight {
ee.err match {
// todo: support warnings in "CompilationFailed"
// todo2: rename CompilationFailed cause it makes no sense with warnings
case DuplicateItem =>
case DuplicateItem | _: EnumFallback =>
warn(
ee.err.render,
adapters.toVscodeRange(doc, ee.range),
Expand Down

0 comments on commit 0c798b4

Please sign in to comment.