Skip to content

Commit

Permalink
Moving to Top level (#303)
Browse files Browse the repository at this point in the history
* Moving everything to top level

* Changing to neotype

* Changing to neotype

* Changing to neotype
  • Loading branch information
michelchan authored Oct 9, 2024
1 parent c43f442 commit f44048e
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 591 deletions.
13 changes: 0 additions & 13 deletions morphir/src/org/finos/morphir/ir/gen1/Names.scala

This file was deleted.

11 changes: 4 additions & 7 deletions morphir/src/org/finos/morphir/ir/gen1/NamingOptionsModule.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.finos.morphir.ir.gen1

trait NamingOptionsModule { self: PackageNameModule with ModuleNameModule =>
sealed case class FQNamingOptions(defaultPackage: PackageName, defaultModule: ModuleName, defaultSeparator: String)

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

object FQNamingOptions {
implicit val default: FQNamingOptions =
FQNamingOptions(PackageName.empty, ModuleName.empty, ":")
}
object FQNamingOptions {
implicit val default: FQNamingOptions =
FQNamingOptions(PackageName.empty, ModuleName.empty, ":")
}
2 changes: 1 addition & 1 deletion morphir/src/org/finos/morphir/ir/gen1/Type.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.finos.morphir.ir.gen1

import org.finos.morphir.ir.gen1.naming.*
import org.finos.morphir.ir.gen1.*
import org.finos.morphir.ir.gen1.Type.{Unit as UnitType, *}

import scala.annotation.{tailrec, unused}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.finos.morphir.ir.gen1

import org.finos.morphir.ir.gen1.naming.*
import org.finos.morphir.ir.gen1.FQName
import org.finos.morphir.ir.gen1.Type.{Unit as UnitType, *}

final case class TypeMapReferenceName[Attrib](f: FQName => FQName) extends TypeRewritingFolder[Any, Attrib] {
Expand Down
163 changes: 78 additions & 85 deletions morphir/src/org/finos/morphir/ir/gen1/fqname.scala
Original file line number Diff line number Diff line change
@@ -1,106 +1,99 @@
package org.finos.morphir.ir.gen1

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

sealed case class FQName(packagePath: PackageName, modulePath: ModuleName, localName: Name) { self =>
def getPackagePath: Path = packagePath.toPath

def getModulePath: Path = modulePath.toPath
def getModuleName: ModuleName = modulePath

/// An alias for `packagePath`
def pack: PackageName = packagePath

def toReferenceName: String = Seq(
Path.toString(Name.toTitleCase, ".", packagePath.toPath),
Path.toString(Name.toTitleCase, ".", modulePath.toPath),
localName.toTitleCase
).mkString(".")

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

def toStringTitleCase: String = Array(
Path.toString(Name.toTitleCase, ".", packagePath.toPath),
Path.toString(Name.toTitleCase, ".", modulePath.toPath),
Name.toTitleCase(this.localName)
).mkString(":")
}
sealed case class FQName(packagePath: PackageName, modulePath: ModuleName, localName: Name) { self =>
def getPackagePath: Path = packagePath.toPath

def getModulePath: Path = modulePath.toPath
def getModuleName: ModuleName = modulePath

/// An alias for `packagePath`
def pack: PackageName = packagePath

def toReferenceName: String = Seq(
Path.toString(Name.toTitleCase, ".", packagePath.toPath),
Path.toString(Name.toTitleCase, ".", modulePath.toPath),
localName.toTitleCase
).mkString(".")

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

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

object FQName {
val empty: FQName = FQName(PackageName.empty, ModuleName.empty, Name.empty)
// def apply(packagePath: Path, modulePath: Path, localName: Name): FQName =
// FQName(PackageName(packagePath), ModulePath(modulePath), localName)
object FQName {
val empty: FQName = FQName(PackageName.empty, ModuleName.empty, Name.empty)
// def apply(packagePath: Path, modulePath: Path, localName: Name): FQName =
// FQName(PackageName(packagePath), ModulePath(modulePath), localName)

val fqName: Path => Path => Name => FQName = packagePath =>
modulePath => localName => FQName(PackageName.fromPath(packagePath), ModuleName(modulePath), localName)
val fqName: Path => Path => Name => FQName = packagePath =>
modulePath => localName => FQName(PackageName.fromPath(packagePath), ModuleName(modulePath), localName)

def fromQName(packagePath: Path, qName: QName): FQName =
FQName(PackageName.fromPath(packagePath), ModuleName(qName.modulePath), qName.localName)
def fromQName(packagePath: Path, qName: QName): FQName =
FQName(PackageName.fromPath(packagePath), ModuleName(qName.modulePath), qName.localName)

def fromQName(qName: QName)(implicit packageName: PackageName): FQName =
FQName(packageName, ModuleName(qName.modulePath), qName.localName)
def fromQName(qName: QName)(implicit packageName: PackageName): FQName =
FQName(packageName, ModuleName(qName.modulePath), qName.localName)

def fromQName(qName: QName)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, ModuleName(QName.getModulePath(qName)), QName.getLocalName(qName))
def fromQName(qName: QName)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, ModuleName(QName.getModulePath(qName)), QName.getLocalName(qName))

/** Get the package path part of a fully-qualified name. */
def getPackagePath(fqName: FQName): Path = fqName.getPackagePath
/** Get the package path part of a fully-qualified name. */
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 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
/** 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(PackageName.fromString(packageName), ModuleName.fromString(moduleName), Name.fromString(localName))
/** Convenience function to create a fully-qualified name from 3 strings */
def fqn(packageName: String, moduleName: String, localName: String): FQName =
FQName(PackageName.fromString(packageName), ModuleName.fromString(moduleName), Name.fromString(localName))

/** Convenience function to create a fully-qualified name from 2 strings with default package name */
def fqn(moduleName: String, localName: String)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, ModuleName(Path.fromString(moduleName)), Name.fromString(localName))
/** Convenience function to create a fully-qualified name from 2 strings with default package name */
def fqn(moduleName: String, localName: String)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, ModuleName(Path.fromString(moduleName)), Name.fromString(localName))

/** Convenience function to create a fully-qualified name from 1 string with defaults for package and module */
def fqn(localName: String)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, options.defaultModule, Name.fromString(localName))
/** Convenience function to create a fully-qualified name from 1 string with defaults for package and module */
def fqn(localName: String)(implicit options: FQNamingOptions): FQName =
FQName(options.defaultPackage, options.defaultModule, Name.fromString(localName))

/// Convenience function to create a fully-qualified name from a local name and an implicitly provided `QualifiedModuleName`.
def fromLocalName(localName: String)(implicit qualifiedModuleName: QualifiedModuleName): FQName =
FQName(qualifiedModuleName.packageName, qualifiedModuleName.modulePath, Name.fromString(localName))
/// Convenience function to create a fully-qualified name from a local name and an implicitly provided `QualifiedModuleName`.
def fromLocalName(localName: String)(implicit qualifiedModuleName: QualifiedModuleName): FQName =
FQName(qualifiedModuleName.packageName, qualifiedModuleName.modulePath, Name.fromString(localName))

def fromLocalName(localName: Name)(implicit qualifiedModuleName: QualifiedModuleName): FQName =
FQName(qualifiedModuleName.packageName, qualifiedModuleName.modulePath, localName)
def fromLocalName(localName: Name)(implicit qualifiedModuleName: QualifiedModuleName): FQName =
FQName(qualifiedModuleName.packageName, qualifiedModuleName.modulePath, localName)

def toString(fqName: FQName): String = fqName.toString
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)(implicit options: FQNamingOptions): FQName =
fqNameString.split(splitter) match {
case Array(packageNameString, moduleNameString, localNameString) =>
fqn(packageNameString, moduleNameString, localNameString)
case Array(moduleNameString, localNameString) =>
fqn(moduleNameString, localNameString)
case Array(localNameString) =>
fqn(localNameString)
case _ => throw FQNameParsingError(fqNameString)
}
/** Parse a string into a FQName using splitter as the separator between package, module, and local names */
def fromString(fqNameString: String, splitter: String)(implicit options: FQNamingOptions): FQName =
fqNameString.split(splitter) match {
case Array(packageNameString, moduleNameString, localNameString) =>
fqn(packageNameString, moduleNameString, localNameString)
case Array(moduleNameString, localNameString) =>
fqn(moduleNameString, localNameString)
case Array(localNameString) =>
fqn(localNameString)
case _ => throw FQNameParsingError(fqNameString)
}

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

