pipegen 0.2.2

A generator for data integration app using pipebase framework
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::models::{
    App, ContextStore, DataField, Entity, EntityAccept, Object, Pipe, VisitEntity,
    CONTEXT_STORE_ENTITY_ID_FIELD, DATA_FIELD_ENTITY_ID_FIELD, OBJECT_ENTITY_ID_FIELD,
    PIPE_ENTITY_DEPENDENCY_FIELD, PIPE_ENTITY_ID_FIELD, PIPE_ENTITY_OUTPUT_FIELD,
    PIPE_ENTITY_TYPE_FIELD,
};

use crate::error::{api_error, Result};
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Display};

use super::utils::PipeGraph;

pub struct ValidationErrorDetailsDisplay {
    details: HashMap<String, String>,
}

impl ValidationErrorDetailsDisplay {
    pub fn new(details: HashMap<String, String>) -> Self {
        ValidationErrorDetailsDisplay { details }
    }
}

impl Display for ValidationErrorDetailsDisplay {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut buffer: Vec<String> = vec![];
        for (location, detail) in &self.details {
            buffer.push(format!("{} at {}", detail, location))
        }
        write!(f, "{}", buffer.join(",\n"))
    }
}

pub trait Validate {
    fn new(location: &str) -> Self;
    fn validate(&mut self) -> Result<()>;
    /// check any error details collected
    fn check(details: &HashMap<String, String>) -> Result<()> {
        match details.is_empty() {
            true => Ok(()),
            false => Err(api_error(ValidationErrorDetailsDisplay::new(
                details.to_owned(),
            ))),
        }
    }
}

pub struct PipeIdValidator {
    pub location: String,
    pub ids: Vec<String>,
}

impl VisitEntity<Pipe> for PipeIdValidator {
    fn visit(&mut self, pipe: &Pipe) {
        self.ids.push(pipe.get_id())
    }
}

impl Validate for PipeIdValidator {
    fn new(location: &str) -> Self {
        PipeIdValidator {
            location: location.to_owned(),
            ids: vec![],
        }
    }

    fn validate(&mut self) -> Result<()> {
        // snake case validation
        let errors = validate_ids_with_predicate(
            &self.ids,
            &self.location,
            PIPE_ENTITY_ID_FIELD,
            "use snake_case",
            &is_snake_lower_case,
        );
        Self::check(&errors)?;
        let errors = validate_ids_uniqueness(
            &self.ids,
            &self.location,
            PIPE_ENTITY_ID_FIELD,
            "duplicated",
        );
        Self::check(&errors)
    }
}

pub struct PipeTypeValidator {
    pub location: String,
    pub has_types: Vec<bool>,
}

impl VisitEntity<Pipe> for PipeTypeValidator {
    fn visit(&mut self, pipe: &Pipe) {
        self.has_types.push(pipe.has_type())
    }
}

impl Validate for PipeTypeValidator {
    fn new(location: &str) -> Self {
        PipeTypeValidator {
            location: location.to_owned(),
            has_types: vec![],
        }
    }

    fn validate(&mut self) -> Result<()> {
        let mut errors: HashMap<String, String> = HashMap::new();
        for i in 0..self.has_types.len() {
            let has_type = self.has_types.get(i).unwrap();
            if !has_type {
                let location = format!("{}[{}].{}", self.location, i, PIPE_ENTITY_TYPE_FIELD);
                errors.insert(location, String::from("pipe type undefined"));
            }
        }
        Self::check(&errors)?;
        Ok(())
    }
}

pub struct PipeOutputValidator {
    location: String,
    pipes: Vec<Pipe>,
}

impl VisitEntity<Pipe> for PipeOutputValidator {
    fn visit(&mut self, pipe: &Pipe) {
        self.pipes.push(pipe.to_owned())
    }
}

impl Validate for PipeOutputValidator {
    fn new(location: &str) -> Self {
        PipeOutputValidator {
            location: location.to_owned(),
            pipes: Vec::new(),
        }
    }

    fn validate(&mut self) -> Result<()> {
        let mut errors: HashMap<String, String> = HashMap::new();
        for (i, pipe) in self.pipes.iter().enumerate() {
            let location = format!("{}[{}].{}", self.location, i, PIPE_ENTITY_OUTPUT_FIELD);
            if pipe.is_sink() && pipe.has_output() {
                errors.insert(location, String::from("found invalid output for sink pipe"));
                continue;
            }
            if !pipe.is_sink() && !pipe.has_output() {
                errors.insert(location, String::from("pipe output not found"));
            }
        }
        Self::check(&errors)?;
        Ok(())
    }
}

