From 067682b5e0918b119b1d36565c7e6c442a9b3af5 Mon Sep 17 00:00:00 2001 From: Michelle Date: Thu, 10 Feb 2022 09:06:24 -0500 Subject: [PATCH] adding FQName (#18) --- .../main/scala/zio/morphir/ir/FQName.scala | 39 +++++++++++++- .../scala/zio/morphir/ir/FQNameTest.scala | 53 +++++++++++++++++++ .../test/scala/zio/morphir/ir/PathSpec.scala | 9 ++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 morphir-ir/shared/src/test/scala/zio/morphir/ir/FQNameTest.scala diff --git a/morphir-ir/shared/src/main/scala/zio/morphir/ir/FQName.scala b/morphir-ir/shared/src/main/scala/zio/morphir/ir/FQName.scala index a67f0cca..20e01c9a 100644 --- a/morphir-ir/shared/src/main/scala/zio/morphir/ir/FQName.scala +++ b/morphir-ir/shared/src/main/scala/zio/morphir/ir/FQName.scala @@ -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 = @@ -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) + } + } + } diff --git a/morphir-ir/shared/src/test/scala/zio/morphir/ir/FQNameTest.scala b/morphir-ir/shared/src/test/scala/zio/morphir/ir/FQNameTest.scala new file mode 100644 index 00000000..221a1a14 --- /dev/null +++ b/morphir-ir/shared/src/test/scala/zio/morphir/ir/FQNameTest.scala @@ -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" + ) + } + } + ) +} diff --git a/morphir-ir/shared/src/test/scala/zio/morphir/ir/PathSpec.scala b/morphir-ir/shared/src/test/scala/zio/morphir/ir/PathSpec.scala index 53bfa66b..51607b9f 100644 --- a/morphir-ir/shared/src/test/scala/zio/morphir/ir/PathSpec.scala +++ b/morphir-ir/shared/src/test/scala/zio/morphir/ir/PathSpec.scala @@ -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(