-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue 527 - Add to StackReference, methods converting to typed Ou…
…tputs (#528) * Fix issue 527 - Add methods to stack reference * Fix issue 527 - Create TypedStackReference * Add all changes necessary for nice typed StackRef api - @lbialy --------- Co-authored-by: Łukasz Biały <[email protected]>
- Loading branch information
Showing
11 changed files
with
288 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/src/main/scala/besom/internal/TypedStackReference.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package besom.internal | ||
|
||
import besom.types.* | ||
|
||
case class TypedStackReference[T]( | ||
urn: Output[URN], | ||
id: Output[ResourceId], | ||
name: Output[String], | ||
outputs: T, | ||
secretOutputNames: Output[Set[String]] | ||
) extends CustomResource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package besom.util | ||
|
||
import besom.json.* | ||
import besom.internal.{Output, Context} | ||
import besom.internal.Constants, Constants.SpecialSig | ||
|
||
object JsonReaderInstances: | ||
implicit def outputJsonReader[A](using jsonReader: JsonReader[A], ctx: Context): JsonReader[Output[A]] = | ||
new JsonReader[Output[A]]: | ||
def read(json: JsValue): Output[A] = json match | ||
case JsObject(fields) => | ||
fields.get(SpecialSig.Key) match | ||
case Some(JsString(sig)) if SpecialSig.fromString(sig) == Some(SpecialSig.OutputSig) => | ||
val maybeInnerValue = fields.get(Constants.ValueName) | ||
maybeInnerValue | ||
.map { innerValue => | ||
try Output(jsonReader.read(innerValue)) | ||
catch case e: Throwable => Output.fail(e) | ||
} | ||
.getOrElse(Output.fail(Exception("Invalid JSON"))) | ||
|
||
case Some(JsString(sig)) if SpecialSig.fromString(sig) == Some(SpecialSig.SecretSig) => | ||
val maybeInnerValue = fields.get(Constants.ValueName) | ||
maybeInnerValue | ||
.map { innerValue => | ||
try Output.secret(jsonReader.read(innerValue)) | ||
catch case e: Throwable => Output.fail(e) | ||
} | ||
.getOrElse(Output.fail(Exception("Invalid JSON"))) | ||
|
||
case _ => Output.fail(Exception("Invalid JSON")) | ||
|
||
case _ => Output.fail(Exception("Invalid JSON")) |
61 changes: 61 additions & 0 deletions
61
core/src/test/scala/besom/internal/StackReferenceTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package besom.internal | ||
|
||
import besom.* | ||
import besom.json.* | ||
import RunResult.{*, given} | ||
|
||
class StackReferenceTest extends munit.FunSuite: | ||
|
||
test("convert stack reference to case class") { | ||
given Context = DummyContext().unsafeRunSync() | ||
case class Test(s: String, i: Int) derives JsonReader | ||
val expected = Test("value1", 2) | ||
val outputs = Map("s" -> JsString("value1"), "i" -> JsNumber(2)) | ||
|
||
val requireObject = StackReference.requireObject[Test](Output(outputs), Output(Set.empty)) | ||
assertEquals(requireObject.getData.unsafeRunSync(), OutputData(expected)) | ||
} | ||
|
||
test("fail when convert stack reference to case class with missing data") { | ||
given Context = DummyContext().unsafeRunSync() | ||
case class Test(s: String, i: Int) derives JsonReader | ||
val outputs = Map("s" -> JsString("value1")) | ||
|
||
val requireObject = StackReference.requireObject[Test](Output(outputs), Output(Set.empty)) | ||
intercept[besom.json.DeserializationException](requireObject.getData.unsafeRunSync()) | ||
} | ||
|
||
test("convert stack reference to case class with secret field") { | ||
given Context = DummyContext().unsafeRunSync() | ||
case class Test(s: String, i: Int) derives JsonReader | ||
val expected = Test("value1", 2) | ||
val outputs = Map("s" -> JsString("value1"), "i" -> JsNumber(2)) | ||
val secretOutputNames = Set("i") | ||
|
||
val requireObject = StackReference.requireObject[Test](Output(outputs), Output(secretOutputNames)) | ||
assertEquals(requireObject.getData.unsafeRunSync(), OutputData(expected).withIsSecret(true)) | ||
} | ||
|
||
test("propagate secret field to whole typed stack reference") { | ||
given Context = DummyContext().unsafeRunSync() | ||
|
||
case class Test(s: String, i: Int) derives JsonReader | ||
val outputs = Map("s" -> JsString("value1"), "i" -> JsNumber(2)) | ||
val secretOutputNames = Set("i") | ||
|
||
val typedStackReference = | ||
StackReference | ||
.requireObject[Test](Output(outputs), Output(secretOutputNames)) | ||
.map(test => | ||
TypedStackReference( | ||
urn = Output(URN.empty), | ||
id = Output(ResourceId.empty), | ||
name = Output(""), | ||
outputs = test, | ||
secretOutputNames = Output(secretOutputNames) | ||
) | ||
) | ||
|
||
assertEquals(typedStackReference.getData.unsafeRunSync().secret, true) | ||
} | ||
end StackReferenceTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.