lerna 2.0.3

Lerna is a framework for elegantly configuring complex applications
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
//! Interpolation resolution engine
//!
//! Resolves interpolations like ${key}, ${oc.env:VAR}, ${oc.decode:...}

use crate::config::{ConfigDict, ConfigValue};
use std::collections::HashMap;
use std::env;

#[derive(Clone, Debug, PartialEq)]
pub enum InterpolationType {
    /// Simple key reference: ${key}
    Key(String),
    /// Nested key reference: ${parent.child}
    NestedKey(Vec<String>),
    /// Environment variable: ${oc.env:VAR} or ${oc.env:VAR,default}
    Env(String, Option<String>),
    /// Decode string: ${oc.decode:...}
    Decode(String),
    /// Create object: ${oc.create:...}
    Create(String),
    /// Selection: ${oc.select:key,dict}
    Select(String, String),
    /// Literal escape: $${...} -> ${...}
    EscapedLiteral(String),
    /// Not an interpolation
    Literal(String),
}

/// Parse an interpolation string into components
pub fn parse_interpolation(s: &str) -> Result<InterpolationType, String> {
    let s = s.trim();

    // Check for escaped literal: $${...} -> ${...}
    if s.starts_with("$${") && s.ends_with('}') {
        let inner = &s[3..s.len() - 1]; // Skip $${ and }
        return Ok(InterpolationType::EscapedLiteral(inner.to_string()));
    }

    // Must start with ${ and end with }
    if !s.starts_with("${") || !s.ends_with('}') {
        return Ok(InterpolationType::Literal(s.to_string()));
    }

    let inner = &s[2..s.len() - 1];

    // Check for oc.* resolvers
    if inner.starts_with("oc.env:") {
        let rest = &inner[7..];
        if let Some(comma_pos) = rest.find(',') {
            let var = rest[..comma_pos].trim().to_string();
            let default = rest[comma_pos + 1..].trim().to_string();
            return Ok(InterpolationType::Env(var, Some(default)));
        }
        return Ok(InterpolationType::Env(rest.trim().to_string(), None));
    }

    if inner.starts_with("oc.decode:") {
        return Ok(InterpolationType::Decode(inner[10..].to_string()));
    }

    if inner.starts_with("oc.create:") {
        return Ok(InterpolationType::Create(inner[10..].to_string()));
    }

    if inner.starts_with("oc.select:") {
        let rest = &inner[10..];
        if let Some(comma_pos) = rest.find(',') {
            let key = rest[..comma_pos].trim().to_string();
            let dict = rest[comma_pos + 1..].trim().to_string();
            return Ok(InterpolationType::Select(key, dict));
        }
        return Err(format!("Invalid oc.select syntax: {}", s));
    }

    // Check for nested key
    if inner.contains('.') {
        let parts: Vec<String> = inner.split('.').map(String::from).collect();
        return Ok(InterpolationType::NestedKey(parts));
    }

    // Simple key reference
    Ok(InterpolationType::Key(inner.to_string()))
}

/// Find all interpolations in a string
pub fn find_interpolations(s: &str) -> Vec<(usize, usize, String)> {
    let mut results = Vec::new();
    let chars: Vec<char> = s.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        // Check for ${ or $${
        if chars[i] == '$' && i + 1 < len && chars[i + 1] == '{' {
            let start = i;
            let mut depth = 1;
            let mut j = i + 2;

            // Skip escaped literals
            if start > 0 && chars[start - 1] == '$' {
                i += 1;
                continue;
            }

            while j < len && depth > 0 {
                if chars[j] == '{' {
                    depth += 1;
                } else if chars[j] == '}' {
                    depth -= 1;
                }
                j += 1;
            }

            if depth == 0 {
                let interp: String = chars[start..j].iter().collect();
                results.push((start, j, interp));
                i = j;
                continue;
            }
        }
        i += 1;
    }

    results
}

/// Resolution context holding current config and environment
#[derive(Clone)]
pub struct ResolutionContext {
    /// The root config being resolved
    config: ConfigDict,
    /// Environment variable overrides for testing
    env_overrides: HashMap<String, String>,
}

impl ResolutionContext {
    pub fn new(config: ConfigDict) -> Self {
        Self {
            config,
            env_overrides: HashMap::new(),
        }
    }

    pub fn with_env_override(mut self, key: &str, value: &str) -> Self {
        self.env_overrides
            .insert(key.to_string(), value.to_string());
        self
    }

    /// Resolve a key path in the config
    fn resolve_key(&self, key: &str) -> Option<ConfigValue> {
        let parts: Vec<&str> = key.split('.').collect();
        self.resolve_key_parts(&parts)
    }

