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
use crate::dsl::{FnJitter, VariantValue};
use anyhow;
use hashbrown::HashMap;
use serde_json::Value;
use std::collections::BTreeMap;

struct NodeCandidateResolver {
    index: usize,
    condition: FnJitter,
}

impl NodeCandidateResolver {
    pub fn new(index: usize, on: &str) -> anyhow::Result<Self> {
        let jitter = FnJitter::new(on)?;
        Ok(Self {
            index,
            condition: jitter,
        })
    }
}

pub struct JsonConfigResolver {
    condition_path: String,
    value_path: String,
    json: Value,
    node_resolvers: BTreeMap<String, Vec<NodeCandidateResolver>>,
}

impl JsonConfigResolver {
    pub fn new(json: Value) -> anyhow::Result<JsonConfigResolver> {
        Self::new_with_custom_path(json, "if".to_owned(), "value".to_owned())
    }

    pub fn new_with_custom_path(
        json: Value,
        condition_path: String,
        value_path: String,
    ) -> anyhow::Result<Self> {
        let mut r = Self {
            condition_path,
            value_path,
            json: Value::default(),
            node_resolvers: BTreeMap::new(),
        };
        r.set_json(json)?;
        Ok(r)
    }

    pub fn resolve(&self, ctx: &HashMap<String, VariantValue>) -> Value {
        let mut ret = self.json.clone();
        if self.node_resolvers.len() > 0 {
            for (path, resolvers) in self.node_resolvers.iter().rev() {
                if let Some(ptr_mut) = ret.pointer_mut(path) {
                    let mut match_value = Value::Null;
                    for resolver in resolvers {
                        if resolver.condition.evaluate(ctx) {
                            let k = format!("/{}/{}", resolver.index, self.value_path);
                            if let Some(v) = ptr_mut.pointer(&k) {
                                match_value = v.clone();
                                break;
                            }
                        }
                    }
                    *ptr_mut = match_value;
                }
            }
        }
        ret
    }

    fn set_json(&mut self, json: Value) -> anyhow::Result<()> {
        let mut path = Vec::new();
        self.parse_variants(&json, &mut path)?;
        self.json = json;
        Ok(())
    }

    fn parse_variants(&mut self, node: &Value, path: &mut Vec<String>) -> anyhow::Result<()> {
        match node {
            Value::Array(vec) => {
                if vec.len() > 0 {
                    let is_variant_array = vec.iter().all(|i| self.is_variant_array(i));
                    let mut node_resolvers: Vec<NodeCandidateResolver>;
                    if is_variant_array {
                        node_resolvers = Vec::with_capacity(vec.len());
                    } else {
                        node_resolvers = Vec::with_capacity(0);
                    }
                    for (idx, item) in vec.iter().enumerate() {
                        if is_variant_array {
                            let value = item.get(&self.value_path).unwrap();
                            if let Some(v) = item.get(&self.condition_path) {
                                match v {
                                    Value::String(on) => {
                                        let r = NodeCandidateResolver::new(idx, &on)?;
                                        node_resolvers.push(r);
                                    }
                                    Value::Null => {
                                        let r = NodeCandidateResolver::new(idx, "1")?;
                                        node_resolvers.push(r);
                                    }
                                    Value::Bool(true) => {
                                        let r = NodeCandidateResolver::new(idx, "1")?;
                                        node_resolvers.push(r);
                                    }
                                    Value::Number(n) => {
                                        let r = NodeCandidateResolver::new(idx, &n.to_string())?;
                                        node_resolvers.push(r);
                                    }
                                    _ => {}
                                }
                            }

                            path.push(format!("{}", idx));
                            path.push(format!("{}", self.value_path));
                            self.parse_variants(value, path)?;
                            path.pop();
                            path.pop();
                        } else {
                            path.push(format!("{}", idx));
                            self.parse_variants(item, path)?;
                            path.pop();
                        }
                    }
                    if is_variant_array {
                        self.node_resolvers
                            .insert(merge_json_path(path), node_resolvers);
                    }
                }
            }
            Value::Object(map) => {
                for (k, v) in map {
                    path.push(k.clone());
                    self.parse_variants(v, path)?;
                    path.pop();
                }
            }
            _ => {}
        }
        Ok(())
    }

    fn is_variant_array(&self, v: &Value) -> bool {
        if v.is_object() {
            if let Some(_) = v.get(&self.value_path) {
                if let Some(c) = v.get(&self.condition_path) {
                    return !c.is_array() && !c.is_object();
                }
            }
        }
        return false;
    }
}

fn merge_json_path(path: &Vec<String>) -> String {
    format!("/{}", path.join("/"))
}