hydrate-schema 0.0.2

Game asset pipeline and authoring 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
mod dynamic_array;
pub use dynamic_array::*;

mod r#enum;
pub use r#enum::*;

//mod interface;
//pub use interface::*;

mod map;
pub use map::*;

mod record;
pub use record::*;

//mod ref_constraint;
//pub use ref_constraint::*;

mod static_array;
pub use static_array::*;

use crate::{DataSetError, DataSetResult, HashMap};
use crate::{HashSet, PropertyPath, SchemaFingerprint};
use std::hash::Hash;
use std::str::FromStr;
use uuid::Uuid;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct SchemaId(u128);

#[derive(Clone, Debug)]
pub enum SchemaNamedType {
    Record(SchemaRecord),
    Enum(SchemaEnum),
}

impl SchemaNamedType {
    pub fn fingerprint(&self) -> SchemaFingerprint {
        match self {
            SchemaNamedType::Record(x) => x.fingerprint(),
            SchemaNamedType::Enum(x) => x.fingerprint(),
        }
    }

    pub fn name(&self) -> &str {
        match self {
            SchemaNamedType::Record(x) => x.name(),
            SchemaNamedType::Enum(x) => x.name(),
        }
    }

    pub fn type_uuid(&self) -> Uuid {
        match self {
            SchemaNamedType::Record(x) => x.type_uuid(),
            SchemaNamedType::Enum(x) => x.type_uuid(),
        }
    }

    pub fn as_record(&self) -> DataSetResult<&SchemaRecord> {
        Ok(self.try_as_record().ok_or(DataSetError::InvalidSchema)?)
    }

    pub fn try_as_record(&self) -> Option<&SchemaRecord> {
        match self {
            SchemaNamedType::Record(x) => Some(x),
            _ => None,
        }
    }

    pub fn as_enum(&self) -> DataSetResult<&SchemaEnum> {
        Ok(self.try_as_enum().ok_or(DataSetError::InvalidSchema)?)
    }

    pub fn try_as_enum(&self) -> Option<&SchemaEnum> {
        match self {
            SchemaNamedType::Enum(x) => Some(x),
            _ => None,
        }
    }

    // How migration works:
    // - Just about everything is stored in property paths like control_point.position.x
    // - The asset has some root named type (and it is a record)
    // - We iteratively walk through the property path, verifying that the target schema is the
    //   same type UUID, and any record field's types are interchangable (see Schema::types_are_interchangeable)

    pub fn find_post_migration_property_path(
        old_root_named_type: &SchemaNamedType,
        old_path: impl AsRef<str>,
        old_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
        new_root_named_type: &SchemaNamedType,
        new_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
        new_named_types_by_uuid: &HashMap<Uuid, SchemaFingerprint>,
    ) -> Option<String> {
        let mut old_schema = Schema::Record(old_root_named_type.fingerprint());
        let mut new_schema = Schema::Record(new_root_named_type.fingerprint());

        log::trace!("migrate property name {:?}", old_path.as_ref());
        let old_split_path = old_path.as_ref().split(".");
        let mut new_path = PropertyPath::default();

        // Iterate the path segments to find

        for old_path_segment in old_split_path {
            let new_path_segment = Schema::find_post_migration_field_name(
                &old_schema,
                old_path_segment,
                old_named_types,
                &new_schema,
                new_named_types,
                new_named_types_by_uuid,
            )?;

            new_path = new_path.push(&new_path_segment);
            let old_s = old_schema.find_field_schema(old_path_segment, old_named_types);
            let new_s = new_schema.find_field_schema(new_path_segment, new_named_types);

            if let (Some(old_s), Some(new_s)) = (old_s, new_s) {
                if !Schema::types_are_interchangeable(
                    old_s,
                    new_s,
                    old_named_types,
                    new_named_types,
                ) {
                    return None;
                }

                old_schema = old_s.clone();
                new_schema = new_s.clone();
            } else {
                return None;
            }
        }

        Some(new_path.path().to_string())
    }

