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

search-api: Index grep with dump rather than calling endpoints #572

Merged
merged 8 commits into from
Jan 14, 2025
Prev Previous commit
Next Next commit
search-api: Add typename discriminator hack
Since scala-tsi messes up adding the `typename` discriminator we need to
hack around it by adding the fields manually to the variants that does
not get it 🤷
jnatten committed Jan 8, 2025

Verified

This commit was signed with the committer’s verified signature.
rouault Even Rouault
commit 939a07013851035b81647dc6ef898b566183e3cb
10 changes: 6 additions & 4 deletions project/Module.scala
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@ import GithubWorkflowPlugin.autoImport.*
import com.scalatsi.plugin.ScalaTsiPlugin.autoImport.{
typescriptExports,
typescriptGenerationImports,
typescriptOutputFile
typescriptOutputFile,
typescriptTaggedUnionDiscriminator
}
import org.scalafmt.sbt.ScalafmtPlugin.autoImport.*
import org.typelevel.sbt.tpolecat.TpolecatPlugin.autoImport.*
@@ -192,9 +193,10 @@ trait Module {

protected def typescriptSettings(imports: Seq[String], exports: Seq[String]) = {
Seq(
typescriptGenerationImports := imports,
typescriptExports := exports,
typescriptOutputFile := file("./typescript/types-backend") / s"${this.moduleName}.ts"
typescriptGenerationImports := imports,
typescriptExports := exports,
typescriptOutputFile := file("./typescript/types-backend") / s"${this.moduleName}.ts",
typescriptTaggedUnionDiscriminator := Some("typename")
)
}
}
3 changes: 2 additions & 1 deletion project/searchapi.scala
Original file line number Diff line number Diff line change
@@ -43,7 +43,8 @@ object searchapi extends Module {
"SubjectAggregationsDTO",
"SubjectAggsInputDTO",
"GrepSearchInputDTO",
"grep.GrepSearchResultsDTO"
"grep.GrepSearchResultsDTO",
"grep.GrepResultDTO"
)
)

Original file line number Diff line number Diff line change
@@ -9,8 +9,14 @@
package no.ndla.searchapi.model.api.grep

import cats.implicits.*
import com.scalatsi.TSType.fromCaseClass
import com.scalatsi.TypescriptType.{TSLiteralString, TSString, TSUnion}
import com.scalatsi.{TSIType, TSNamedType, TSType}
import com.scalatsi.dsl.*
import io.circe.generic.auto.*
import sttp.tapir.generic.auto.*

import scala.reflect.runtime.universe.*
import io.circe.syntax.*
import io.circe.{Decoder, Encoder, Json}
import no.ndla.language.Language
@@ -29,6 +35,7 @@ import no.ndla.searchapi.model.search.SearchableGrepElement
import sttp.tapir.Schema
import sttp.tapir.Schema.annotations.description

import scala.reflect.ClassTag
import scala.util.{Success, Try}

@description("Information about a single grep search result entry")
@@ -48,9 +55,12 @@ object GrepResultDTO {
}
// NOTE: Adding the discriminator field that scala-tsi generates in the typescript type.
// Useful for guarding the type of the object in the frontend.
json.mapObject(_.add("type", Json.fromString(result.getClass.getSimpleName)))
json.mapObject(_.add("typename", Json.fromString(result.getClass.getSimpleName)))
}

implicit val s1: Schema["GrepLaererplanDTO"] = Schema.string
implicit val s2: Schema["GrepTverrfagligTemaDTO"] = Schema.string

implicit val decoder: Decoder[GrepResultDTO] = List[Decoder[GrepResultDTO]](
Decoder[GrepKjerneelementDTO].widen,
Decoder[GrepKompetansemaalDTO].widen,
@@ -169,9 +179,11 @@ case class GrepKompetansemaalSettDTO(
) extends GrepResultDTO
case class GrepLaererplanDTO(
code: String,
title: TitleDTO
title: TitleDTO,
typename: "GrepLaererplanDTO" = "GrepLaererplanDTO"
) extends GrepResultDTO
case class GrepTverrfagligTemaDTO(
code: String,
title: TitleDTO
title: TitleDTO,
typename: "GrepTverrfagligTemaDTO" = "GrepTverrfagligTemaDTO"
) extends GrepResultDTO
11 changes: 6 additions & 5 deletions typescript/types-backend/search-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT EDIT: generated file by scala-tsi

export type GrepResultDTO = (IGrepKompetansemaalDTO | IGrepKjerneelementDTO | IGrepLaererplanDTO | IGrepTverrfagligTemaDTO | IGrepKompetansemaalSettDTO)
export type GrepResultDTO = (IGrepKjerneelementDTO | IGrepKompetansemaalSettDTO | IGrepTverrfagligTemaDTO | IGrepKompetansemaalDTO | IGrepLaererplanDTO)

export type GrepSort = ("-relevance" | "relevance" | "-title" | "title" | "-code" | "code")

@@ -122,7 +122,7 @@ export interface IGrepKjerneelementDTO {
title: ITitleDTO
description: IDescriptionDTO
laereplan: IGrepLaererplanDTO
type: "GrepKjerneelementDTO"
typename: "GrepKjerneelementDTO"
}

export interface IGrepKompetansemaalDTO {
@@ -132,20 +132,20 @@ export interface IGrepKompetansemaalDTO {
kompetansemaalSett: IGrepReferencedKompetansemaalSettDTO
tverrfagligeTemaer: IGrepTverrfagligTemaDTO[]
kjerneelementer: IGrepReferencedKjerneelementDTO[]
type: "GrepKompetansemaalDTO"
typename: "GrepKompetansemaalDTO"
}

export interface IGrepKompetansemaalSettDTO {
code: string
title: ITitleDTO
kompetansemaal: IGrepReferencedKompetansemaalDTO[]
type: "GrepKompetansemaalSettDTO"
typename: "GrepKompetansemaalSettDTO"
}

export interface IGrepLaererplanDTO {
code: string
title: ITitleDTO
type: "GrepLaererplanDTO"
typename: "GrepLaererplanDTO"
}

export interface IGrepReferencedKjerneelementDTO {
@@ -184,6 +184,7 @@ export interface IGrepSearchResultsDTO {
export interface IGrepTverrfagligTemaDTO {
code: string
title: ITitleDTO
typename: "GrepTverrfagligTemaDTO"
}

export interface IGroupSearchResultDTO {