~jan0sch/smederee

Showing details for patch 2b1f43ec7dababe23c1fbff1afb92e859086d3f4.
2023-02-13 (Mon), 1:09 PM - Jens Grassel - 2b1f43ec7dababe23c1fbff1afb92e859086d3f4

Refactoring: Move extension methods into companion objects if possible.

This should avoid namespace cluttering and compiler errors like "method
already defined at ..."
Summary of changes
11 files modified with 178 lines added and 173 lines removed
  • modules/hub/src/main/scala/de/smederee/html/MetaTags.scala with 7 added and 6 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/Account.scala with 42 added and 41 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/EditVcsRepositoryForm.scala with 25 added and 26 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/config/SmedereeHubConfig.scala with 21 added and 18 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/types.scala with 3 added and 3 removed lines
  • modules/hub/src/main/scala/de/smederee/ssh/PublicSshKey.scala with 36 added and 35 removed lines
  • modules/hub/src/main/scala/de/smederee/ssh/SshServer.scala with 12 added and 11 removed lines
  • modules/hub/src/main/scala/de/smederee/tickets/Label.scala with 4 added and 4 removed lines
  • modules/hub/src/main/scala/de/smederee/tickets/LabelForm.scala with 17 added and 17 removed lines
  • modules/hub/src/main/scala/de/smederee/tickets/LabelRoutes.scala with 0 added and 1 removed lines
  • modules/security/src/main/scala/de/smederee/security/PrivateKey.scala with 11 added and 11 removed lines
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/html/MetaTags.scala new-smederee/modules/hub/src/main/scala/de/smederee/html/MetaTags.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/html/MetaTags.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/html/MetaTags.scala	2025-01-31 19:52:03.053281924 +0000
@@ -78,12 +78,13 @@
       case Nil      => None
       case keywords => Option(keywords)
     }
-}
 
-extension (keywords: MetaKeyWords) {
-  def isEmpty: Boolean  = keywords.isEmpty
-  def mkString: String  = keywords.toList.mkString(", ")
-  def nonEmpty: Boolean = keywords.nonEmpty
+  extension (keywords: MetaKeyWords) {
+    def isEmpty: Boolean  = keywords.isEmpty
+    def mkString: String  = keywords.toList.mkString(", ")
+    def nonEmpty: Boolean = keywords.nonEmpty
+  }
+
 }
 
 /** HTML meta attributes which can be written into the header part of an HTML page.
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/Account.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/Account.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/Account.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/Account.scala	2025-01-31 19:52:03.057281931 +0000
@@ -80,23 +80,24 @@
       case (email, _, _, _, _, _) => email
     }
   }
-}
 
-extension (email: Email) {
+  extension (email: Email) {
 
-  /** Convert the email into a [[de.smederee.email.FromAddress]] to be useable within the email middleware.
-    *
-    * @return
-    *   The email address to be used in a `From` header.
-    */
-  def toFromAddress: FromAddress = FromAddress(email.toString)
+    /** Convert the email into a [[de.smederee.email.FromAddress]] to be useable within the email middleware.
+      *
+      * @return
+      *   The email address to be used in a `From` header.
+      */
+    def toFromAddress: FromAddress = FromAddress(email.toString)
+
+    /** Convert the email into a [[de.smederee.email.ToAddress]] to be useable within the email middleware.
+      *
+      * @return
+      *   The email address to be used in a `To` header.
+      */
+    def toToAddress: ToAddress = ToAddress(email.toString)
+  }
 