    pub fn find_property_schema(
        &self,
        path: impl AsRef<str>,
        named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
    ) -> Option<Schema> {
        let mut schema = Schema::Record(self.fingerprint());

        let split_path = path.as_ref().split(".");

        // Iterate the path segments to find
        for path_segment in split_path {
            let s = schema.find_field_schema(path_segment, named_types);
            if let Some(s) = s {
                schema = s.clone();
            } else {
                return None;
            }
        }

        Some(schema)
    }
}

/// Describes format of data, either a single primitive value or complex layout comprised of
/// potentially many values
#[derive(Clone, Debug, PartialEq)]
pub enum Schema {
    /// Marks the field as possible to be null
    Nullable(Box<Schema>),
    Boolean,
    I32,
    I64,
    U32,
    U64,
    F32,
    F64,
    /// Variable amount of bytes stored within the asset
    Bytes,
    /// Variable-length UTF-8 String
    String,
    /// Fixed-size array of values
    StaticArray(SchemaStaticArray),
    DynamicArray(SchemaDynamicArray),
    Map(SchemaMap),
    AssetRef(SchemaFingerprint),
    /// Named type, it could be an enum, record, etc.
    Record(SchemaFingerprint),
    Enum(SchemaFingerprint),
}

impl Schema {
    pub fn is_nullable(&self) -> bool {
        match self {
            Schema::Nullable(_) => true,
            _ => false,
        }
    }

    pub fn is_boolean(&self) -> bool {
        match self {
            Schema::Boolean => true,
            _ => false,
        }
    }

    pub fn is_i32(&self) -> bool {
        match self {
            Schema::I32 => true,
            _ => false,
        }
    }

    pub fn is_i64(&self) -> bool {
        match self {
            Schema::I64 => true,
            _ => false,
        }
    }

    pub fn is_u32(&self) -> bool {
        match self {
            Schema::U32 => true,
            _ => false,
        }
    }

    pub fn is_u64(&self) -> bool {
        match self {
            Schema::U64 => true,
            _ => false,
        }
    }

    pub fn is_f32(&self) -> bool {
        match self {
            Schema::F32 => true,
            _ => false,
        }
    }

    pub fn is_f64(&self) -> bool {
        match self {
            Schema::F64 => true,
            _ => false,
        }
    }

    pub fn is_bytes(&self) -> bool {
        match self {
            Schema::Bytes => true,
            _ => false,
        }
    }

    pub fn is_string(&self) -> bool {
        match self {
            Schema::String => true,
            _ => false,
        }
    }

    pub fn is_static_array(&self) -> bool {
        match self {
            Schema::StaticArray(_) => true,
            _ => false,
        }
    }

    pub fn is_dynamic_array(&self) -> bool {
        match self {
            Schema::DynamicArray(_) => true,
            _ => false,
        }
    }

    pub fn is_map(&self) -> bool {
        match self {
            Schema::Map(_) => true,
            _ => false,
        }
    }

    pub fn is_asset_ref(&self) -> bool {
        match self {
            Schema::AssetRef(_) => true,
            _ => false,
        }
    }

    pub fn is_record(&self) -> bool {
        match self {
            Schema::Record(_) => true,
            _ => false,
        }
    }

    pub fn is_enum(&self) -> bool {
        match self {
            Schema::Enum(_) => true,
            _ => false,
        }
    }

    pub fn is_number(&self) -> bool {
        match self {
            Schema::I32 | Schema::I64 | Schema::U32 | Schema::U64 | Schema::F32 | Schema::F64 => {
                true
            }
            _ => false,
        }
    }

