-
Notifications
You must be signed in to change notification settings - Fork 180
/
AwsEndpointsStdLib.kt
54 lines (48 loc) · 2.35 KB
/
AwsEndpointsStdLib.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rustsdk
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.node.ObjectNode
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.awsStandardLib
import kotlin.io.path.readText
/**
* Standard library functions for AWS-specific endpoint standard library functions.
*
* This decorator uses partitions.json to source a default partition map for the partition resolver (when used).
*
* For test purposes, [awsStandardLib] can be used directly with a manually supplied partitions.json
*/
class AwsEndpointsStdLib() : ClientCodegenDecorator {
private var partitionsCache: ObjectNode? = null
override val name: String = "AwsEndpointsStdLib"
override val order: Byte = 0
private fun partitionMetadata(sdkSettings: SdkSettings): ObjectNode {
if (partitionsCache == null) {
val partitionsJson = when (val path = sdkSettings.partitionsConfigPath) {
null -> (
javaClass.getResource("/default-partitions.json")
?: throw IllegalStateException("Failed to find default-partitions.json in the JAR")
).readText()
else -> path.readText()
}
partitionsCache = Node.parse(partitionsJson).expectObjectNode()
}
return partitionsCache!!
}
override fun endpointCustomizations(codegenContext: ClientCodegenContext): List<EndpointCustomization> {
return listOf<EndpointCustomization>(
object : EndpointCustomization {
override fun customRuntimeFunctions(codegenContext: ClientCodegenContext): List<CustomRuntimeFunction> {
val sdkSettings = SdkSettings.from(codegenContext.settings)
return awsStandardLib(codegenContext.runtimeConfig, partitionMetadata(sdkSettings))
}
},
)
}
}