From 6e88fa0ce88e351093cb7313ce3f2b3c91a80cfb Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Sat, 6 Jan 2024 11:03:55 -0600 Subject: [PATCH] Better Id parsing --- build.sbt | 2 +- .../src/main/scala/com/outr/arango/DocumentModel.scala | 10 +--------- core/src/main/scala/com/outr/arango/Id.scala | 10 ++++++++++ .../scala/com/outr/arango/core/ArangoDBDocuments.scala | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/build.sbt b/build.sbt index 4baddc2a..834e39f5 100644 --- a/build.sbt +++ b/build.sbt @@ -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") diff --git a/core/src/main/scala/com/outr/arango/DocumentModel.scala b/core/src/main/scala/com/outr/arango/DocumentModel.scala index 3e4b8295..76bff42f 100644 --- a/core/src/main/scala/com/outr/arango/DocumentModel.scala +++ b/core/src/main/scala/com/outr/arango/DocumentModel.scala @@ -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) } \ No newline at end of file diff --git a/core/src/main/scala/com/outr/arango/Id.scala b/core/src/main/scala/com/outr/arango/Id.scala index 40bf2a5c..b7354c53 100644 --- a/core/src/main/scala/com/outr/arango/Id.scala +++ b/core/src/main/scala/com/outr/arango/Id.scala @@ -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) ) diff --git a/driver/src/main/scala/com/outr/arango/core/ArangoDBDocuments.scala b/driver/src/main/scala/com/outr/arango/core/ArangoDBDocuments.scala index 27406738..85e0f06d 100644 --- a/driver/src/main/scala/com/outr/arango/core/ArangoDBDocuments.scala +++ b/driver/src/main/scala/com/outr/arango/core/ArangoDBDocuments.scala @@ -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)))