Skip to content

Commit

Permalink
Better Id parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Jan 6, 2024
1 parent 8b9424f commit 6e88fa0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val scala3 = "3.3.1"

name := "scarango"
ThisBuild / organization := "com.outr"
ThisBuild / version := "3.18.2"
ThisBuild / version := "3.18.3-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := List(scala3, scala213)
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation")
Expand Down
10 changes: 1 addition & 9 deletions core/src/main/scala/com/outr/arango/DocumentModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,5 @@ trait DocumentModel[D <: Document[D]] { model =>

def collectionOptions: CreateCollectionOptions = CreateCollectionOptions()

def id(value: String = generateId()): Id[D] = {
val index = value.indexOf('/')
val v = if (index != -1) {
value.substring(index + 1)
} else {
value
}
Id[D](v, collectionName)
}
def id(value: String = generateId()): Id[D] = Id.parse[D](value, collectionName)
}
10 changes: 10 additions & 0 deletions core/src/main/scala/com/outr/arango/Id.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ object Id {
case ExtractorRegex(collection, value) => Id[D](value, collection)
}

def parse[D](key: String, collectionName: String): Id[D] = {
val index = key.indexOf('/')
val v = if (index != -1) {
key.substring(index + 1)
} else {
key
}
Id[D](v, collectionName)
}

def isValid(key: String): Boolean = key.forall(c =>
c.isLetterOrDigit || ValidSpecialChars.contains(c)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait ArangoDBDocuments[T] {

def count: IO[Int] = io(_collection.count()).map(_.getCount.toInt)

def id(key: String): Id[T] = Id[T](key, _collection.name())
def id(key: String): Id[T] = Id.parse[T](key, _collection.name())

def apply(id: Id[T],
default: Id[T] => T = id => throw NotFoundException(id._id)): IO[T] = get(id).map(_.getOrElse(default(id)))
Expand Down

0 comments on commit 6e88fa0

Please sign in to comment.