    pub fn types_are_interchangeable(
        old_parent_schema: &Schema,
        new_parent_schema: &Schema,
        old_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
        new_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
    ) -> bool {
        // Covers strings/bytes
        if old_parent_schema == new_parent_schema {
            return true;
        }

        if old_parent_schema.is_number() && new_parent_schema.is_number() {
            return true;
        }

        match old_parent_schema {
            Schema::Nullable(old_inner) => {
                //TODO: Would be nice if we could handle nullable being added/removed on existing properties
                if let Schema::Nullable(new_inner) = new_parent_schema {
                    Self::types_are_interchangeable(
                        &*old_inner,
                        &*new_inner,
                        old_named_types,
                        new_named_types,
                    )
                } else {
                    false
                }
            }
            Schema::StaticArray(old_inner) => {
                if let Schema::StaticArray(new_inner) = new_parent_schema {
                    Self::types_are_interchangeable(
                        old_inner.item_type(),
                        new_inner.item_type(),
                        old_named_types,
                        new_named_types,
                    )
                } else {
                    false
                }
            }
            Schema::DynamicArray(old_inner) => {
                if let Schema::DynamicArray(new_inner) = new_parent_schema {
                    Self::types_are_interchangeable(
                        old_inner.item_type(),
                        new_inner.item_type(),
                        old_named_types,
                        new_named_types,
                    )
                } else {
                    false
                }
            }
            Schema::Map(old_inner) => {
                if let Schema::Map(new_inner) = new_parent_schema {
                    let keys_are_interchangage = Self::types_are_interchangeable(
                        old_inner.key_type(),
                        new_inner.key_type(),
                        old_named_types,
                        new_named_types,
                    );
                    let values_are_interchangable = Self::types_are_interchangeable(
                        old_inner.value_type(),
                        new_inner.value_type(),
                        old_named_types,
                        new_named_types,
                    );
                    keys_are_interchangage && values_are_interchangable
                } else {
                    false
                }
            }
            Schema::AssetRef(_) => {
                if let Schema::AssetRef(_) = new_parent_schema {
                    // won't enforce any type constraints here, we can leave that for schema validation
                    // later, which allows users to fix any problems
                    true
                } else {
                    false
                }
            }
            Schema::Record(old_inner) => {
                if let Schema::Record(new_inner) = new_parent_schema {
                    let old_named_type = old_named_types.get(old_inner).unwrap();
                    let new_named_type = new_named_types.get(new_inner).unwrap();

                    // TODO: Could see support for specific type transformations in the future
                    old_named_type.type_uuid() == new_named_type.type_uuid()
                } else {
                    false
                }
            }
            Schema::Enum(old_inner) => {
                if let Schema::Enum(new_inner) = new_parent_schema {
                    let old_named_type = old_named_types.get(old_inner).unwrap();
                    let new_named_type = new_named_types.get(new_inner).unwrap();

                    old_named_type.type_uuid() == new_named_type.type_uuid()
                } else {
                    false
                }
            }
            _ => false,
        }
    }

