Skip to content

Commit 7f10814

Browse files
committed
Support Bash-style default values in EnvVarResolver (#299)
1 parent a394864 commit 7f10814

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

flink-sql-runner/src/main/java/com/datasqrl/flinkrunner/EnvVarResolver.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ public String resolve(String src) {
6868
String key;
6969
String defaultValue = null;
7070

71-
// Split at first ':' to support default values
72-
int colonIdx = rawKey.indexOf(':');
73-
if (colonIdx >= 0) {
74-
key = rawKey.substring(0, colonIdx);
75-
defaultValue = rawKey.substring(colonIdx + 1);
71+
// Support bash-style default values: ${VAR:-default} or ${VAR:=default}
72+
int splitIdx = rawKey.indexOf(":-");
73+
if (splitIdx == -1) {
74+
splitIdx = rawKey.indexOf(":=");
75+
}
76+
77+
if (splitIdx >= 0) {
78+
key = rawKey.substring(0, splitIdx);
79+
defaultValue = rawKey.substring(splitIdx + 2);
7680
} else {
7781
key = rawKey;
7882
}

flink-sql-runner/src/test/java/com/datasqrl/flinkrunner/EnvVarResolverTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ void givenSimilarEnvVariableNames_whenResolveEnv_Vars_thenPartialMatchDoesNotOcc
8383
@ParameterizedTest
8484
@CsvSource({
8585
// Env var not present, fallback used
86-
"'${USERNAME:guest}', 'guest'",
87-
"'Welcome ${USERNAME:anonymous}', 'Welcome anonymous'",
86+
"'${USERNAME:-guest}', 'guest'",
87+
"'${USERNAME:=guest}', 'guest'",
88+
"'Welcome ${USERNAME:-anonymous}', 'Welcome anonymous'",
8889
// Env var present, fallback ignored
89-
"'${USER:guest}', 'John'",
90-
"'Path: ${PATH:/default}', 'Path: /usr/bin'",
90+
"'${USER:-guest}', 'John'",
91+
"'${USER:=guest}', 'John'",
92+
"'Path: ${PATH:-/default}', 'Path: /usr/bin'",
9193
// Empty default
92-
"'Empty fallback: ${MISSING:}', 'Empty fallback: '",
94+
"'Empty fallback: ${MISSING:-}', 'Empty fallback: '",
9395
// Mixed present and fallback
94-
"'User=${USER}, ID=${ID:0000}', 'User=John, ID=0000'",
95-
"'${MISSING1:default1} and ${MISSING2:default2}', 'default1 and default2'"
96+
"'User=${USER}, ID=${ID:-0000}', 'User=John, ID=0000'",
97+
"'${MISSING1:-default1} and ${MISSING2:=default2}', 'default1 and default2'"
9698
})
9799
void givenDefaultEnvValues_whenResolve_thenFallbackOrUseEnvValue(
98100
String command, String expected) {
@@ -108,9 +110,10 @@ void givenDefaultEnvValues_whenResolve_thenFallbackOrUseEnvValue(
108110
@ParameterizedTest
109111
@ValueSource(
110112
strings = {
113+
"${TOKEN:default}", // Single colon no longer supported
111114
"${MISSING}", // Still fails because no default
112115
"Hi ${MISSING}!", // Same
113-
"${A:} ${B}" // A has empty default, B is missing
116+
"${A:-} ${B}" // A has empty default, B is missing
114117
})
115118
void givenMissingEnvWithoutDefault_whenResolve_thenThrowException(String command) {
116119
Map<String, String> envVariables = Map.of("A", "something");
@@ -122,9 +125,11 @@ void givenMissingEnvWithoutDefault_whenResolve_thenThrowException(String command
122125

123126
@ParameterizedTest
124127
@CsvSource({
125-
"'${TOKEN:abc:def}', 'abc:def'", // Semicolon inside fallback value
126-
"'${TOKEN:abc:def:ghi}', 'abc:def:ghi'", // colon is only split on first occurrence
127-
"'${TOKEN:}', ''" // colon at the end
128+
"'${TOKEN:-abc:def}', 'abc:def'", // Bash-style with colon in fallback
129+
"'${TOKEN:=abc:def}', 'abc:def'", // Bash-style with colon in fallback
130+
"'${TOKEN:-abc:def:ghi}', 'abc:def:ghi'", // Bash-style
131+
"'${TOKEN:-}', ''", // Bash-style colon-dash at the end
132+
"'${TOKEN:=}', ''" // Bash-style colon-equals at the end
128133
})
129134
void givenFallbackWithColons_whenResolve_thenParseCorrectly(String command, String expected) {
130135
Map<String, String> envVariables = Map.of(); // No TOKEN set

0 commit comments

Comments
 (0)