Skip to content

Commit

Permalink
adding FQName (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelchan authored Feb 10, 2022
1 parent 71009c2 commit 067682b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
39 changes: 37 additions & 2 deletions morphir-ir/shared/src/main/scala/zio/morphir/ir/FQName.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package zio.morphir.ir

final case class FQName(packagePath: PackageName, modulePath: ModulePath, localName: Name)
final case class FQName(packagePath: PackageName, modulePath: ModulePath, localName: Name) {
def getPackagePath: Path = packagePath.toPath
def getModulePath: Path = modulePath.toPath

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

object FQName {
def apply(packagePath: Path, modulePath: Path, localName: Name): FQName =
Expand All @@ -9,6 +18,32 @@ object FQName {
val fqName: Path => Path => Name => FQName = packagePath =>
modulePath => localName => FQName(PackageName(packagePath), ModulePath(modulePath), localName)

def fromQName(packagePath: Path, qName: QName): FQName =
FQName(packagePath, QName.getModulePath(qName), QName.getLocalName(qName))

/** Get the package path part of a fully-qualified name. */
def getPackagePath(fqName: FQName): Path = fqName.packagePath.toPath
def getPackagePath(fqName: FQName): Path = fqName.getPackagePath

/** Get the module path part of a fully-qualified name */
def getModulePath(fqName: FQName): Path = fqName.getModulePath

/** Get the local name part of a fully-qualified name */
def getLocalName(fqName: FQName): Name = fqName.localName

/** Convenience function to create a fully-qualified name from 3 strings */
def fqn(packageName: String, moduleName: String, localName: String): FQName = {
FQName(Path.fromString(packageName), Path.fromString(moduleName), Name.fromString(localName))
}

def toString(fqName: FQName): String = fqName.toString

/** Parse a string into a FQName using splitter as the separator between package, module, and local names */
def fromString(fqNameString: String, splitter: String): FQName = {
fqNameString.split(splitter) match {
case Array(moduleNameString, packageNameString, localNameString) =>
fqn(moduleNameString, packageNameString, localNameString)
case _ => FQName(Path.empty, Path.empty, Name.empty)
}
}

}
53 changes: 53 additions & 0 deletions morphir-ir/shared/src/test/scala/zio/morphir/ir/FQNameTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package zio.morphir.ir

import zio.morphir.ir.testing.MorphirBaseSpec
import zio.test.*

object FQNameTest extends MorphirBaseSpec {
def spec = suite("FQName")(
suite("Create a FQName:")(
test("By using a string") {
assertTrue(
FQName.fromString("moduleName/packageName/localName", "/") ==
FQName(
PackageName(Path.fromString("moduleName")),
ModulePath(Path.fromString("packageName")),
Name.fromString("localName")
)
)
},
test("By using a QName") {
val path = Path.fromString("package Name")
val qName = QName(Path.fromString("qualified.Name.Path"), Name.fromString("localName"))
assertTrue(FQName.fromQName(path, qName) == FQName(path, qName.modulePath, qName.localName))
}
),
suite("Retrieving variables should work")(
test("Get PackagePath") {
val fqName = FQName.fromString("moduleName/packageName/localName", "/")
assertTrue(FQName.getPackagePath(fqName) == Path.fromString("moduleName"))
},
test("Get ModulePath") {
val fqName = FQName.fromString("moduleName/packageName/localName", "/")
assertTrue(FQName.getModulePath(fqName) == Path.fromString("packageName"))
},
test("Get localName") {
val fqName = FQName.fromString("moduleName/packageName/localName", "/")
assertTrue(FQName.getLocalName(fqName) == Name.fromString("localName"))
}
),
suite("Creating a string from FQName") {
test("should work") {
assertTrue(
FQName.toString(
FQName(
PackageName(Path.fromString("com.example")),
ModulePath(Path.fromString("java home")),
Name.fromString("morphir")
)
) == "Com.Example:JavaHome:morphir"
)
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ object PathSpec extends MorphirBaseSpec {
test("It can be constructed from a simple string") {
assertTrue(Path.fromString("Person") == Path(Chunk(Name.fromString("person"))))
},
test("It can be constructed from a long string") {
assertTrue(
Path.fromString("She Sells Seashells") == Path(
Chunk(
Name.fromList(List("she", "sells", "seashells"))
)
)
)
},
test("It can be constructed when given a dotted string") {
assertTrue(
Path.fromString("blog.Author") == Path(
Expand Down

0 comments on commit 067682b

Please sign in to comment.