pub struct PipeGraphValidator {
    pub location: String,
    pub graph: PipeGraph<Pipe>,
    pub index: HashMap<String, usize>,
}

impl VisitEntity<Pipe> for PipeGraphValidator {
    fn visit(&mut self, pipe: &Pipe) {
        self.graph.add_pipe(pipe, pipe.to_owned());
        self.index.insert(pipe.get_id(), self.index.len());
    }
}

impl Validate for PipeGraphValidator {
    fn new(location: &str) -> Self {
        PipeGraphValidator {
            location: location.to_owned(),
            graph: PipeGraph::new(),
            index: HashMap::new(),
        }
    }

    fn validate(&mut self) -> Result<()> {
        let mut errors: HashMap<String, String> = HashMap::new();
        for (pid, i) in &self.index {
            let pipe = self.graph.get_pipe_value(pid).unwrap();
            let location = format!("{}[{}].{}", self.location, i, PIPE_ENTITY_DEPENDENCY_FIELD);
            if pipe.is_source() {
                if self.graph.has_upstream_pipe(pid) {
                    errors.insert(
                        location.to_owned(),
                        String::from("found invalid upstream for source pipe"),
                    );
                }
                continue;
            }
            // non-source pipe must have upstream
            if !self.graph.has_upstream_pipe(pid) {
                errors.insert(
                    location.to_owned(),
                    "no upstream found for downstream pipe".to_string(),
                );
                continue;
            }
            for upid in self.graph.get_upstream_pipes(pid) {
                if !self.graph.has_pipe(upid) {
                    errors.insert(location.to_owned(), "upstream does not exists".to_string());
                }
            }
        }
        Self::check(&errors)?;
        let cycle_vertex = self.graph.find_cycle();
        for pid in &cycle_vertex {
            let location = format!("{}[{}]", self.location, self.index.get(pid).unwrap());
            errors.insert(location, "cycle detected".to_owned());
        }
        Self::check(&errors)
    }
}

#[derive(Default)]
pub struct ObjectIdValidator {
    pub location: String,
    pub ids: Vec<String>,
}

impl VisitEntity<Object> for ObjectIdValidator {
    fn visit(&mut self, object: &Object) {
        self.ids.push(object.get_id())
    }
}

impl Validate for ObjectIdValidator {
    fn new(location: &str) -> Self {
        ObjectIdValidator {
            location: location.to_owned(),
            ..Default::default()
        }
    }

    fn validate(&mut self) -> Result<()> {
        // camel case validation
        let errors = validate_ids_with_predicate(
            &self.ids,
            &self.location,
            OBJECT_ENTITY_ID_FIELD,
            "use CamelCase",
            &is_camel_case,
        );
        Self::check(&errors)?;
        let errors = validate_ids_uniqueness(
            &self.ids,
            &self.location,
            OBJECT_ENTITY_ID_FIELD,
            "duplicated",
        );
        Self::check(&errors)
    }
}

#[derive(Default)]
pub struct ObjectDependencyValidator {
    pub location: String,
    pub deps: HashMap<String, Vec<String>>,
    pub ids: Vec<String>,
}

impl VisitEntity<Object> for ObjectDependencyValidator {
    fn visit(&mut self, object: &Object) {
        let id = &object.get_id();
        let dep = object.list_dependency();
        self.ids.push(id.to_owned());
        self.deps.insert(id.to_owned(), dep);
    }
}

impl Validate for ObjectDependencyValidator {
    fn new(location: &str) -> Self {
        ObjectDependencyValidator {
            location: location.to_owned(),
            ..Default::default()
        }
    }

    fn validate(&mut self) -> Result<()> {
        let mut errors: HashMap<String, String> = HashMap::new();
        for i in 0..self.ids.len() {
            let id = self.ids.get(i).unwrap();
            let mut j: usize = 0;
            for dep in self.deps.get(id).unwrap() {
                if !self.deps.contains_key(dep) {
                    let location = format!("{}[{}].fields[{}]", self.location, i, j);
                    errors.insert(location, "object dependency not found".to_owned());
                    j += 1;
                    continue;
                }
                // other check ...
                j += 1;
            }
        }
        Self::check(&errors)
    }
}

