Skip to content

Commit 60a3427

Browse files
committed
Refactor getAllEnvVars
1 parent 2f42928 commit 60a3427

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

runtime/runtime-core/mingw/src/aws/smithy/kotlin/runtime/util/SystemDefaultProviderMingw.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ import kotlinx.cinterop.*
1010
import platform.posix.environ
1111
import platform.posix.memcpy
1212

13-
internal actual val rawEnvironmentVariables: CPointer<CPointerVarOf<CPointer<ByteVarOf<Byte>>>>? = environ
14-
1513
public actual object SystemDefaultProvider : SystemDefaultProviderBase() {
1614
actual override val filePathSeparator: String = "\\"
1715
actual override fun osInfo(): OperatingSystem = OperatingSystem(OsFamily.Windows, osVersionFromKernel())
16+
17+
actual override fun getAllEnvVars(): Map<String, String> = memScoped {
18+
generateSequence(0) { it + 1 }
19+
.map { idx -> environ.get(idx)?.toKString() }
20+
.takeWhile { it != null }
21+
.associate { env ->
22+
val parts = env?.split("=", limit = 2)
23+
check(parts?.size == 2) { "Environment entry \"$env\" is malformed" }
24+
parts[0] to parts[1]
25+
}
26+
}
1827
}
1928

2029
// The functions below are adapted from C++ SDK:

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/util/SystemDefaultProviderBase.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ import kotlinx.cinterop.*
1010
import kotlinx.coroutines.withContext
1111
import platform.posix.*
1212

13-
internal expect val rawEnvironmentVariables: CPointer<CPointerVarOf<CPointer<ByteVarOf<Byte>>>>?
14-
1513
public abstract class SystemDefaultProviderBase : PlatformProvider {
16-
override fun getAllEnvVars(): Map<String, String> = memScoped {
17-
generateSequence(0) { it + 1 }
18-
.map { idx -> rawEnvironmentVariables?.get(idx)?.toKString() }
19-
.takeWhile { it != null }
20-
.associate { env ->
21-
val parts = env?.split("=", limit = 2)
22-
check(parts?.size == 2) { "Environment entry \"$env\" is malformed" }
23-
parts[0] to parts[1]
24-
}
25-
}
26-
2714
override fun getenv(key: String): String? = platform.posix.getenv(key)?.toKString()
2815

2916
override suspend fun readFileOrNull(path: String): ByteArray? = withContext(SdkDispatchers.IO) {

runtime/runtime-core/posix/src/aws/smithy/kotlin/runtime/util/SystemDefaultProviderPosix.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
package aws.smithy.kotlin.runtime.util
66

77
import kotlinx.cinterop.*
8-
import platform.posix.__environ
8+
import aws.smithy.platform.posix.get_environ_ptr
99
import platform.posix.uname
1010
import platform.posix.utsname
1111

12-
internal actual val rawEnvironmentVariables: CPointer<CPointerVarOf<CPointer<ByteVarOf<Byte>>>>? = __environ
13-
1412
public actual object SystemDefaultProvider : SystemDefaultProviderBase() {
1513
actual override val filePathSeparator: String = "/"
1614

@@ -39,4 +37,16 @@ public actual object SystemDefaultProvider : SystemDefaultProviderBase() {
3937

4038
return OperatingSystem(family, version)
4139
}
40+
41+
actual override fun getAllEnvVars(): Map<String, String> = memScoped {
42+
val environ = get_environ_ptr()
43+
generateSequence(0) { it + 1 }
44+
.map { idx -> environ?.get(idx)?.toKString() }
45+
.takeWhile { it != null }
46+
.associate { env ->
47+
val parts = env?.split("=", limit = 2)
48+
check(parts?.size == 2) { "Environment entry \"$env\" is malformed" }
49+
parts[0] to parts[1]
50+
}
51+
}
4252
}

0 commit comments

Comments
 (0)