-  /** Convert the email into a [[de.smederee.email.ToAddress]] to be useable within the email middleware.
-    *
-    * @return
-    *   The email address to be used in a `To` header.
-    */
-  def toToAddress: ToAddress = ToAddress(email.toString)
 }
 
 /** A password is stored as an `Array[Byte]` internally and its `validate(source: String)` function will check that the
@@ -413,28 +414,28 @@
     Eq.instance { (a, b) =>
       a.uid === b.uid && a.name === b.name && a.email === b.email
     }
-}
 
-extension (account: Account) {
+  extension (account: Account) {
 
-  /** Create an assignee entity from this account.
-    *
-    * @return
-    *   An [[Assignee]] which is used to related to tickets being worked on.
-    */
-  def toAssignee: Assignee = Assignee(account.uid, account.name)
-
-  /** Create a submitter entity from this account.
-    *
-    * @return
-    *   A [[Submitter]] who created a ticket.
-    */
-  def toSubmitter: Submitter = Submitter(account.uid, account.name)
-
-  /** Create vcs repository owner metadata from the account.
-    *
-    * @return
-    *   Descriptive information about the owner of a vcs repository based on the account.
-    */
-  def toVcsRepositoryOwner: VcsRepositoryOwner = VcsRepositoryOwner(account.uid, account.name)
+    /** Create an assignee entity from this account.
+      *
+      * @return
+      *   An [[Assignee]] which is used to related to tickets being worked on.
+      */
+    def toAssignee: Assignee = Assignee(account.uid, account.name)
+
+    /** Create a submitter entity from this account.
+      *
+      * @return
+      *   A [[Submitter]] who created a ticket.
+      */
+    def toSubmitter: Submitter = Submitter(account.uid, account.name)
+
+    /** Create vcs repository owner metadata from the account.
+      *
+      * @return
+      *   Descriptive information about the owner of a vcs repository based on the account.
+      */
+    def toVcsRepositoryOwner: VcsRepositoryOwner = VcsRepositoryOwner(account.uid, account.name)
+  }
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/config/SmedereeHubConfig.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/config/SmedereeHubConfig.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/config/SmedereeHubConfig.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/config/SmedereeHubConfig.scala	2025-01-31 19:52:03.057281931 +0000
@@ -141,10 +141,11 @@
     *   An option to the successfully converted DirectoryPath.
     */
   def fromString(source: String): Option[DirectoryPath] = Try(Paths.get(source)).toOption
-}
 
-extension (dir: DirectoryPath) {
-  def toPath: Path = dir
+  extension (dir: DirectoryPath) {
+    def toPath: Path = dir
+  }
+
 }
 
 opaque type FailedAttempts = Int
@@ -171,10 +172,11 @@
       Option(source)
     else
       None
-}
 
-extension (attempts: FailedAttempts) {
-  def toInt: Int = attempts
+  extension (attempts: FailedAttempts) {
+    def toInt: Int = attempts
+  }
+
 }
 
 opaque type LanguageCode = String
@@ -202,16 +204,17 @@
       case Some(true) => Option(source)
       case _          => None
     }
-}
 
-extension (code: LanguageCode) {
+  extension (code: LanguageCode) {
+
+    /** Convert the language code into a [[java.util.Locale]] to be used for internationalisation functions.
+      *
+      * @return
+      *   A locale retrieved via the `forLanguageTag` method.
+      */
+    def toLocale: java.util.Locale = java.util.Locale.forLanguageTag(code.toString)
+  }
 
-  /** Convert the language code into a [[java.util.Locale]] to be used for internationalisation functions.
-    *
-    * @return
-    *   A locale retrieved via the `forLanguageTag` method.
-    */
-  def toLocale: java.util.Locale = java.util.Locale.forLanguageTag(code.toString)
 }
 
 /** Global constants which are used throughout the code.
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/EditVcsRepositoryForm.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/EditVcsRepositoryForm.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/EditVcsRepositoryForm.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/EditVcsRepositoryForm.scala	2025-01-31 19:52:03.057281931 +0000
@@ -101,31 +101,30 @@
     }
   }
 
-}
-
-extension (form: EditVcsRepositoryForm) {
+  extension (form: EditVcsRepositoryForm) {
 
-  /** Convert the form class into a stringified map which is used as underlying data type for form handling in the twirl
-    * templating library.
-    *
-    * @return
-    *   A stringified map containing the data of the form.
-    */
-  def toMap: Map[String, String] = {
-    val isPrivate =
-      if (form.isPrivate)
-        "true"
-      else
-        "false"
-    val formData = Map(
-      EditVcsRepositoryForm.fieldName.toString      -> form.name.toString,
-      EditVcsRepositoryForm.fieldIsPrivate.toString -> isPrivate
-    )
-    val description = form.description.fold(Map.empty)(description =>
-      Map(EditVcsRepositoryForm.fieldDescription.toString -> description.toString)
-    )
-    val website =
-      form.website.fold(Map.empty)(website => Map(EditVcsRepositoryForm.fieldWebsite.toString -> website.toString))
-    formData ++ description ++ website
+    /** Convert the form class into a stringified map which is used as underlying data type for form handling in the
+      * twirl templating library.
+      *
+      * @return
+      *   A stringified map containing the data of the form.
+      */
+    def toMap: Map[String, String] = {
+      val isPrivate =
+        if (form.isPrivate)
+          "true"
+        else
+          "false"
+      val formData = Map(
+        EditVcsRepositoryForm.fieldName.toString      -> form.name.toString,
+        EditVcsRepositoryForm.fieldIsPrivate.toString -> isPrivate
+      )
+      val description = form.description.fold(Map.empty)(description =>
+        Map(EditVcsRepositoryForm.fieldDescription.toString -> description.toString)
+      )
+      val website =
+        form.website.fold(Map.empty)(website => Map(EditVcsRepositoryForm.fieldWebsite.toString -> website.toString))
+      formData ++ description ++ website
+    }
   }
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/types.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/types.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/types.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/types.scala	2025-01-31 19:52:03.057281931 +0000
@@ -150,7 +150,7 @@
     */
   def randomUserId: UserId = UUID.randomUUID
 