#[derive(Default)]
pub struct DataFieldValidator {
    pub location: String,
    pub ids: Vec<String>,
}

impl VisitEntity<DataField> for DataFieldValidator {
    fn visit(&mut self, field: &DataField) {
        self.ids.push(field.get_id())
    }
}

impl Validate for DataFieldValidator {
    fn new(location: &str) -> Self {
        DataFieldValidator {
            location: location.to_owned(),
            ..Default::default()
        }
    }

    fn validate(&mut self) -> Result<()> {
        let errors = validate_ids_with_predicate(
            &self.ids,
            &self.location,
            DATA_FIELD_ENTITY_ID_FIELD,
            "empty",
            &is_non_empty,
        );
        Self::check(&errors)?;
        let errors = validate_ids_with_predicate(
            &self.ids,
            &self.location,
            DATA_FIELD_ENTITY_ID_FIELD,
            "use snake_case",
            &is_snake_lower_case,
        );
        Self::check(&errors)?;
        let errors = validate_ids_uniqueness(
            &self.ids,
            &self.location,
            DATA_FIELD_ENTITY_ID_FIELD,
            "duplicate",
        );
        Self::check(&errors)
    }
}

#[derive(Default)]
pub struct ContextStoreIdValidator {
    pub location: String,
    pub ids: Vec<String>,
}

impl VisitEntity<ContextStore> for ContextStoreIdValidator {
    fn visit(&mut self, cstore: &ContextStore) {
        self.ids.push(cstore.get_id())
    }
}

impl Validate for ContextStoreIdValidator {
    fn new(location: &str) -> Self {
        ContextStoreIdValidator {
            location: location.to_owned(),
            ..Default::default()
        }
    }

    fn validate(&mut self) -> Result<()> {
        // camel case validation
        let errors = validate_ids_with_predicate(
            &self.ids,
            &self.location,
            CONTEXT_STORE_ENTITY_ID_FIELD,
            "use snake_case",
            &is_snake_lower_case,
        );
        Self::check(&errors)?;
        let errors = validate_ids_uniqueness(
            &self.ids,
            &self.location,
            CONTEXT_STORE_ENTITY_ID_FIELD,
            "duplicated",
        );
        Self::check(&errors)
    }
}

pub struct AppValidator {
    app: Option<App>,
}

impl VisitEntity<App> for AppValidator {
    fn visit(&mut self, app: &App) {
        self.app = Some(app.to_owned())
    }
}

impl Validate for AppValidator {
    fn new(_location: &str) -> Self {
        AppValidator { app: None }
    }

    fn validate(&mut self) -> Result<()> {
        self.validate_pipes()?;
        self.validate_objects()?;
        self.validate_cstores()
    }
}

impl AppValidator {
    fn get_app(&self) -> &App {
        self.app.as_ref().unwrap()
    }

    fn validate_entities<T: EntityAccept<V>, V: Validate + VisitEntity<T>>(
        items: &[T],
        location: &str,
    ) -> Result<()> {
        let mut validator: V = V::new(location);
        for item in items {
            item.accept_entity_visitor(&mut validator);
        }
        validator.validate()
    }

    pub fn validate_pipes(&self) -> Result<()> {
        let pipes = self.get_app().get_pipes();
        Self::validate_entities::<Pipe, PipeIdValidator>(pipes, "pipes")?;
        Self::validate_entities::<Pipe, PipeTypeValidator>(pipes, "pipes")?;
        Self::validate_entities::<Pipe, PipeOutputValidator>(pipes, "pipes")?;
        Self::validate_entities::<Pipe, PipeGraphValidator>(pipes, "pipes")
    }

    pub fn validate_objects(&self) -> Result<()> {
        let objects = self.get_app().get_objects();
        Self::validate_entities::<Object, ObjectIdValidator>(objects, "objects")?;
        for i in 0..objects.len() {
            let object = objects.get(i).unwrap();
            let location = format!("objects[{}].fields", i);
            Self::validate_entities::<DataField, DataFieldValidator>(
                object.get_fields(),
                &location,
            )?;
        }
        Self::validate_entities::<Object, ObjectDependencyValidator>(objects, "objects")?;
        Ok(())
    }

