Skip to content

Commit

Permalink
missing RuntimeConfig node should behave the same as {} (#1678)
Browse files Browse the repository at this point in the history
* test: missing RuntimeConfig node should behave the same as `{}`
* missing RuntimeConfig node should behave the same as `{}`
* use `maybe` prefix for Optional type

Co-authored-by: John DiSanti <[email protected]>
  • Loading branch information
weihanglo and jdisanti authored Aug 26, 2022
1 parent df077c9 commit 5abd687
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,18 @@ data class RuntimeConfig(
/**
* Load a `RuntimeConfig` from an [ObjectNode] (JSON)
*/
fun fromNode(node: Optional<ObjectNode>): RuntimeConfig {
return if (node.isPresent) {
val crateVersionMap = node.get().getObjectMember("versions").orElse(Node.objectNode()).members.entries.let { members ->
val map = members.associate { it.key.toString() to it.value.expectStringNode().value }
CrateVersionMap(map)
}
val path = node.get().getStringMember("relativePath").orNull()?.value
val runtimeCrateLocation = RuntimeCrateLocation(path = path, versions = crateVersionMap)
RuntimeConfig(
node.get().getStringMemberOrDefault("cratePrefix", "aws-smithy"),
runtimeCrateLocation = runtimeCrateLocation,
)
} else {
RuntimeConfig()
fun fromNode(maybeNode: Optional<ObjectNode>): RuntimeConfig {
val node = maybeNode.orElse(Node.objectNode())
val crateVersionMap = node.getObjectMember("versions").orElse(Node.objectNode()).members.entries.let { members ->
val map = members.associate { it.key.toString() to it.value.expectStringNode().value }
CrateVersionMap(map)
}
val path = node.getStringMember("relativePath").orNull()?.value
val runtimeCrateLocation = RuntimeCrateLocation(path = path, versions = crateVersionMap)
return RuntimeConfig(
node.getStringMemberOrDefault("cratePrefix", "aws-smithy"),
runtimeCrateLocation = runtimeCrateLocation,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
package software.amazon.smithy.rust.codegen.smithy

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.rust.codegen.rustlang.CratesIo
import software.amazon.smithy.rust.codegen.rustlang.DependencyLocation
import software.amazon.smithy.rust.codegen.rustlang.Local
import java.util.Optional

class RuntimeTypesTest {
@ParameterizedTest
Expand All @@ -26,6 +28,13 @@ class RuntimeTypesTest {
cfg.runtimeCrateLocation shouldBe expectedCrateLocation
}

@Test
fun `succeeded to provide a default runtime config if missing`() {
// This default config should share the same behaviour with `{}` empty object.
val cfg = RuntimeConfig.fromNode(Optional.empty())
cfg.runtimeCrateLocation shouldBe RuntimeCrateLocation(null, CrateVersionMap(mapOf()))
}

@ParameterizedTest
@MethodSource("runtimeCrateLocationProvider")
fun `runtimeCrateLocation provides dependency location`(
Expand Down

0 comments on commit 5abd687

Please sign in to comment.