-}
-extension (uid: UserId) {
-  def toUUID: UUID = uid
+  extension (uid: UserId) {
+    def toUUID: UUID = uid
+  }
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/ssh/PublicSshKey.scala new-smederee/modules/hub/src/main/scala/de/smederee/ssh/PublicSshKey.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/ssh/PublicSshKey.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/ssh/PublicSshKey.scala	2025-01-31 19:52:03.057281931 +0000
@@ -66,16 +66,17 @@
       case None           => throw new IllegalArgumentException(s"Illegal format for EncodedKeyBytes: $source")
       case Some(keyBytes) => keyBytes
     }
-}
 
-extension (keyBytes: EncodedKeyBytes) {
+  extension (keyBytes: EncodedKeyBytes) {
+
+    /** Convert the key bytes into an array of bytes by decoding the base64 string.
+      *
+      * @return
+      *   The decoded bytes from the actual key bytes that were base64 encoded.
+      */
+    def toByteArray: Array[Byte] = Base64.getDecoder().decode(keyBytes)
+  }
 
-  /** Convert the key bytes into an array of bytes by decoding the base64 string.
-    *
-    * @return
-    *   The decoded bytes from the actual key bytes that were base64 encoded.
-    */
-  def toByteArray: Array[Byte] = Base64.getDecoder().decode(keyBytes)
 }
 
 opaque type KeyComment = String
@@ -267,29 +268,29 @@
       PublicSshKey(id, ownerId, keyType, base64Key, fingerprint, comment, createdAt, None)
     }
   }
-}
-
-extension (sshKey: PublicSshKey) {
 
-  /** Convert this key into an instance of a [[SshPublicKeyStringhPublicKeyString]].
-    *
-    * @return
-    *   A well formatted ssh public key string.
-    */
-  def toSshPublicKeyString: SshPublicKeyString =
-    SshPublicKeyString(
-      s"""${sshKey.keyType.identifier} ${sshKey.keyBytes.toString}${sshKey.comment
-          .map(c => s" ${c.toString}")
-          .getOrElse("")}"""
-    )
+  extension (sshKey: PublicSshKey) {
 
-  /** Convert this key into an instance of a AuthorizedKeyEntry useable by the Apache Mina SSHD library.
-    *
-    * @return
-    *   Either the converted AuthorizedKeyEntry or an error message.
-    */
-  def toAuthorizedKeyEntry: Either[String, AuthorizedKeyEntry] =
-    Try {
-      AuthorizedKeyEntry.parseAuthorizedKeyEntry(sshKey.toSshPublicKeyString.toString)
-    }.toEither.leftMap(_.getMessage())
+    /** Convert this key into an instance of a [[SshPublicKeyStringhPublicKeyString]].
+      *
+      * @return
+      *   A well formatted ssh public key string.
+      */
+    def toSshPublicKeyString: SshPublicKeyString =
+      SshPublicKeyString(
+        s"""${sshKey.keyType.identifier} ${sshKey.keyBytes.toString}${sshKey.comment
+            .map(c => s" ${c.toString}")
+            .getOrElse("")}"""
+      )
+
+    /** Convert this key into an instance of a AuthorizedKeyEntry useable by the Apache Mina SSHD library.
+      *
+      * @return
+      *   Either the converted AuthorizedKeyEntry or an error message.
+      */
+    def toAuthorizedKeyEntry: Either[String, AuthorizedKeyEntry] =
+      Try {
+        AuthorizedKeyEntry.parseAuthorizedKeyEntry(sshKey.toSshPublicKeyString.toString)
+      }.toEither.leftMap(_.getMessage())
+  }
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/ssh/SshServer.scala new-smederee/modules/hub/src/main/scala/de/smederee/ssh/SshServer.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/ssh/SshServer.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/ssh/SshServer.scala	2025-01-31 19:52:03.057281931 +0000
@@ -62,17 +62,18 @@
     *   An option to the successfully converted SshUsername.
     */
   def from(source: String): Option[SshUsername] = Option(source).filter(string => Format.matches(string))
-}
 