    pub fn validate_cstores(&self) -> Result<()> {
        let cstores = self.get_app().get_context_stores();
        Self::validate_entities::<ContextStore, ContextStoreIdValidator>(cstores, "cstores")?;
        Ok(())
    }
}

fn validate_ids_with_predicate(
    ids: &[String],
    location: &str,
    id_field: &str,
    error_msg: &str,
    predicate: &dyn Fn(&str) -> bool,
) -> HashMap<String, String> {
    let mut errors: HashMap<String, String> = HashMap::new();
    for i in 0..ids.len() {
        let id = ids.get(i).unwrap();
        if !predicate(id) {
            let location = format!("{}[{}].{}", location, i, id_field);
            errors.insert(location, error_msg.to_owned());
        }
    }
    errors
}

fn validate_ids_uniqueness(
    ids: &[String],
    location: &str,
    id_field: &str,
    error_msg: &str,
) -> HashMap<String, String> {
    let mut errors: HashMap<String, String> = HashMap::new();
    let mut id_set: HashSet<String> = HashSet::new();
    for i in 0..ids.len() {
        let id = ids.get(i).unwrap();
        if id_set.contains(id) {
            let location = format!("{}[{}].{}", location, i, id_field);
            errors.insert(location, error_msg.to_owned());
            continue;
        }
        id_set.insert(id.to_owned());
    }
    errors
}

fn is_non_empty(s: &str) -> bool {
    !s.is_empty()
}

fn is_snake_lower_case(s: &str) -> bool {
    is_snake_case(s, false)
}

fn is_snake_case(s: &str, uppercase: bool) -> bool {
    // no leading underscore
    let mut underscore = true;
    let mut initial_char = true;
    for c in s.chars() {
        if initial_char && !c.is_ascii() {
            return false;
        }
        initial_char = false;
        if c.is_numeric() {
            underscore = false;
            continue;
        }
        if c.is_ascii() && c.is_ascii_uppercase() == uppercase {
            underscore = false;
            continue;
        }
        if c == '_' {
            if underscore {
                // consecutive underscore
                return false;
            }
            underscore = true;
            continue;
        }
        return false;
    }
    true
}

fn is_camel_case(s: &str) -> bool {
    let mut uppercase = false;
    let mut initial_char = true;
    for c in s.chars() {
        if initial_char && !c.is_ascii_uppercase() {
            // initial uppercase
            return false;
        }
        initial_char = false;
        if !(c.is_ascii() || c.is_numeric()) {
            return false;
        }
        if c.is_ascii_uppercase() {
            // no concecutive upper case
            if uppercase {
                return false;
            }
            uppercase = true;
            continue;
        }
        uppercase = false;
    }
    true
}

#[cfg(test)]
mod tests {

    use crate::models::App;
    use std::path::Path;

    #[test]
    fn test_bad_name_case_pipe() {
        let manifest_path = Path::new("resources/manifest/bad_name_case_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_duplicate_name_pipe() {
        let manifest_path = Path::new("resources/manifest/duplicate_name_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_invalid_source_dependency_pipe() {
        let manifest_path = Path::new("resources/manifest/invalid_source_dependency_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_non_exists_upstream_pipe() {
        let manifest_path = Path::new("resources/manifest/non_exists_upstream_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_no_upstream_downstream_pipe() {
        let manifest_path = Path::new("resources/manifest/no_upstream_downstream_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_cycle_dependency_pipe() {
        let manifest_path = Path::new("resources/manifest/cycle_dependency_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_bad_object_ty_case_pipe() {
        let manifest_path = Path::new("resources/manifest/bad_object_ty_case_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_duplicate_object_ty_pipe() {
        let manifest_path = Path::new("resources/manifest/duplicate_object_ty_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_unnamed_data_field_pipe() {
        let manifest_path = Path::new("resources/manifest/unnamed_data_field_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_duplicate_data_field_name_pipe() {
        let manifest_path = Path::new("resources/manifest/duplicate_data_field_name_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_non_exists_object_pipe() {
        let manifest_path = Path::new("resources/manifest/non_exists_object_pipe.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_invalid_exporter_output() {
        let manifest_path = Path::new("resources/manifest/invalid_exporter_output.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }

    #[test]
    fn test_pipe_output_not_found() {
        let manifest_path = Path::new("resources/manifest/pipe_output_not_found.yml");
        let app = App::from_path(manifest_path).unwrap();
        let e = app.validate().expect_err("expect invalid");
        println!("{}", e)
    }
}