Skip to content
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

feat: type discriminator field in the search API response #24

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package io.renku.search.api
import cats.effect.{Async, Resource}
import cats.syntax.all.*
import fs2.io.net.Network
import io.renku.search.api.Project.given
import io.renku.search.http.borer.TapirBorerJson
import io.renku.solr.client.SolrConfig
import org.http4s.dsl.Http4sDsl
Expand Down Expand Up @@ -60,13 +59,14 @@ class HttpApplication[F[_]: Async](searchApi: SearchApi[F])
searchEndpoint.serverLogic(searchApi.find)
)

private lazy val searchEndpoint: PublicEndpoint[String, String, List[Project], Any] =
private lazy val searchEndpoint
: PublicEndpoint[String, String, List[SearchEntity], Any] =
val query =
path[String].name("user query").description("User defined query e.g. renku~")
endpoint.get
.in(query)
.errorOut(borerJsonBody[String])
.out(borerJsonBody[List[Project]])
.out(borerJsonBody[List[SearchEntity]])
.description("Search API for searching Renku entities")

private lazy val swaggerEndpoints =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import io.renku.search.solr.client.SearchSolrClient
import io.renku.solr.client.SolrConfig

trait SearchApi[F[_]]:
def find(phrase: String): F[Either[String, List[Project]]]
def find(phrase: String): F[Either[String, List[SearchEntity]]]

object SearchApi:
def apply[F[_]: Async: Network](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ private class SearchApiImpl[F[_]: Async](solrClient: SearchSolrClient[F])

private given Scribe[F] = scribe.cats[F]

override def find(phrase: String): F[Either[String, List[Project]]] =
override def find(phrase: String): F[Either[String, List[SearchEntity]]] =
solrClient
.findProjects(phrase)
.map(toApiModel)
.map(_.asRight[String])
.handleErrorWith(errorResponse(phrase))
.widen

private def errorResponse(
phrase: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 Swiss Data Science Center (SDSC)
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
* Eidgenössische Technische Hochschule Zürich (ETHZ).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.renku.search.api

import io.bullet.borer.derivation.MapBasedCodecs.{deriveAllCodecs, deriveCodec}
import io.bullet.borer.{AdtEncodingStrategy, Codec, Decoder, Encoder}
import io.renku.search.model.*
import sttp.tapir.Schema.SName
import sttp.tapir.SchemaType.{SDateTime, SProductField}
import sttp.tapir.generic.Configuration
import sttp.tapir.{FieldName, Schema, SchemaType}

sealed trait SearchEntity

final case class Project(
id: projects.Id,
name: projects.Name,
slug: projects.Slug,
repositories: Seq[projects.Repository],
visibility: projects.Visibility,
description: Option[projects.Description] = None,
createdBy: User,
creationDate: projects.CreationDate,
members: Seq[User]
) extends SearchEntity

object Project:
private given Schema[projects.Id] = Schema.string[projects.Id]
private given Schema[projects.Name] = Schema.string[projects.Name]
private given Schema[projects.Slug] = Schema.string[projects.Slug]
private given Schema[projects.Repository] = Schema.string[projects.Repository]
private given Schema[projects.Visibility] =
Schema.derivedEnumeration[projects.Visibility].defaultStringBased
private given Schema[projects.Description] = Schema.string[projects.Description]
private given Schema[projects.CreationDate] = Schema(SDateTime())
given Schema[Project] = Schema.derived[Project]

final case class User(
id: users.Id
)

object User:
given Codec[User] = deriveCodec[User]

private given Schema[users.Id] = Schema.string[users.Id]
given Schema[User] = Schema.derived[User]

object SearchEntity:

private val discriminatorField = "type"
given AdtEncodingStrategy = AdtEncodingStrategy.flat(discriminatorField)
given Codec[SearchEntity] = deriveAllCodecs[SearchEntity]

given Schema[SearchEntity] = {
val derived = Schema.derived[SearchEntity]
derived.schemaType match {
case s: SchemaType.SCoproduct[_] =>
derived.copy(schemaType =
s.addDiscriminatorField(
FieldName(discriminatorField),
Schema.string,
List(
implicitly[Schema[Project]].name.map(SchemaType.SRef(_)).map("Project" -> _)
).flatten.toMap
)
)
case s => derived
}
}
Loading