|
| 1 | +part of 'schema.dart'; |
| 2 | + |
| 3 | +/// Defers resolving another schema until parse, validation, or encode time. |
| 4 | +/// |
| 5 | +/// This enables recursive schema graphs where a child schema needs to refer |
| 6 | +/// back to an outer schema that is assigned after construction. |
| 7 | +@immutable |
| 8 | +final class LazySchema<Boundary extends Object, Runtime extends Object> |
| 9 | + extends AckSchema<Boundary, Runtime> |
| 10 | + with FluentSchema<Boundary, Runtime, LazySchema<Boundary, Runtime>> { |
| 11 | + LazySchema( |
| 12 | + this.name, |
| 13 | + this._builder, { |
| 14 | + super.isNullable, |
| 15 | + super.isOptional, |
| 16 | + super.description, |
| 17 | + super.constraints, |
| 18 | + super.refinements, |
| 19 | + }); |
| 20 | + |
| 21 | + /// Human-readable name for this deferred schema reference. |
| 22 | + final String name; |
| 23 | + |
| 24 | + final AckSchema<Boundary, Runtime> Function() _builder; |
| 25 | + |
| 26 | + late final AckSchema<Boundary, Runtime> _target = _builder(); |
| 27 | + |
| 28 | + @override |
| 29 | + SchemaType get schemaType => SchemaType.lazy; |
| 30 | + |
| 31 | + @override |
| 32 | + @protected |
| 33 | + SchemaResult<Runtime> parseWithContext(Object? value, SchemaContext context) { |
| 34 | + final nullResult = handleNullInput(value, context); |
| 35 | + if (nullResult != null) return nullResult; |
| 36 | + |
| 37 | + final result = _target.parseWithContext(value, context); |
| 38 | + if (result.isFail) return SchemaResult.fail(result.getError()); |
| 39 | + |
| 40 | + final runtime = result.getOrNull(); |
| 41 | + if (runtime == null) return SchemaResult.ok(null); |
| 42 | + return applyConstraintsAndRefinements(runtime, context); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + @protected |
| 47 | + SchemaResult<Runtime> validateRuntimeWithContext( |
| 48 | + Object? value, |
| 49 | + SchemaContext context, |
| 50 | + ) { |
| 51 | + final nullResult = handleNullInput(value, context); |
| 52 | + if (nullResult != null) return nullResult; |
| 53 | + |
| 54 | + final result = _target.validateRuntimeWithContext(value, context); |
| 55 | + if (result.isFail) return SchemaResult.fail(result.getError()); |
| 56 | + |
| 57 | + final runtime = result.getOrNull(); |
| 58 | + if (runtime == null) return SchemaResult.ok(null); |
| 59 | + return applyConstraintsAndRefinements(runtime, context); |
| 60 | + } |
| 61 | + |
| 62 | + @override |
| 63 | + @protected |
| 64 | + SchemaResult<Boundary> encodeWithContext( |
| 65 | + Runtime value, |
| 66 | + SchemaContext context, |
| 67 | + ) { |
| 68 | + final validated = validateRuntimeWithContext(value, context); |
| 69 | + if (validated.isFail) return SchemaResult.fail(validated.getError()); |
| 70 | + |
| 71 | + final runtime = validated.getOrNull(); |
| 72 | + if (runtime == null) return SchemaResult.ok(null); |
| 73 | + return _target.encodeWithContext(runtime, context); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + LazySchema<Boundary, Runtime> copyWith({ |
| 78 | + bool? isNullable, |
| 79 | + bool? isOptional, |
| 80 | + String? description, |
| 81 | + List<Constraint<Runtime>>? constraints, |
| 82 | + List<Refinement<Runtime>>? refinements, |
| 83 | + }) { |
| 84 | + return LazySchema<Boundary, Runtime>( |
| 85 | + name, |
| 86 | + _builder, |
| 87 | + isNullable: isNullable ?? this.isNullable, |
| 88 | + isOptional: isOptional ?? this.isOptional, |
| 89 | + description: description ?? this.description, |
| 90 | + constraints: constraints ?? this.constraints, |
| 91 | + refinements: refinements ?? this.refinements, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @override |
| 96 | + Map<String, Object?> toMap() => {...super.toMap(), 'name': name}; |
| 97 | + |
| 98 | + @override |
| 99 | + bool operator ==(Object other) { |
| 100 | + if (identical(this, other)) return true; |
| 101 | + if (other is! LazySchema<Boundary, Runtime>) return false; |
| 102 | + return baseFieldsEqual(other) && |
| 103 | + name == other.name && |
| 104 | + identical(_builder, other._builder); |
| 105 | + } |
| 106 | + |
| 107 | + @override |
| 108 | + int get hashCode { |
| 109 | + return Object.hash(baseFieldsHashCode, name, identityHashCode(_builder)); |
| 110 | + } |
| 111 | +} |
0 commit comments