    // This looks for equivalent field name in new types as existed in old types
    pub fn find_post_migration_field_name<'a>(
        old_parent_schema: &Schema,
        old_property_name: &'a str,
        old_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
        _new_parent_schema: &Schema,
        new_named_types: &HashMap<SchemaFingerprint, SchemaNamedType>,
        new_named_types_by_uuid: &HashMap<Uuid, SchemaFingerprint>,
    ) -> Option<String> {
        match old_parent_schema {
            Schema::Nullable(_) => {
                if old_property_name == "value" {
                    Some(old_property_name.to_string())
                } else {
                    None
                }
            }
            Schema::Record(old_schema_fingerprint) => {
                let old_named_type = old_named_types.get(old_schema_fingerprint).unwrap();
                let old_schema_record = old_named_type.as_record().unwrap();
                let old_field = old_schema_record
                    .find_field_from_name(old_property_name.as_ref())
                    .unwrap();
                let old_record_type_uuid = old_named_type.type_uuid();

                // This is just finding the field with same UUID. No validation here that the schemas
                // are the same.
                let new_schema_fingerprint =
                    new_named_types_by_uuid.get(&old_record_type_uuid).unwrap();
                let new_named_type = new_named_types.get(new_schema_fingerprint).unwrap();
                let new_schema_record = new_named_type.as_record().unwrap();

                // This may fail to find the new field, in which case the field is probably removed
                new_schema_record
                    .find_field_from_field_uuid(old_field.field_uuid())
                    .map(|x| x.name().to_string())
            }
            Schema::StaticArray(_) => {
                if old_property_name.parse::<u32>().is_ok() {
                    Some(old_property_name.to_string())
                } else {
                    None
                }
            }
            Schema::DynamicArray(_) => {
                // We could validate that name is a valid UUID
                Uuid::from_str(old_property_name.as_ref()).ok()?;
                Some(old_property_name.to_string())
            }
            Schema::Map(_) => {
                if old_property_name.ends_with(":key") {
                    Uuid::from_str(&old_property_name[0..old_property_name.len() - 4]).ok()?;
                    Some(old_property_name.to_string())
                } else if old_property_name.ends_with(":value") {
                    Uuid::from_str(&old_property_name[0..old_property_name.len() - 6]).ok()?;
                    Some(old_property_name.to_string())
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    // This looks for direct descendent field with given name
    pub fn find_field_schema<'a>(
        &'a self,
        name: impl AsRef<str>,
        named_types: &'a HashMap<SchemaFingerprint, SchemaNamedType>,
    ) -> Option<&'a Schema> {
        match self {
            Schema::Nullable(x) => {
                if name.as_ref() == "value" {
                    Some(&*x)
                } else {
                    // "null_value" special property name is purposefully omitted here
                    None
                }
            }
            Schema::Record(named_type_id) => {
                let named_type = named_types.get(named_type_id).unwrap();
                match named_type {
                    SchemaNamedType::Record(x) => x.field_schema(name),
                    SchemaNamedType::Enum(_) => None,
                }
            }
            Schema::StaticArray(x) => {
                if name.as_ref().parse::<u32>().is_ok() {
                    Some(x.item_type())
                } else {
                    None
                }
            }
            Schema::DynamicArray(x) => {
                // "replace" special property name is purposefully omitted here
                // We could validate that name is a valid UUID
                Uuid::from_str(name.as_ref()).ok()?;
                Some(x.item_type())
            }
            Schema::Map(x) => {
                if name.as_ref().ends_with(":key") {
                    Uuid::from_str(&name.as_ref()[0..name.as_ref().len() - 4]).ok()?;
                    Some(x.key_type())
                } else if name.as_ref().ends_with(":value") {
                    Uuid::from_str(&name.as_ref()[0..name.as_ref().len() - 6]).ok()?;
                    Some(x.value_type())
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    // Given a schema (that is likely a record with fields), depth-first search
    // it to find all the schemas that are used within it
    pub fn find_referenced_schemas<'a>(
        named_types: &'a HashMap<SchemaFingerprint, SchemaNamedType>,
        schema: &'a Schema,
        referenced_schema_fingerprints: &mut HashSet<SchemaFingerprint>,
        visit_stack: &mut Vec<&'a Schema>,
    ) {
        if visit_stack.contains(&schema) {
            return;
        }

        visit_stack.push(&schema);
        //referenced_schema_fingerprints.insert(schema)
        match schema {
            Schema::Nullable(inner) => Self::find_referenced_schemas(
                named_types,
                &*inner,
                referenced_schema_fingerprints,
                visit_stack,
            ),
            Schema::Boolean => {}
            Schema::I32 => {}
            Schema::I64 => {}
            Schema::U32 => {}
            Schema::U64 => {}
            Schema::F32 => {}
            Schema::F64 => {}
            Schema::Bytes => {}
            Schema::String => {}
            Schema::StaticArray(inner) => Self::find_referenced_schemas(
                named_types,
                inner.item_type(),
                referenced_schema_fingerprints,
                visit_stack,
            ),
            Schema::DynamicArray(inner) => Self::find_referenced_schemas(
                named_types,
                inner.item_type(),
                referenced_schema_fingerprints,
                visit_stack,
            ),
            Schema::Map(inner) => {
                Self::find_referenced_schemas(
                    named_types,
                    inner.key_type(),
                    referenced_schema_fingerprints,
                    visit_stack,
                );
                Self::find_referenced_schemas(
                    named_types,
                    inner.value_type(),
                    referenced_schema_fingerprints,
                    visit_stack,
                );
            }
            Schema::AssetRef(_) => {}
            Schema::Record(inner) => {
                referenced_schema_fingerprints.insert(*inner);
                let record = named_types.get(inner).unwrap().try_as_record().unwrap();
                for field in record.fields() {
                    Self::find_referenced_schemas(
                        named_types,
                        field.field_schema(),
                        referenced_schema_fingerprints,
                        visit_stack,
                    );
                }
            }
            Schema::Enum(inner) => {
                referenced_schema_fingerprints.insert(*inner);
            }
        }
        visit_stack.pop();
    }
}