Skip to content

Commit

Permalink
optional suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Aug 27, 2024
1 parent df00a36 commit 2640ba4
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 196 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/io/septimalmind/baboon/Baboon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ case class Options(
"do not generate usings for System, System.Collections.Generic and System.Linq (see ImplicitUsings)"
)
csExcludeGlobalUsings: Option[Boolean],
omitMostRecentVersionSuffixFromPaths: Option[Boolean],
omitMostRecentVersionSuffixFromNamespaces: Option[Boolean],
)

sealed trait RuntimeGenOpt
Expand Down Expand Up @@ -64,6 +66,8 @@ object Baboon {
rtOpt,
!opts.disableConversions.getOrElse(false),
!opts.csExcludeGlobalUsings.getOrElse(false),
!opts.omitMostRecentVersionSuffixFromPaths.getOrElse(false),
!opts.omitMostRecentVersionSuffixFromNamespaces.getOrElse(false),
)
Injector.NoCycles().produceRun(new BaboonModule(options)) {
(compiler: BaboonCompiler) =>
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/io/septimalmind/baboon/BaboonCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ object BaboonCompiler {
runtime: RuntimeGenOpt,
generateConversions: Boolean,
disregardImplicitUsings: Boolean,
omitMostRecentVersionSuffixFromPaths: Boolean,
omitMostRecentVersionSuffixFromNamespaces: Boolean,
)

class BaboonCompilerImpl(loader: BaboonLoader,
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/io/septimalmind/baboon/BaboonModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class BaboonModule(options: CompilerOptions) extends ModuleDef {
DIKey[Version],
DIKey.get[Domain].named("current"),
DIKey.get[Domain].named("source"),
DIKey.get[BaboonRuleset]
DIKey.get[BaboonRuleset],
DIKey.get[BaboonEvolution],
)
)
.withSubmodule(new ModuleDef {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ class CSBaboonTranslator(
toCurrent: Set[EvolutionStep],
defnOut: List[CSDefnTranslator.OutputExt]
): Out[List[CSDefnTranslator.Output]] = {
val pkg = trans.toCsPkg(domain.id, domain.version)
val pkg = trans.toCsPkg(domain.id, domain.version, lineage.evolution, options)

for {
convs <- lineage.evolution.rules
Expand All @@ -657,6 +657,7 @@ class CSBaboonTranslator(
.provide[Domain]("current")(domain)
.provide[Domain]("source")(lineage.versions(srcVer.from))
.provide(rules)
.provide(lineage.evolution)
.produce()
.use(_.makeConvs())
}
Expand Down Expand Up @@ -710,12 +711,14 @@ class CSBaboonTranslator(

val rt = tools.inNs(pkg.parts.toSeq, runtime)

val basename = tools.basename(domain, lineage.evolution, options)

List(
CSDefnTranslator
.Output(s"${tools.basename(domain)}/Baboon-Runtime.cs", rt, pkg, isTest = false)
.Output(s"$basename/Baboon-Runtime.cs", rt, pkg, isTest = false)
) ++ convs.map { conv =>
CSDefnTranslator
.Output(s"${tools.basename(domain)}/${conv.fname}", conv.conv, pkg, isTest = false)
.Output(s"$basename/${conv.fname}", conv.conv, pkg, isTest = false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.septimalmind.baboon.translator.csharp

import io.septimalmind.baboon.translator.csharp.CSBaboonTranslator.binaryWriter
import io.septimalmind.baboon.BaboonCompiler.CompilerOptions
import io.septimalmind.baboon.typer.model.*
import io.septimalmind.baboon.util.BLogger
import izumi.fundamentals.platform.strings.TextTree
Expand All @@ -12,18 +13,21 @@ trait CSCodecTestsTranslator {
def translate(definition: DomainMember.User,
csRef: CSValue.CSType,
srcRef: CSValue.CSType,
domain: Domain): Option[TextTree[CSValue]]
domain: Domain,
evo: BaboonEvolution): Option[TextTree[CSValue]]
}

object CSCodecTestsTranslator {
final class Impl(codecs: Set[CSCodecTranslator],
typeTranslator: CSTypeTranslator,
logger: BLogger)
extends CSCodecTestsTranslator {
options: CompilerOptions,
logger: BLogger,
) extends CSCodecTestsTranslator {
override def translate(definition: DomainMember.User,
csRef: CSValue.CSType,
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): Option[TextTree[CSValue]] = {
val testClassName =
CSValue.CSType(srcRef.pkg, s"${srcRef.name}_Codec_Test", srcRef.fq)
Expand Down Expand Up @@ -57,10 +61,10 @@ object CSCodecTestsTranslator {
| [OneTimeSetUp]
| public void Setup()
| {
| ${fieldsInitialization(definition, srcRef, domain)}
| ${fieldsInitialization(definition, srcRef, domain, evo)}
| }
|
| ${tests(definition, srcRef, domain)}
| ${tests(definition, srcRef, domain, evo)}
|}
|""".stripMargin
Some(testClass)
Expand All @@ -82,13 +86,16 @@ object CSCodecTestsTranslator {
}
}

private def fieldsInitialization(definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain): TextTree[CSValue] = {
private def fieldsInitialization(
definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution
): TextTree[CSValue] = {
definition.defn match {
case Typedef.Adt(root, members) =>
val adtMembersNamespace = typeTranslator
.toCsPkg(domain.id, domain.version)
.toCsPkg(domain.id, domain.version, evo, options)
.parts
.mkString(".") + s".${root.name.name.toLowerCase}"
members
Expand All @@ -109,21 +116,34 @@ object CSCodecTestsTranslator {
private def tests(definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
codecs
.map {
case jsonCodec: CSNSJsonCodecGenerator =>
q"""[Test]
|public void jsonCodecTest()
|{
| ${jsonCodecAssertions(jsonCodec, definition, srcRef, domain)}
| ${jsonCodecAssertions(
jsonCodec,
definition,
srcRef,
domain,
evo
)}
|}
|""".stripMargin
case uebaCodec: CSUEBACodecGenerator =>
q"""[Test]
|public void uebaCodecTest()
|{
| ${uebaCodecAssertions(uebaCodec, definition, srcRef, domain)}
| ${uebaCodecAssertions(
uebaCodec,
definition,
srcRef,
domain,
evo
)}
|}
|""".stripMargin
case unknown =>
Expand All @@ -141,12 +161,15 @@ object CSCodecTestsTranslator {
private def jsonCodecAssertions(codec: CSNSJsonCodecGenerator,
definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain): TextTree[CSValue] = {
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
definition.defn match {
case Typedef.Adt(root, members) =>
members
.map { member =>
val typeRef = typeTranslator.toCsTypeRefNoDeref(root, domain)
val typeRef =
typeTranslator.toCsTypeRefNoDeref(root, domain, evo, options)
val codecName = codec.codecName(typeRef)
val fieldName = member.name.name.toLowerCase
val serialized = s"${fieldName}_json"
Expand Down Expand Up @@ -175,12 +198,15 @@ object CSCodecTestsTranslator {
private def uebaCodecAssertions(codec: CSUEBACodecGenerator,
definition: DomainMember.User,
srcRef: CSValue.CSType,
domain: Domain): TextTree[CSValue] = {
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue] = {
definition.defn match {
case Typedef.Adt(root, members) =>
members
.map { member =>
val typeRef = typeTranslator.toCsTypeRefNoDeref(root, domain)
val typeRef =
typeTranslator.toCsTypeRefNoDeref(root, domain, evo, options)
val codecName = codec.codecName(typeRef)
val fieldName = member.name.name.toLowerCase
val serialized = s"${fieldName}_bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ trait CSCodecTranslator {
def translate(defn: DomainMember.User,
csRef: CSValue.CSType,
srcRef: CSValue.CSType,
domain: Domain): TextTree[CSValue]
domain: Domain,
evo: BaboonEvolution,
): TextTree[CSValue]

def codecName(name: CSValue.CSType): CSValue.CSType

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
package io.septimalmind.baboon.translator.csharp

import io.septimalmind.baboon.BaboonCompiler.CompilerOptions
import io.septimalmind.baboon.typer.model.*
import izumi.fundamentals.platform.strings.TextTree
import izumi.fundamentals.platform.strings.TextTree.*

trait CSDefnTools {
def inNs(nss: Seq[String], tree: TextTree[CSValue]): TextTree[CSValue]

def basename(dom: Domain): String
def basename(dom: Domain, evolution: BaboonEvolution, options: CompilerOptions): String = {
basename(dom, options.omitMostRecentVersionSuffixFromPaths && evolution.latest == dom.version)
}

def basename(dom: Domain, omitVersion: Boolean): String

def makeMeta(defn: DomainMember.User,
version: Version): Seq[TextTree[CSValue]]
}

object CSDefnTools {
class CSDefnToolsImpl extends CSDefnTools {
def basename(dom: Domain): String = {
(dom.id.path.map(_.capitalize) ++ Seq(dom.version.version))
.mkString("-")
def basename(dom: Domain, omitVersion: Boolean): String = {
val base = dom.id.path.map(_.capitalize)
val segments = if (omitVersion) {
base
} else {
base ++ Seq(dom.version.version)
}

segments.mkString("-")
}

def makeMeta(defn: DomainMember.User,
Expand All @@ -34,7 +45,8 @@ object CSDefnTools {
|""".stripMargin)
}

private def inNs(name: String, tree: TextTree[CSValue]): TextTree[CSValue] = {
private def inNs(name: String,
tree: TextTree[CSValue]): TextTree[CSValue] = {
q"""namespace ${name} {
| ${tree.shift(4).trim}
|}""".stripMargin
Expand All @@ -46,5 +58,6 @@ object CSDefnTools {
inNs(ns, acc)
}
}

}
}
Loading

0 comments on commit 2640ba4

Please sign in to comment.