Skip to content

Commit

Permalink
Allow schemaVersion to be not prefixed (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek authored May 10, 2024
1 parent 1c8844a commit bf570f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@ object SchemaVersion:
val all: NonEmptyList[SchemaVersion] =
NonEmptyList.fromListUnsafe(SchemaVersion.values.toList)

// the avro schema defines the version to be a string, not an enum
// we try a few values that would make sense here
private val candidateValues = all.toList.map { v =>
v -> Set(v.name, v.name.toLowerCase(), v.name.drop(1))
}.toMap

def fromString(s: String): Either[String, SchemaVersion] =
all.find(_.name.equalsIgnoreCase(s)).toRight(s"Invalid schema version: $s")
candidateValues
.find(_._2.contains(s))
.map(_._1)
.toRight(s"Invalid schema version: $s")
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.events

import io.renku.search.events.SchemaVersion
import munit.FunSuite

class SchemaVersionSpec extends FunSuite:

test("parse values successfully"):
val v1Values = List("v1", "V1", "1")
v1Values.foreach { str =>
assertEquals(SchemaVersion.fromString(str), Right(SchemaVersion.V1))
}

val v2Values = List("v2", "V2", "2")
v2Values.foreach { str =>
assertEquals(SchemaVersion.fromString(str), Right(SchemaVersion.V2))
}

0 comments on commit bf570f6

Please sign in to comment.