-extension (sshUsername: SshUsername) {
+  extension (sshUsername: SshUsername) {
+
+    /** Convert to a [[Username]] instance. The format of both is identical so we just return the wrapped ssh username
+      * string.
+      *
+      * @return
+      *   A proper username derived from the given ssh username.
+      */
+    def toUsername: Username = Username(sshUsername)
+  }
 
-  /** Convert to a [[Username]] instance. The format of both is identical so we just return the wrapped ssh username
-    * string.
-    *
-    * @return
-    *   A proper username derived from the given ssh username.
-    */
-  def toUsername: Username = Username(sshUsername)
 }
 
 /** Configuration for the SSH server.
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelForm.scala new-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelForm.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelForm.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelForm.scala	2025-01-31 19:52:03.057281931 +0000
@@ -41,23 +41,6 @@
     colour: ColourCode
 )
 
-extension (form: LabelForm) {
-
-  /** Convert the form class into a stringified map which is used as underlying data type for form handling in the twirl
-    * templating library.
-    *
-    * @return
-    *   A stringified map containing the data of the form.
-    */
-  def toMap: Map[String, String] =
-    Map(
-      LabelForm.fieldId.toString          -> form.id.map(_.toString).getOrElse(""),
-      LabelForm.fieldName.toString        -> form.name.toString,
-      LabelForm.fieldDescription.toString -> form.description.map(_.toString).getOrElse(""),
-      LabelForm.fieldColour.toString      -> form.colour.toString
-    )
-}
-
 object LabelForm extends FormValidator[LabelForm] {
   val fieldColour: FormField      = FormField("colour")
   val fieldDescription: FormField = FormField("description")
@@ -110,4 +93,21 @@
     }
   }
 
+  extension (form: LabelForm) {
+
+    /** Convert the form class into a stringified map which is used as underlying data type for form handling in the
+      * twirl templating library.
+      *
+      * @return
+      *   A stringified map containing the data of the form.
+      */
+    def toMap: Map[String, String] =
+      Map(
+        LabelForm.fieldId.toString          -> form.id.map(_.toString).getOrElse(""),
+        LabelForm.fieldName.toString        -> form.name.toString,
+        LabelForm.fieldDescription.toString -> form.description.map(_.toString).getOrElse(""),
+        LabelForm.fieldColour.toString      -> form.colour.toString
+      )
+  }
+
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelRoutes.scala new-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelRoutes.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelRoutes.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/tickets/LabelRoutes.scala	2025-01-31 19:52:03.057281931 +0000
@@ -26,7 +26,6 @@
 import de.smederee.hub._
 import de.smederee.hub.config._
 import de.smederee.hub.forms.types._
-import de.smederee.tickets.toMap // TODO Maybe convert the extions methods into a proper type class?
 import org.http4s._
 import org.http4s.dsl.Http4sDsl
 import org.http4s.dsl.impl._
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/tickets/Label.scala new-smederee/modules/hub/src/main/scala/de/smederee/tickets/Label.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/tickets/Label.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/tickets/Label.scala	2025-01-31 19:52:03.057281931 +0000
@@ -55,10 +55,10 @@
     */
   def fromString(source: String): Option[LabelId] = Option(source).filter(Format.matches).map(_.toLong).flatMap(from)
 
-}
+  extension (id: LabelId) {
+    def toLong: Long = id
+  }
 
-extension (id: LabelId) {
-  def toLong: Long = id
 }
 
 /** Extractor to retrieve an LabelId from a path parameter.
diff -rN -u old-smederee/modules/security/src/main/scala/de/smederee/security/PrivateKey.scala new-smederee/modules/security/src/main/scala/de/smederee/security/PrivateKey.scala
--- old-smederee/modules/security/src/main/scala/de/smederee/security/PrivateKey.scala	2025-01-31 19:52:03.053281924 +0000
+++ new-smederee/modules/security/src/main/scala/de/smederee/security/PrivateKey.scala	2025-01-31 19:52:03.057281931 +0000
@@ -44,15 +44,15 @@
       None
     else
       Option(source)
-}
 
-extension (key: PrivateKey) {
+  extension (key: PrivateKey) {
 
-  /** Return the corresponding array of bytes for the private key.
-    *
-    * @return
-    *   An array of bytes representing the private key.
-    */
-  def toArray: Array[Byte] = key
+    /** Return the corresponding array of bytes for the private key.
+      *
+      * @return
+      *   An array of bytes representing the private key.
+      */
+    def toArray: Array[Byte] = key
 
+  }
 }