Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Printing and fromString to QualifiedModuleName #343

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions morphir/extensibility/src/org/finos/morphir/Names.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ trait Names
with PackageNameModule
with QualifiedModuleNameModule
with QNameModule
with NamingOptionsModule
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.finos.morphir

trait NamingOptionsModule { self: PackageNameModule with ModuleNameModule =>

sealed case class FQNamingOptions(defaultPackage: PackageName, defaultModule: ModuleName, defaultSeparator: String)

object FQNamingOptions {
implicit val default: FQNamingOptions =
FQNamingOptions(PackageName.empty, ModuleName.empty, ":")
}
}
10 changes: 2 additions & 8 deletions morphir/extensibility/src/org/finos/morphir/fqname.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package org.finos.morphir
trait FQNameModule {
self: NameModule with ModuleNameModule with NamespaceModule with PackageNameModule with PathModule
with QualifiedModuleNameModule
with QNameModule =>
with QNameModule
with NamingOptionsModule =>

sealed case class FQName(packagePath: PackageName, modulePath: ModuleName, localName: Name) { self =>
def getPackagePath: Path = packagePath.toPath
Expand Down Expand Up @@ -100,13 +101,6 @@ trait FQNameModule {
}
}

sealed case class FQNamingOptions(defaultPackage: PackageName, defaultModule: ModuleName, defaultSeparator: String)

object FQNamingOptions {
implicit val default: FQNamingOptions =
FQNamingOptions(PackageName.empty, ModuleName.empty, ":")
}

sealed case class FQNameParsingError(invalidName: String)
extends Exception(s"Unable to parse: [$invalidName] into a valid FQName")
}
3 changes: 2 additions & 1 deletion morphir/extensibility/src/org/finos/morphir/nodeID.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.finos.morphir

trait NodeIDModule {
self: FQNameModule with NameModule with PathModule with PackageNameModule with ModuleNameModule
self: FQNameModule with NamingOptionsModule with NameModule with PathModule with PackageNameModule
with ModuleNameModule
with QualifiedModuleNameModule =>
import NodePath.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.finos.morphir

trait QualifiedModuleNameModule {
self: PackageNameModule with PathModule with ModuleNameModule with NamespaceModule =>
self: NameModule with PackageNameModule with PathModule with ModuleNameModule with NamespaceModule
with NamingOptionsModule =>

/// A qualified module name is a globally unique identifier for a module. It is represented by the combination of a package name and the module name.
sealed case class QualifiedModuleName(packageName: PackageName, modulePath: ModuleName) { self =>
Expand All @@ -11,6 +12,11 @@ trait QualifiedModuleNameModule {
QualifiedModuleName(self.packageName, modulePath.addPart(namespaceAddition))

def toTuple: (Path, Path) = (packageName.toPath, modulePath.toPath)

override def toString: String = Array(
Path.toString(Name.toTitleCase, ".", packageName.toPath),
Path.toString(Name.toTitleCase, ".", modulePath.toPath)
).mkString(":")
}

object QualifiedModuleName {
Expand All @@ -22,9 +28,33 @@ trait QualifiedModuleNameModule {
def apply(modulePath: String)(implicit packageName: PackageName): QualifiedModuleName =
QualifiedModuleName(packageName, ModuleName.fromString(modulePath))

/** Parse a string into a QualifedModuleName using splitter as the separator between package, and module */
def fromString(nameString: String, splitter: String)(implicit options: FQNamingOptions): QualifiedModuleName =
nameString.split(splitter) match {
case Array(moduleNameString, localNameString) =>
qmn(moduleNameString, localNameString)
case Array(localNameString) =>
qmn(localNameString)
case _ => throw QualifiedModuleNameParsingError(nameString)
}

def fromString(fqNameString: String)(implicit options: FQNamingOptions): QualifiedModuleName =
fromString(fqNameString, options.defaultSeparator)

/** Convenience function to create a fully-qualified name from 2 strings with default package name */
def qmn(packageName: String, moduleName: String): QualifiedModuleName =
QualifiedModuleName(PackageName.fromString(packageName), ModuleName(Path.fromString(moduleName)))

/** Convenience function to create a fully-qualified name from 1 string with defaults for package and module */
def qmn(moduleName: String)(implicit options: FQNamingOptions): QualifiedModuleName =
QualifiedModuleName(options.defaultPackage, ModuleName(Path.fromString(moduleName)))

object AsTuple {
def unapply(name: QualifiedModuleName): Option[(Path, Path)] =
Some(name.toTuple)
}
}

sealed case class QualifiedModuleNameParsingError(invalidName: String)
extends Exception(s"Unable to parse: [$invalidName] into a valid QualifiedModuleName")
}