Repository: 7mind/izumi
Pull request #1167 (2020-08-02 03:12:05)

logstage-rendering-circe: make JSON key overridable
Discussion at https://github.com/7mind/izumi/issues/1001#issuecomment-660983314


Patch:
diff --git a/logstage/logstage-rendering-circe/src/main/scala/izumi/logstage/api/rendering/json/LogstageCirceRenderingPolicy.scala b/logstage/logstage-rendering-circe/src/main/scala/izumi/logstage/api/rendering/json/LogstageCirceRenderingPolicy.scala
index 8dec2e1e9..674db3f61 100644
--- a/logstage/logstage-rendering-circe/src/main/scala/izumi/logstage/api/rendering/json/LogstageCirceRenderingPolicy.scala
+++ b/logstage/logstage-rendering-circe/src/main/scala/izumi/logstage/api/rendering/json/LogstageCirceRenderingPolicy.scala
@@ -17,6 +17,11 @@ class LogstageCirceRenderingPolicy(
 
   import LogstageCirceRenderingPolicy._
 
+  protected def EventKey = "event"
+  protected def ContextKey = "context"
+  protected def MetaKey = "meta"
+  protected def TextKey = "text"
+
   override def render(entry: Log.Entry): String = {
     val result = mutable.ArrayBuffer[(String, Json)]()
 
@@ -28,7 +33,7 @@ class LogstageCirceRenderingPolicy(
     )
 
     if (params.nonEmpty) {
-      result += "event" -> params.asJson
+      result += EventKey -> params.asJson
     }
 
     val ctx = parametersToJson[LogArg](
@@ -38,7 +43,7 @@ class LogstageCirceRenderingPolicy(
     )
 
     if (ctx.nonEmpty) {
-      result += "context" -> ctx.asJson
+      result += ContextKey -> ctx.asJson
     }
 
     result ++= makeEventEnvelope(entry, formatted)
@@ -78,8 +83,8 @@ class LogstageCirceRenderingPolicy(
     )
 
     val tail = Seq(
-      "meta" -> eventInfo,
-      "text" -> Json.fromFields(
+      MetaKey -> eventInfo,
+      TextKey -> Json.fromFields(
         Seq(
           "template" -> Json.fromString(formatted.template),
           "message" -> Json.fromString(formatted.message),


Test patch:
diff --git a/distage/distage-core/src/test/scala/izumi/distage/dsl/DSLTest.scala b/distage/distage-core/src/test/scala/izumi/distage/dsl/DSLTest.scala
index 5cf4c5519..aa42277cd 100644
--- a/distage/distage-core/src/test/scala/izumi/distage/dsl/DSLTest.scala
+++ b/distage/distage-core/src/test/scala/izumi/distage/dsl/DSLTest.scala
@@ -6,6 +6,7 @@ import izumi.distage.fixtures.BasicCases._
 import izumi.distage.fixtures.SetCases._
 import izumi.distage.injector.MkInjector
 import izumi.distage.model.definition.Binding.{SetElementBinding, SingletonBinding}
+import izumi.distage.model.definition.StandardAxis.{Env, Repo}
 import izumi.distage.model.definition.{Binding, BindingTag, Bindings, ImplDef, Module}
 import izumi.fundamentals.platform.functional.Identity
 import izumi.fundamentals.platform.language.SourceFilePosition
@@ -634,6 +635,19 @@ class DSLTest extends AnyWordSpec with MkInjector {
       assert(res2.getMessage contains "`trait DIResource`")
     }
 
+    "define multiple bindings with different axis but the same implementation" in {
+      class X
+
+      val module = new ModuleDef {
+        make[X].tagged(Repo.Prod, Env.Prod)
+        make[X].tagged(Repo.Dummy, Env.Prod)
+        make[X].tagged(Env.Test)
+      }
+      val bindings = (module ++ module).overridenBy(module).bindings
+      assert(bindings.size == 3)
+      assert(bindings.map(_.tags) == Set(Set[BindingTag](Repo.Prod, Env.Prod), Set[BindingTag](Repo.Dummy, Env.Prod), Set[BindingTag](Env.Test)))
+    }
+
   }
 
 }
diff --git a/distage/distage-core/src/test/scala/izumi/distage/injector/AxisTest.scala b/distage/distage-core/src/test/scala/izumi/distage/injector/AxisTest.scala
index e592293a5..d734a7dad 100644
--- a/distage/distage-core/src/test/scala/izumi/distage/injector/AxisTest.scala
+++ b/distage/distage-core/src/test/scala/izumi/distage/injector/AxisTest.scala
@@ -6,6 +6,7 @@ import izumi.distage.model.PlannerInput
 import izumi.distage.model.definition.StandardAxis.Repo
 import izumi.distage.model.definition.{Activation, BootstrapModuleDef, ModuleDef}
 import izumi.distage.model.plan.Roots
+import org.scalatest.exceptions.TestFailedException
 import org.scalatest.wordspec.AnyWordSpec
 
 class AxisTest extends AnyWordSpec with MkInjector {
@@ -60,12 +61,14 @@ class AxisTest extends AnyWordSpec with MkInjector {
       make[JustTrait].tagged(Repo.Dummy).from[Impl0]
     }
 
-    val context = Injector().produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots.Everything)).unsafeGet()
+    val context = Injector()
+      .produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots.Everything))
+      .unsafeGet()
 
     assert(context.find[JustTrait].isEmpty)
   }
 
-  "report conflict if there's both an implementation with no axis and the current choice" in {
+  "progression test: should report conflict if there's both an implementation with no axis and the current choice" in {
     import BasicCase1._
 
     val definition = new ModuleDef {
@@ -73,10 +76,18 @@ class AxisTest extends AnyWordSpec with MkInjector {
       make[JustTrait].tagged(Repo.Prod).from[Impl1]
     }
 
-    assert(Injector().produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots(DIKey.get[JustTrait]))).unsafeGet().get[JustTrait].isInstanceOf[Impl1])
+    assertThrows[TestFailedException] {
+      assertThrows[Throwable] {
+        val instance = Injector()
+          .produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots(DIKey.get[JustTrait])))
+          .unsafeGet()
+          .get[JustTrait]
+        assert(instance.isInstanceOf[Impl1]) // should fail to produce, not choose `Repo.Prod`
+      }
+    }
   }
 
-  "report conflict if there's both an implementation with no axis and the opposite choice" in {
+  "progression test: should report conflict if there's both an implementation with no axis and the opposite choice" in {
     import BasicCase1._
 
     val definition = new ModuleDef {
@@ -84,7 +95,15 @@ class AxisTest extends AnyWordSpec with MkInjector {
       make[JustTrait].tagged(Repo.Dummy).from[Impl1]
     }
 
-    assert(Injector().produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots(DIKey.get[JustTrait]))).unsafeGet().get[JustTrait].isInstanceOf[Impl0])
+    assertThrows[TestFailedException] {
+      assertThrows[Throwable] {
+        val instance = Injector()
+          .produce(PlannerInput(definition, Activation(Repo -> Repo.Prod), Roots(DIKey.get[JustTrait])))
+          .unsafeGet()
+          .get[JustTrait]
+        assert(instance.isInstanceOf[Impl0]) // should fail to produce, not choose untagged
+      }
+    }
   }
 
 }