Unverified Commit 5abd687a authored by Weihang Lo's avatar Weihang Lo Committed by GitHub
Browse files

missing `RuntimeConfig` node should behave the same as `{}` (#1678)



* 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: default avatarJohn DiSanti <jdisanti@amazon.com>
parent df077c97
Loading
Loading
Loading
Loading
+11 −14
Original line number Diff line number Diff line
@@ -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 ->
        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.get().getStringMember("relativePath").orNull()?.value
            val path = node.getStringMember("relativePath").orNull()?.value
            val runtimeCrateLocation = RuntimeCrateLocation(path = path, versions = crateVersionMap)
                RuntimeConfig(
                    node.get().getStringMemberOrDefault("cratePrefix", "aws-smithy"),
            return RuntimeConfig(
                node.getStringMemberOrDefault("cratePrefix", "aws-smithy"),
                runtimeCrateLocation = runtimeCrateLocation,
            )
            } else {
                RuntimeConfig()
            }
        }
    }

+9 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
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
@@ -13,6 +14,7 @@ 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
@@ -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`(