hypertune 0.6.2

Hypertune SDK for type safe configuration
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::anyhow;
use anyhow::Result;
use parking_lot::RwLock;
use serde_json::Value;

use crate::context::Context;
use crate::evaluate::EvaluationError;
use crate::expression::ListExpression;
use crate::expression::Logs;
use crate::expression::{Expression, ObjectExpression};
use crate::node_props::NodeProps;
use crate::node_props::NodePropsError;
use crate::node_props::NodePropsType;
use crate::query::*;

#[derive(Clone)]
pub enum UpdateStep {
    GetField {
        field_name: String,
        field_arguments: Value,
    },
    GetItem {
        index: usize,
    },
}

struct NodeInner {
    pub props: NodeProps,
}

impl NodeInner {
    fn new(props: NodeProps) -> Self {
        Self { props }
    }

    fn check_for_update(&self) -> bool {
        let context = self.props.context.read();

        match &context.init_data {
            None => false,
            Some(init_data) => match &self.props.commit_hash {
                Some(commit_hash) => commit_hash != &init_data.hash,
                None => true,
            },
        }
    }

    fn get_path(&self) -> String {
        let prefix = match self.props.parent {
            Some(ref parent) => format!("{} > ", parent.get_path()),
            None => "".to_string(),
        };

        match self.props.step {
            Some(UpdateStep::GetItem { ref index }) => format!("{}[{}]", prefix, index),
            Some(UpdateStep::GetField {
                ref field_name,
                ref field_arguments,
            }) => format!("{}{}({})", prefix, field_name, field_arguments),
            None => format!("{}{{}}", prefix),
        }
    }

    fn update(&mut self) -> Result<()> {
        match &self.props.parent {
            Some(parent) => match self.props.step {
                Some(UpdateStep::GetField {
                    ref field_name,
                    ref field_arguments,
                }) => {
                    self.props = parent.get_field(field_name, field_arguments.clone())?;
                    Ok(())
                }
                Some(UpdateStep::GetItem { index }) => {
                    let items = parent.get_items()?;
                    let props = items.get(index).ok_or(anyhow!(
                        "Child node with index is no longer present in parent items"
                    ))?;
                    self.props = props.clone();
                    Ok(())
                }
                None => Err(anyhow!(
                    "Could not recreate child from parent as Node step was missing"
                )),
            },
            None => {
                let context = self.props.context.read();
                let init_data = context.init_data.as_ref().ok_or(anyhow!("Could not get init data despite having already determined node needs to update"))?;
                self.props.expression = Some(init_data.reduced_expression.clone());
                self.props.commit_hash = Some(init_data.hash.clone());
                Ok(())
            }
        }
    }
}

#[derive(Clone)]
pub struct Node {
    inner: Arc<RwLock<NodeInner>>,
}

impl Node {
    pub fn new(props: NodeProps) -> Self {
        Self {
            inner: Arc::new(RwLock::new(NodeInner::new(props))),
        }
    }

    #[cfg(test)]
    pub fn new_with_hash(
        context: Arc<RwLock<Context>>,
        expression: Expression,
        commit_hash: String,
    ) -> Self {
        let props = NodeProps {
            r#type: expression.get_node_props_type(),
            expression: Some(expression),
            context,
            commit_hash: Some(commit_hash),
            parent: None,
            step: None,
        };
        Self::new(props)
    }

    pub fn wait_for_initialization(&self) {
        let node = self.inner.read();
        Context::wait_for_initialization(node.props.context.clone());
    }

    pub async fn flush_logs(&self) {
        let node = self.inner.read();
        Context::flush_logs(node.props.context.clone()).await;
    }

    pub fn close(&self) {
        let node = self.inner.read();
        Context::close(node.props.context.clone());
    }

