Skip to content

Commit

Permalink
Merge pull request #201 from lichess-org/inline-ouputs-for-smithy
Browse files Browse the repository at this point in the history
Inline outputs for smithy & RC3
  • Loading branch information
lenguyenthanh authored May 14, 2024
2 parents 7704fc3 + 7fd9d89 commit abd5300
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inThisBuild(
Seq(
scalaVersion := scala213,
versionScheme := Some("early-semver"),
version := "3.0.0-RC2",
version := "3.0.0-RC3",
organization := "org.lichess.search",
run / fork := true,
run / javaOptions += "-Dconfig.override_with_env_vars=true",
Expand Down
12 changes: 0 additions & 12 deletions modules/api/src/main/smithy/_global.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ structure InternalServerError {
message: String
}

structure SearchResponse {
@required
hitIds: Ids
}

list Ids {
member: String
}
Expand All @@ -26,16 +21,9 @@ list Strings {
member: String
}

structure CountResponse {
@required
count: Integer
}


@trait(selector: "string")
structure DateTimeFormat {}


apply lila.search.spec#DateTimeFormat @refinement(
targetType: "lila.search.spec.SearchDateTime"
)
Expand Down
10 changes: 8 additions & 2 deletions modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ operation Search {
size: Integer
}

output: SearchResponse
output := {
@required
hitIds: Ids
}

errors: [InternalServerError]
}
Expand All @@ -44,7 +47,10 @@ operation Count {
query: Query
}

output: CountResponse
output := {
@required
count: Integer
}

errors: [InternalServerError]
}
Expand Down
8 changes: 4 additions & 4 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ class SearchServiceImpl(esClient: ESClient[IO])(using logger: Logger[IO]) extend
logger.error(e)(s"Error in deleteByIds: index=$index, ids=$ids") *>
IO.raiseError(InternalServerError("Internal server error"))

override def count(query: Query): IO[CountResponse] =
override def count(query: Query): IO[CountOutput] =
esClient
.count(query.index, query)
.map(_.to[CountResponse])
.map(x => CountOutput(x.count))
.handleErrorWith: e =>
logger.error(e)(s"Error in count: query=$query") *>
IO.raiseError(InternalServerError("Internal server error"))

override def search(query: Query, from: Int, size: Int): IO[SearchResponse] =
override def search(query: Query, from: Int, size: Int): IO[SearchOutput] =
esClient
.search(query.index, query, From(from), Size(size))
.map(_.to[SearchResponse])
.map(x => SearchOutput(x.hitIds))
.handleErrorWith: e =>
logger.error(e)(s"Error in searchForum: query=$query, from=$from, size=$size") *>
IO.raiseError(InternalServerError("Internal server error"))
Expand Down
6 changes: 3 additions & 3 deletions modules/client/src/main/scala/NoopSearchClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ object NoopSearchClient extends SearchClient:

override def deleteById(index: Index, id: String): Future[Unit] = Future.successful(())

override def search(query: Query, from: Int, size: Int): Future[SearchResponse] =
Future.successful(SearchResponse(Nil))
override def search(query: Query, from: Int, size: Int): Future[SearchOutput] =
Future.successful(SearchOutput(Nil))

override def store(id: String, source: Source): Future[Unit] = Future.successful(())

override def storeBulkGame(sources: List[GameSourceWithId]): Future[Unit] = Future.successful(())

override def storeBulkStudy(sources: List[StudySourceWithId]): Future[Unit] = Future.successful(())

override def count(query: Query): Future[CountResponse] = Future.successful(CountResponse(0))
override def count(query: Query): Future[CountOutput] = Future.successful(CountOutput(0))

override def mapping(index: Index): Future[Unit] = Future.successful(())
4 changes: 2 additions & 2 deletions modules/client/src/main/scala/PlaySearchClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class PlaySearchClient(client: StandaloneWSClient, baseUrl: String)(using Execut
override def deleteByIds(index: Index, ids: List[String]): Future[Unit] =
request_(s"$baseUrl/delete/ids/${index.value}", IdsInput(ids))

override def count(query: Query): Future[CountResponse] =
override def count(query: Query): Future[CountOutput] =
request(s"$baseUrl/count", SearchInput(query))

override def search(query: Query, from: Int, size: Int): Future[SearchResponse] =
override def search(query: Query, from: Int, size: Int): Future[SearchOutput] =
request(s"$baseUrl/search/$from/$size", SearchInput(query))

private def request[D: Schema, R: Schema](url: String, data: D): Future[R] =
Expand Down
6 changes: 3 additions & 3 deletions modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import lila.search.app.AppResources
import lila.search.app.SearchApp
import lila.search.app.{ AppConfig, ElasticConfig, HttpServerConfig }
import lila.search.client.SearchClient
import lila.search.spec.{ Query, Index as SpecIndex, Source }
import lila.search.spec.{ Query, Index as SpecIndex, Source, SearchOutput, CountOutput }
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import play.api.libs.ws.*
Expand All @@ -35,11 +35,11 @@ object CompatSuite extends weaver.IOSuite:

test("search endpoint"): client =>
val query = Query.Forum("foo")
IO.fromFuture(IO(client.search(query, 0, 10))).map(expect.same(_, lila.search.spec.SearchResponse(Nil)))
IO.fromFuture(IO(client.search(query, 0, 10))).map(expect.same(_, lila.search.spec.SearchOutput(Nil)))

test("count endpoint"): client =>
val query = Query.Team("foo")
IO.fromFuture(IO(client.count(query))).map(expect.same(_, lila.search.spec.CountResponse(0)))
IO.fromFuture(IO(client.count(query))).map(expect.same(_, lila.search.spec.CountOutput(0)))

test("deleteById endpoint"): client =>
IO.fromFuture(IO(client.deleteById(SpecIndex.Game, "iddddd"))).map(expect.same(_, ()))
Expand Down

0 comments on commit abd5300

Please sign in to comment.