-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Parse search query from single string and json (#21)
To allow more flexible searching, a query string following renku terminology can be given. It can be parsed from a single string or a json structure.
- Loading branch information
Showing
25 changed files
with
1,484 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
modules/search-query/src/main/scala/io/renku/search/query/Comparison.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.query | ||
|
||
import io.bullet.borer.{Decoder, Encoder} | ||
|
||
enum Comparison: | ||
case Is | ||
case LowerThan | ||
case GreaterThan | ||
|
||
private[query] def asString = this match | ||
case Is => ":" | ||
case LowerThan => "<" | ||
case GreaterThan => ">" | ||
|
||
object Comparison: | ||
given Encoder[Comparison] = Encoder.forString.contramap(_.asString) | ||
given Decoder[Comparison] = Decoder.forString.mapEither(fromString) | ||
|
||
private[query] def fromString(str: String): Either[String, Comparison] = | ||
Comparison.values.find(_.asString == str).toRight(s"Invalid comparison: $str") | ||
|
||
private[query] def unsafeFromString(str: String): Comparison = | ||
fromString(str).fold(sys.error, identity) |
44 changes: 44 additions & 0 deletions
44
modules/search-query/src/main/scala/io/renku/search/query/DateTimeCalc.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.query | ||
|
||
import java.time.Period | ||
|
||
final case class DateTimeCalc( | ||
ref: PartialDateTime | RelativeDate, | ||
amount: Period, | ||
range: Boolean | ||
): | ||
def asString: String = | ||
val period = amount.getDays.abs | ||
val sep = | ||
if (range) DateTimeCalc.range | ||
else if (amount.isNegative) DateTimeCalc.sub | ||
else DateTimeCalc.add | ||
ref match | ||
case d: PartialDateTime => | ||
s"${d.asString}$sep${period}d" | ||
|
||
case d: RelativeDate => | ||
s"${d.name}$sep${period}d" | ||
|
||
object DateTimeCalc: | ||
private[query] val add: String = "+" | ||
private[query] val sub: String = "-" | ||
private[query] val range: String = "/" |
43 changes: 43 additions & 0 deletions
43
modules/search-query/src/main/scala/io/renku/search/query/DateTimeRef.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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.query | ||
|
||
import cats.syntax.all.* | ||
import io.bullet.borer.{Decoder, Encoder} | ||
import io.renku.search.query.parse.DateTimeParser | ||
|
||
enum DateTimeRef: | ||
case Literal(ref: PartialDateTime) | ||
case Relative(ref: RelativeDate) | ||
case Calc(ref: DateTimeCalc) | ||
|
||
val asString: String = this match | ||
case Literal(ref) => ref.asString | ||
case Relative(ref) => ref.name | ||
case Calc(ref) => ref.asString | ||
|
||
object DateTimeRef: | ||
given Encoder[DateTimeRef] = Encoder.forString.contramap(_.asString) | ||
given Decoder[DateTimeRef] = Decoder.forString.mapEither { str => | ||
DateTimeParser.dateTimeRef.parseAll(str).leftMap(_.show) | ||
} | ||
|
||
def apply(ref: PartialDateTime): DateTimeRef = Literal(ref) | ||
def apply(ref: RelativeDate): DateTimeRef = Relative(ref) | ||
def apply(ref: DateTimeCalc): DateTimeRef = Calc(ref) |
45 changes: 45 additions & 0 deletions
45
modules/search-query/src/main/scala/io/renku/search/query/Field.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.query | ||
|
||
import io.bullet.borer.{Decoder, Encoder} | ||
|
||
enum Field: | ||
case ProjectId | ||
case Name | ||
case Slug | ||
case Visibility | ||
case Created | ||
case CreatedBy | ||
|
||
val name: String = Strings.lowerFirst(productPrefix) | ||
|
||
object Field: | ||
given Encoder[Field] = Encoder.forString.contramap(_.name) | ||
given Decoder[Field] = Decoder.forString.mapEither(fromString) | ||
|
||
private[this] val allNames: String = Field.values.mkString(", ") | ||
|
||
def fromString(str: String): Either[String, Field] = | ||
Field.values | ||
.find(_.name.equalsIgnoreCase(str)) | ||
.toRight(s"Invalid field: $str. Allowed are: $allNames") | ||
|
||
def unsafeFromString(str: String): Field = | ||
fromString(str).fold(sys.error, identity) |
56 changes: 56 additions & 0 deletions
56
modules/search-query/src/main/scala/io/renku/search/query/FieldTerm.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.query | ||
|
||
import cats.data.NonEmptyList | ||
import io.renku.search.model.projects.Visibility | ||
|
||
enum FieldTerm(val field: Field, val cmp: Comparison): | ||
case ProjectIdIs(values: NonEmptyList[String]) | ||
extends FieldTerm(Field.ProjectId, Comparison.Is) | ||
case NameIs(values: NonEmptyList[String]) extends FieldTerm(Field.Name, Comparison.Is) | ||
case SlugIs(values: NonEmptyList[String]) extends FieldTerm(Field.Slug, Comparison.Is) | ||
case VisibilityIs(values: NonEmptyList[Visibility]) | ||
extends FieldTerm(Field.Visibility, Comparison.Is) | ||
case Created(override val cmp: Comparison, values: NonEmptyList[DateTimeRef]) | ||
extends FieldTerm(Field.Created, cmp) | ||
case CreatedByIs(values: NonEmptyList[String]) | ||
extends FieldTerm(Field.CreatedBy, Comparison.Is) | ||
|
||
private[query] def asString = | ||
val value = this match | ||
case ProjectIdIs(values) => FieldTerm.nelToString(values) | ||
case NameIs(values) => FieldTerm.nelToString(values) | ||
case SlugIs(values) => FieldTerm.nelToString(values) | ||
case VisibilityIs(values) => | ||
val vis = values.toList.distinct.map(_.name) | ||
vis.mkString(",") | ||
case Created(_, values) => FieldTerm.nelToString(values.map(_.asString)) | ||
case CreatedByIs(values) => FieldTerm.nelToString(values) | ||
|
||
s"${field.name}${cmp.asString}${value}" | ||
|
||
object FieldTerm: | ||
private def quote(s: String): String = | ||
if (s.exists(c => c.isWhitespace || c == ',' || c == '"')) | ||
s"\"${s.replace("\"", "\\\"")}\"" | ||
else s | ||
|
||
private def nelToString(nel: NonEmptyList[String]): String = | ||
nel.map(quote).toList.mkString(",") |
Oops, something went wrong.