    pub fn get_field(
        &self,
        field_name: &str,
        field_arguments: Value,
    ) -> Result<NodeProps, NodePropsError> {
        match self.get_field_inner(field_name, &field_arguments) {
            Ok(props) => Ok(props),
            Err(error) => {
                let node = self.inner.read();
                Context::log_error(node.props.context.clone(), format!("{}", error));
                Err(NodePropsError::GetField(NodeProps {
                    r#type: NodePropsType::Error,
                    context: node.props.context.clone(),
                    expression: None,
                    commit_hash: None,
                    parent: Some(self.clone()),
                    step: Some(UpdateStep::GetField {
                        field_name: field_name.to_string(),
                        field_arguments,
                    }),
                }))
            }
        }
    }

    fn get_field_inner(&self, field_name: &str, field_arguments: &Value) -> Result<NodeProps> {
        // Ideally we would only want the read lock for when we check for an
        // update, since we will rarely need the write lock. I tried using
        // an upgradable lock here but hit a nasty bug in parking_lot when trying
        // to unlock the upgraded lock. Opting to keep it simple here but if we
        // ever do want to optimise this and upgradable lock is still broken
        // we could just manually grab the read lock then the write lock,
        // dropping accordingly.
        let mut node = self.inner.write();
        if node.check_for_update() {
            node.update()?;
        }

        match &node.props.expression {
            Some(Expression::Object(expression)) => {
                let selection: Selection = HashMap::from([(
                    field_name.to_string(),
                    SelectionField {
                        field_query: None,
                        field_arguments: Some(match field_arguments {
                            Value::Object(ref object) => object
                                .into_iter()
                                .map(|(key, value)| match value.clone().try_into() {
                                    Ok(value) => Ok((key.clone(), value)),
                                    Err(error) => Err(error),
                                })
                                .collect::<Result<_>>(),
                            _ => Err(anyhow!("field arguments must be an object value")),
                        })
                        .transpose()?,
                    },
                )]);

                let fragment = Fragment {
                    object_type_name: expression.object_type_name.clone(),
                    selection,
                };
                let query: Query = HashMap::from([(expression.object_type_name.clone(), fragment)]);

                let expression = {
                    let context = node.props.context.read();
                    context.reduce(Some(&query), Expression::Object(expression.clone()))?
                };

                if let Expression::Object(ObjectExpression { fields, .. }) = expression {
                    fields
                        .get(field_name)
                        .ok_or(anyhow!(format!(
                            "Object expression does not contain field {}",
                            field_name
                        )))
                        .map(|expression| NodeProps {
                            r#type: expression.get_node_props_type(),
                            expression: Some(expression.clone()),
                            context: node.props.context.clone(),
                            commit_hash: node.props.commit_hash.clone(),
                            parent: Some(self.clone()),
                            step: Some(UpdateStep::GetField {
                                field_name: field_name.to_string(),
                                field_arguments: field_arguments.clone(),
                            }),
                        })
                } else {
                    Err(anyhow!(format!(
                        "Cannot get field {} as expression type is {:?}",
                        field_name, node.props.expression
                    )))
                }
            }
            _ => Err(anyhow!(format!(
                "Cannot get field {} as expression type is {:?}",
                field_name, node.props.expression
            ))),
        }
    }

    pub fn get_items(&self) -> Result<Vec<NodeProps>> {
        match self.get_items_inner() {
            Ok(props) => Ok(props),
            Err(error) => {
                let node = self.inner.read();
                Context::log_error(node.props.context.clone(), format!("{}", error));
                Err(error)
            }
        }
    }