    fn resolve_key_parts(&self, parts: &[&str]) -> Option<ConfigValue> {
        if parts.is_empty() {
            return None;
        }

        let mut current = ConfigValue::Dict(self.config.clone());

        for part in parts {
            match current {
                ConfigValue::Dict(ref dict) => {
                    current = dict.get(*part)?.clone();
                }
                _ => return None,
            }
        }

        Some(current)
    }

    /// Get environment variable
    fn get_env(&self, var: &str) -> Option<String> {
        if let Some(val) = self.env_overrides.get(var) {
            return Some(val.clone());
        }
        env::var(var).ok()
    }
}

/// Resolve a single interpolation
pub fn resolve_interpolation(
    interp: &InterpolationType,
    ctx: &ResolutionContext,
) -> Result<ConfigValue, String> {
    match interp {
        InterpolationType::Key(key) => ctx
            .resolve_key(key)
            .ok_or_else(|| format!("Key not found: {}", key)),
        InterpolationType::NestedKey(parts) => {
            let parts_ref: Vec<&str> = parts.iter().map(|s| s.as_str()).collect();
            ctx.resolve_key_parts(&parts_ref)
                .ok_or_else(|| format!("Key not found: {}", parts.join(".")))
        }
        InterpolationType::Env(var, default) => match ctx.get_env(var) {
            Some(val) => Ok(ConfigValue::String(val)),
            None => match default {
                Some(d) => Ok(ConfigValue::String(d.clone())),
                None => Err(format!("Environment variable not found: {}", var)),
            },
        },
        InterpolationType::Decode(expr) => {
            // Simple decode - just return as string for now
            // Full decode would need expression parsing
            Ok(ConfigValue::String(expr.clone()))
        }
        InterpolationType::Create(_) => Err("oc.create requires runtime instantiation".to_string()),
        InterpolationType::Select(key, _dict_ref) => {
            // Need to resolve the dict reference first
            Err(format!("oc.select requires full resolution: {}", key))
        }
        InterpolationType::EscapedLiteral(inner) => {
            Ok(ConfigValue::String(format!("${{{}}}", inner)))
        }
        InterpolationType::Literal(s) => Ok(ConfigValue::String(s.clone())),
    }
}

/// Resolve all interpolations in a string
pub fn resolve_string(s: &str, ctx: &ResolutionContext) -> Result<String, String> {
    let interpolations = find_interpolations(s);

    if interpolations.is_empty() {
        return Ok(s.to_string());
    }

    let mut result = s.to_string();

    // Process in reverse order to maintain positions
    for (start, end, interp_str) in interpolations.into_iter().rev() {
        let interp_type = parse_interpolation(&interp_str)?;
        let resolved = resolve_interpolation(&interp_type, ctx)?;

        let replacement = match resolved {
            ConfigValue::String(s) => s,
            ConfigValue::Int(i) => i.to_string(),
            ConfigValue::Float(f) => f.to_string(),
            ConfigValue::Bool(b) => b.to_string(),
            _ => return Err(format!("Cannot interpolate complex type: {:?}", resolved)),
        };

        result.replace_range(start..end, &replacement);
    }

    Ok(result)
}

/// Resolve all interpolations in a config value recursively
pub fn resolve_value(value: ConfigValue, ctx: &ResolutionContext) -> Result<ConfigValue, String> {
    match value {
        ConfigValue::String(s) => {
            // Check if entire string is a single interpolation
            let s_trimmed = s.trim();
            if s_trimmed.starts_with("${")
                && s_trimmed.ends_with('}')
                && find_interpolations(&s).len() == 1
            {
                // Return the actual type from resolution
                let interp_type = parse_interpolation(&s)?;
                return resolve_interpolation(&interp_type, ctx);
            }
            // Otherwise resolve as string
            Ok(ConfigValue::String(resolve_string(&s, ctx)?))
        }
        ConfigValue::List(items) => {
            let resolved: Result<Vec<ConfigValue>, String> =
                items.into_iter().map(|v| resolve_value(v, ctx)).collect();
            Ok(ConfigValue::List(resolved?))
        }
        ConfigValue::Dict(dict) => {
            let mut resolved = ConfigDict::new();
            for (key, val) in dict.iter() {
                resolved.insert(key.to_string(), resolve_value(val.clone(), ctx)?);
            }
            Ok(ConfigValue::Dict(resolved))
        }
        // Primitives pass through unchanged
        other => Ok(other),
    }
}