object ReferenceName {
def unapply(fqName: FQName): Some[String] = Some(fqName.toReferenceName)
}
object ReferenceName {
def unapply(fqName: FQName): Some[String] = Some(fqName.toReferenceName)
}

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

sealed case class FQNameParsingError(invalidName: String)
extends Exception(s"Unable to parse: [$invalidName] into a valid FQName")
53 changes: 26 additions & 27 deletions morphir/src/org/finos/morphir/ir/gen1/moduleName.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package org.finos.morphir.ir.gen1

trait ModuleNameModule { self: NameModule with PathModule =>
import neotype.*

/** A module name is a unique identifier for a module within a package. It is represented by a `Path`, which is a
* "list" of names.
*/
sealed case class ModuleName(path: Path) { self =>
/** A module name is a unique identifier for a module within a package. It is represented by a `Path`, which is a "list"
* of names.
*/
type ModuleName = ModuleName.Type

object ModuleName extends Newtype[Path] {
/// Create an empty module name.
val empty: ModuleName = ModuleName(Path.empty)

def apply(input: String): ModuleName = fromString(input)

def unapply(input: ModuleName): Option[Path] = Some(input.path)

def fromString(input: String): ModuleName = ModuleName(Path.fromString(input))

def apply(parts: Name*): ModuleName = ModuleName(Path.fromIterable(parts))
def fromIterable(segments: Iterable[Name]): ModuleName = ModuleName(Path.fromIterable(segments))
def fromStrings(parts: String*): ModuleName = ModuleName(Path.fromStrings(parts: _*))

extension (self: ModuleName)
def path: Path = self.unwrap

/// Construct a new module name by concatting the given module name to this one.
def ++(other: ModuleName): ModuleName = ModuleName(path ++ other.path)

/// Construct a new module name by concatting the given module path to this one.
def ++(other: Path): ModuleName = ModuleName(path ++ other)

/// Construct a new module name by concatting the given local name to this module name.
def /(name: Name): ModuleName = ModuleName(path / name)
/// Construct a new module name by concatting the given local name to this module name.
Expand All @@ -28,34 +42,19 @@ trait ModuleNameModule { self: NameModule with PathModule =>
/// Get the name of this module.
/// For example if the module name is `Morphir.SDK.Basics` then the name is `Basics`.
def name: Name =
self match {
path match {
case ModuleName(Path(Vector())) => Name.empty
case ModuleName(Path(segments)) => segments.last
}

// Get the name of this module if a name is present.
def nameOption: Option[Name] =
self match {
path match {
case ModuleName(Path(Vector())) => None
case ModuleName(Path(segments)) => Some(segments.last)
}

/// Convert this module name to a `Path`.
@inline def toPath: Path = path
override def toString: String = path.toString
}

object ModuleName {
/// Create an empty module name.
val empty: ModuleName = ModuleName(Path.empty)

def apply(input: String): ModuleName = fromString(input)

def fromString(input: String): ModuleName = ModuleName(Path.fromString(input))

def apply(parts: Name*): ModuleName = ModuleName(Path.fromIterable(parts))
def fromIterable(segments: Iterable[Name]): ModuleName = ModuleName(Path.fromIterable(segments))
def fromStrings(parts: String*): ModuleName = ModuleName(Path.fromStrings(parts: _*))
}

@inline def toPath: Path = path
def render: String = path.toString
}
Loading

0 comments on commit f44048e

Please sign in to comment.