    fn get_items_inner(&self) -> Result<Vec<NodeProps>> {
        let mut node = self.inner.write();
        if node.check_for_update() {
            node.update()?;
        }

        match &node.props.expression {
            Some(Expression::List(ListExpression { items, logs, .. })) => {
                let list_logs = Logs::merge(
                    logs,
                    &Logs::evaluation_logs(node.props.expression.as_ref().unwrap()),
                );

                Ok(items
                    .iter()
                    .enumerate()
                    .map(|(index, item)| {
                        let expression = item.clone().merge_logs(&list_logs);

                        NodeProps {
                            r#type: expression.get_node_props_type(),
                            expression: Some(expression),
                            context: node.props.context.clone(),
                            commit_hash: node.props.commit_hash.clone(),
                            parent: Some(self.clone()),
                            step: Some(UpdateStep::GetItem { index }),
                        }
                    })
                    .collect())
            }
            _ => Err(anyhow!(format!(
                "Cannot get items as expression type is {:?}",
                node.props.expression
            ))),
        }
    }

    pub fn evaluate(&self) -> std::result::Result<Value, EvaluationError> {
        match self.evaluate_inner() {
            Ok(props) => Ok(props),
            Err(error) => {
                let node = self.inner.read();
                Context::log_error(node.props.context.clone(), format!("{}", error));
                Err(error)
            }
        }
    }

    fn evaluate_inner(&self) -> std::result::Result<Value, EvaluationError> {
        let node = self.inner.read();
        match &node.props.expression {
            Some(expression) => match expression.evaluate() {
                Ok((value, logs)) => {
                    node.props
                        .context
                        .write()
                        .backend_logger
                        .reduction_logs(&logs);
                    Ok(value)
                }
                Err(error) => Err(error),
            },
            None => Err(EvaluationError::MissingExpression),
        }
    }

    pub fn get_error_node_props(&self) -> NodeProps {
        let node = self.inner.read();
        NodeProps {
            r#type: NodePropsType::Error,
            context: node.props.context.clone(),
            expression: None,
            commit_hash: None,
            parent: Some(self.clone()),
            step: None,
        }
    }

    pub fn log_unexpected_type_error(&self) {
        let path = self.get_path();
        let node = self.inner.read();
        Context::log_error(
            node.props.context.clone(),
            format!("Unexpected type error at {}", path),
        );
    }

    pub fn log_unexpected_value_error(&self, value: Result<Value, EvaluationError>) {
        let path = self.get_path();
        let node = self.inner.read();
        match node.props.r#type {
            // If there was an error in producing the node props then this
            // 'error' was expected and client SDKs will use the 'expected'
            // fallback error.
            NodePropsType::Error => (),
            _ => Context::log_error(
                node.props.context.clone(),
                format!("Unexpected value error at {}: {:?}", path, value),
            ),
        };
    }

    fn get_path(&self) -> String {
        self.inner.read().get_path()
    }
}

#[cfg(test)]
mod tests {
    use parking_lot::RwLock;
    use serde_json::Value;
    use std::sync::Arc;

    use super::Node;
    use crate::context::Context;
    use crate::expression::{BooleanExpression, Expression};