/// Fully resolve a config dictionary
pub fn resolve_config(config: ConfigDict) -> Result<ConfigDict, String> {
    let ctx = ResolutionContext::new(config.clone());

    match resolve_value(ConfigValue::Dict(config), &ctx)? {
        ConfigValue::Dict(d) => Ok(d),
        _ => Err("Config resolution did not return a dict".to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_interpolation_simple() {
        let result = parse_interpolation("${key}").unwrap();
        assert_eq!(result, InterpolationType::Key("key".to_string()));
    }

    #[test]
    fn test_parse_interpolation_nested() {
        let result = parse_interpolation("${db.host}").unwrap();
        assert_eq!(
            result,
            InterpolationType::NestedKey(vec!["db".to_string(), "host".to_string()])
        );
    }

    #[test]
    fn test_parse_interpolation_env() {
        let result = parse_interpolation("${oc.env:HOME}").unwrap();
        assert_eq!(result, InterpolationType::Env("HOME".to_string(), None));
    }

    #[test]
    fn test_parse_interpolation_env_default() {
        let result = parse_interpolation("${oc.env:MISSING,default_val}").unwrap();
        assert_eq!(
            result,
            InterpolationType::Env("MISSING".to_string(), Some("default_val".to_string()))
        );
    }

    #[test]
    fn test_find_interpolations() {
        let s = "host=${db.host}, port=${db.port}";
        let interps = find_interpolations(s);
        assert_eq!(interps.len(), 2);
        assert_eq!(interps[0].2, "${db.host}");
        assert_eq!(interps[1].2, "${db.port}");
    }

    #[test]
    fn test_resolve_simple_key() {
        let mut config = ConfigDict::new();
        config.insert("name".to_string(), ConfigValue::String("test".to_string()));

        let ctx = ResolutionContext::new(config);
        let interp = InterpolationType::Key("name".to_string());
        let result = resolve_interpolation(&interp, &ctx).unwrap();

        assert_eq!(result, ConfigValue::String("test".to_string()));
    }

    #[test]
    fn test_resolve_nested_key() {
        let mut db = ConfigDict::new();
        db.insert(
            "host".to_string(),
            ConfigValue::String("localhost".to_string()),
        );

        let mut config = ConfigDict::new();
        config.insert("db".to_string(), ConfigValue::Dict(db));

        let ctx = ResolutionContext::new(config);
        let interp = InterpolationType::NestedKey(vec!["db".to_string(), "host".to_string()]);
        let result = resolve_interpolation(&interp, &ctx).unwrap();

        assert_eq!(result, ConfigValue::String("localhost".to_string()));
    }

    #[test]
    fn test_resolve_string() {
        let mut config = ConfigDict::new();
        config.insert(
            "host".to_string(),
            ConfigValue::String("localhost".to_string()),
        );
        config.insert("port".to_string(), ConfigValue::Int(3306));

        let ctx = ResolutionContext::new(config);
        let result = resolve_string("mysql://${host}:${port}", &ctx).unwrap();

        assert_eq!(result, "mysql://localhost:3306");
    }

    #[test]
    fn test_resolve_env() {
        let config = ConfigDict::new();
        let ctx = ResolutionContext::new(config).with_env_override("TEST_VAR", "test_value");

        let interp = InterpolationType::Env("TEST_VAR".to_string(), None);
        let result = resolve_interpolation(&interp, &ctx).unwrap();

        assert_eq!(result, ConfigValue::String("test_value".to_string()));
    }

    #[test]
    fn test_resolve_env_default() {
        let config = ConfigDict::new();
        let ctx = ResolutionContext::new(config);

        let interp = InterpolationType::Env(
            "NONEXISTENT_VAR_12345".to_string(),
            Some("default".to_string()),
        );
        let result = resolve_interpolation(&interp, &ctx).unwrap();

        assert_eq!(result, ConfigValue::String("default".to_string()));
    }

    #[test]
    fn test_resolve_config() {
        let mut db = ConfigDict::new();
        db.insert(
            "host".to_string(),
            ConfigValue::String("localhost".to_string()),
        );
        db.insert("port".to_string(), ConfigValue::Int(3306));

        let mut config = ConfigDict::new();
        config.insert("db".to_string(), ConfigValue::Dict(db));
        config.insert(
            "url".to_string(),
            ConfigValue::String("mysql://${db.host}:${db.port}".to_string()),
        );

        let resolved = resolve_config(config).unwrap();

        assert_eq!(
            resolved.get("url").unwrap(),
            &ConfigValue::String("mysql://localhost:3306".to_string())
        );
    }

    #[test]
    fn test_escaped_literal() {
        let result = parse_interpolation("$${escaped}").unwrap();
        assert_eq!(
            result,
            InterpolationType::EscapedLiteral("escaped".to_string())
        );
    }
}