    #[test]
    fn test_get_field() {
        let json = r#"
{
  "id": "gZDqtndJoiI8RQ7q4CuCo",
  "logs": {
    "evaluations": {
      "gy-OTD4_b0zH2XOmaXctc": 1
    },
    "events": {},
    "exposures": {}
  },
  "type": "ObjectExpression",
  "fields": {
    "unreducedFlag": {
      "id": "ej8Xy-9QFcd29W22D7poY",
      "body": {
        "id": "usSc5C1in4PECg9FtttwZ",
        "logs": {
          "events": {},
          "exposures": {},
          "evaluations": {}
        },
        "type": "SwitchExpression",
        "cases": [
          {
            "when": {
              "a": {
                "id": "D30a6QkgwwZqZirsvUv44",
                "logs": {
                  "events": {},
                  "exposures": {},
                  "evaluations": {}
                },
                "type": "GetFieldExpression",
                "object": {
                  "id": "LLtvzKMh9aUUiuOQ5aTfu",
                  "logs": {
                    "events": {},
                    "exposures": {},
                    "evaluations": {}
                  },
                  "type": "VariableExpression",
                  "valueType": {
                    "type": "ObjectValueType",
                    "objectTypeName": "Root_unreducedFlag_args"
                  },
                  "variableId": "IRjkAecIF-NGJEuHgIc4r"
                },
                "fieldPath": "num",
                "valueType": {
                  "type": "IntValueType"
                }
              },
              "b": {
                "id": "TphPzOq00QBZWn4J6c1sa",
                "logs": {
                  "events": {},
                  "exposures": {},
                  "evaluations": {}
                },
                "type": "ListExpression",
                "items": [
                  {
                    "id": "7aneXIcBqW0lrl02VxDUz",
                    "logs": {
                      "events": {},
                      "exposures": {},
                      "evaluations": {}
                    },
                    "type": "IntExpression",
                    "value": 42,
                    "valueType": {
                      "type": "IntValueType"
                    }
                  }
                ],
                "valueType": {
                  "type": "ListValueType",
                  "itemValueType": {
                    "type": "IntValueType"
                  }
                }
              },
              "id": "toCWw-idWeRnLG0ZDEp2U",
              "logs": {
                "events": {},
                "exposures": {},
                "evaluations": {}
              },
              "type": "ComparisonExpression",
              "operator": "in",
              "valueType": {
                "type": "BooleanValueType"
              }
            },
            "then": {
              "id": "mPQwUTHRjFLfXIuw-Lf5L",
              "logs": {
                "events": {},
                "exposures": {},
                "evaluations": {}
              },
              "type": "BooleanExpression",
              "value": true,
              "valueType": {
                "type": "BooleanValueType"
              }
            }
          }
        ],
        "control": {
          "id": "lf6BDeESCHuGUMFVv3Bj6",
          "logs": {
            "events": {},
            "exposures": {},
            "evaluations": {}
          },
          "type": "BooleanExpression",
          "value": true,
          "valueType": {
            "type": "BooleanValueType"
          }
        },
        "default": {
          "id": "5yMNttnvgNyqzD0GN07kV",
          "logs": {
            "events": {},
            "exposures": {},
            "evaluations": {}
          },
          "type": "BooleanExpression",
          "value": false,
          "valueType": {
            "type": "BooleanValueType"
          }
        },
        "valueType": {
          "type": "BooleanValueType"
        }
      },
      "logs": {
        "events": {},
        "exposures": {},
        "evaluations": {}
      },
      "type": "FunctionExpression",
      "valueType": {
        "type": "FunctionValueType",
        "returnValueType": {
          "type": "BooleanValueType"
        },
        "parameterValueTypes": [
          {
            "type": "ObjectValueType",
            "objectTypeName": "Root_unreducedFlag_args"
          }
        ]
      },
      "parameters": [
        {
          "id": "IRjkAecIF-NGJEuHgIc4r",
          "name": "unreducedFlagArgs"
        }
      ]
    }
  },
  "valueType": {
    "type": "ObjectValueType",
    "objectTypeName": "Root"
  },
  "objectTypeName": "Root"
}"#;

        let expression: Expression = serde_json::from_str(json).unwrap();
        let node = Node::new_with_hash(
            Arc::new(RwLock::new(Context::test())),
            expression,
            "".to_string(),
        );

        let field_arguments: Value = serde_json::from_str(
            r#"
            {
               "num": 42
            }"#,
        )
        .unwrap();

        let field = node.get_field("unreducedFlag", field_arguments).unwrap();
        if let Some(Expression::Boolean(BooleanExpression { value, .. })) = field.get_expression() {
            assert!(value);
        } else {
            panic!("Expected BooleanExpression");
        }

        let field_arguments: Value = serde_json::from_str(
            r#"
            {
               "num": 22
            }"#,
        )
        .unwrap();

        let field = node.get_field("unreducedFlag", field_arguments).unwrap();
        if let Some(Expression::Boolean(BooleanExpression { value, .. })) = field.get_expression() {
            assert!(!value);
        } else {
            panic!("Expected BooleanExpression");
        }
    }
}