|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'helper.dart'; |
| 6 | + |
| 7 | +const _typeKey = 'type'; |
| 8 | +const _valueKey = 'value'; |
| 9 | + |
| 10 | +/// A constant value that can be recorded and serialized. |
| 11 | +/// |
| 12 | +/// This supports basic constants such as [bool]s or [int]s, as well as |
| 13 | +/// [ListConstant], [MapConstant] or [InstanceConstant] for more complex |
| 14 | +/// structures. |
| 15 | +/// |
| 16 | +/// This follows the AST constant concept from the Dart SDK. |
| 17 | +sealed class Constant { |
| 18 | + /// Creates a [Constant] object. |
| 19 | + const Constant(); |
| 20 | + |
| 21 | + /// Converts this [Constant] object to a JSON representation. |
| 22 | + /// |
| 23 | + /// [constants] needs to be passed, as the [Constant]s are normalized and |
| 24 | + /// stored separately in the JSON. |
| 25 | + Map<String, Object?> toJson(Map<Constant, int> constants); |
| 26 | + |
| 27 | + /// Converts this [Constant] to the value it represents. |
| 28 | + Object? toValue() => switch (this) { |
| 29 | + NullConstant() => null, |
| 30 | + PrimitiveConstant p => p.value, |
| 31 | + ListConstant<Constant> l => l.value.map((c) => c.toValue()).toList(), |
| 32 | + MapConstant<Constant> m => m.value.map( |
| 33 | + (key, value) => MapEntry(key, value.toValue()), |
| 34 | + ), |
| 35 | + InstanceConstant i => i.fields.map( |
| 36 | + (key, value) => MapEntry(key, value.toValue()), |
| 37 | + ), |
| 38 | + }; |
| 39 | + |
| 40 | + /// Creates a [Constant] object from its JSON representation. |
| 41 | + /// |
| 42 | + /// [constants] needs to be passed, as the [Constant]s are normalized and |
| 43 | + /// stored separately in the JSON. |
| 44 | + static Constant fromJson( |
| 45 | + Map<String, Object?> value, |
| 46 | + List<Constant> constants, |
| 47 | + ) => switch (value[_typeKey] as String) { |
| 48 | + NullConstant._type => const NullConstant(), |
| 49 | + BoolConstant._type => BoolConstant(value[_valueKey] as bool), |
| 50 | + IntConstant._type => IntConstant(value[_valueKey] as int), |
| 51 | + StringConstant._type => StringConstant(value[_valueKey] as String), |
| 52 | + ListConstant._type => ListConstant( |
| 53 | + (value[_valueKey] as List<dynamic>) |
| 54 | + .map((value) => value as int) |
| 55 | + .map((value) => constants[value]) |
| 56 | + .toList(), |
| 57 | + ), |
| 58 | + MapConstant._type => MapConstant( |
| 59 | + (value[_valueKey] as Map<String, Object?>).map( |
| 60 | + (key, value) => MapEntry(key, constants[value as int]), |
| 61 | + ), |
| 62 | + ), |
| 63 | + InstanceConstant._type => InstanceConstant( |
| 64 | + fields: (value[_valueKey] as Map<String, Object?>).map( |
| 65 | + (key, value) => MapEntry(key, constants[value as int]), |
| 66 | + ), |
| 67 | + ), |
| 68 | + String() => |
| 69 | + throw UnimplementedError('This type is not a supported constant'), |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +/// Represents the `null` constant value. |
| 74 | +final class NullConstant extends Constant { |
| 75 | + /// The type identifier for JSON serialization. |
| 76 | + static const _type = 'Null'; |
| 77 | + |
| 78 | + /// Creates a [NullConstant] object. |
| 79 | + const NullConstant() : super(); |
| 80 | + |
| 81 | + @override |
| 82 | + Map<String, Object?> toJson(Map<Constant, int> constants) => |
| 83 | + _toJson(_type, null); |
| 84 | + |
| 85 | + @override |
| 86 | + bool operator ==(Object other) => other is NullConstant; |
| 87 | + |
| 88 | + @override |
| 89 | + int get hashCode => 0; |
| 90 | +} |
| 91 | + |
| 92 | +/// Represents a constant value of a primitive type. |
| 93 | +sealed class PrimitiveConstant<T extends Object> extends Constant { |
| 94 | + /// The underlying value of this constant. |
| 95 | + final T value; |
| 96 | + |
| 97 | + /// Creates a [PrimitiveConstant] object with the given [value]. |
| 98 | + const PrimitiveConstant(this.value); |
| 99 | + |
| 100 | + @override |
| 101 | + int get hashCode => value.hashCode; |
| 102 | + |
| 103 | + @override |
| 104 | + bool operator ==(Object other) { |
| 105 | + if (identical(this, other)) return true; |
| 106 | + |
| 107 | + return other is PrimitiveConstant<T> && other.value == value; |
| 108 | + } |
| 109 | + |
| 110 | + @override |
| 111 | + Map<String, Object?> toJson(Map<Constant, int> constants) => valueToJson(); |
| 112 | + |
| 113 | + /// Converts this primitive constant to a JSON representation. |
| 114 | + Map<String, Object?> valueToJson(); |
| 115 | +} |
| 116 | + |
| 117 | +/// Represents a constant boolean value. |
| 118 | +final class BoolConstant extends PrimitiveConstant<bool> { |
| 119 | + /// The type identifier for JSON serialization. |
| 120 | + static const _type = 'bool'; |
| 121 | + |
| 122 | + /// Creates a [BoolConstant] object with the given boolean [value]. |
| 123 | + const BoolConstant(super.value); |
| 124 | + |
| 125 | + @override |
| 126 | + Map<String, Object?> valueToJson() => _toJson(_type, value); |
| 127 | +} |
| 128 | + |
| 129 | +/// Represents a constant integer value. |
| 130 | +final class IntConstant extends PrimitiveConstant<int> { |
| 131 | + /// The type identifier for JSON serialization. |
| 132 | + static const _type = 'int'; |
| 133 | + |
| 134 | + /// Creates an [IntConstant] object with the given integer [value]. |
| 135 | + const IntConstant(super.value); |
| 136 | + |
| 137 | + @override |
| 138 | + Map<String, Object?> valueToJson() => _toJson(_type, value); |
| 139 | +} |
| 140 | + |
| 141 | +/// Represents a constant string value. |
| 142 | +final class StringConstant extends PrimitiveConstant<String> { |
| 143 | + /// The type identifier for JSON serialization. |
| 144 | + static const _type = 'String'; |
| 145 | + |
| 146 | + /// Creates a [StringConstant] object with the given string [value]. |
| 147 | + const StringConstant(super.value); |
| 148 | + |
| 149 | + @override |
| 150 | + Map<String, Object?> valueToJson() => _toJson(_type, value); |
| 151 | +} |
| 152 | + |
| 153 | +/// Represents a constant list of [Constant] values. |
| 154 | +final class ListConstant<T extends Constant> extends Constant { |
| 155 | + /// The type identifier for JSON serialization. |
| 156 | + static const _type = 'list'; |
| 157 | + |
| 158 | + /// The underlying list of constant values. |
| 159 | + final List<T> value; |
| 160 | + |
| 161 | + /// Creates a [ListConstant] object with the given list of [value]s. |
| 162 | + const ListConstant(this.value); |
| 163 | + |
| 164 | + @override |
| 165 | + int get hashCode => deepHash(value); |
| 166 | + |
| 167 | + @override |
| 168 | + bool operator ==(Object other) { |
| 169 | + if (identical(this, other)) return true; |
| 170 | + |
| 171 | + return other is ListConstant && deepEquals(other.value, value); |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + Map<String, Object?> toJson(Map<Constant, int> constants) => |
| 176 | + _toJson(_type, value.map((constant) => constants[constant]).toList()); |
| 177 | +} |
| 178 | + |
| 179 | +/// Represents a constant map from string keys to [Constant] values. |
| 180 | +final class MapConstant<T extends Constant> extends Constant { |
| 181 | + /// The type identifier for JSON serialization. |
| 182 | + static const _type = 'map'; |
| 183 | + |
| 184 | + /// The underlying map of constant values. |
| 185 | + final Map<String, T> value; |
| 186 | + |
| 187 | + /// Creates a [MapConstant] object with the given map of [value]s. |
| 188 | + const MapConstant(this.value); |
| 189 | + |
| 190 | + @override |
| 191 | + int get hashCode => deepHash(value); |
| 192 | + |
| 193 | + @override |
| 194 | + bool operator ==(Object other) { |
| 195 | + if (identical(this, other)) return true; |
| 196 | + |
| 197 | + return other is MapConstant && deepEquals(other.value, value); |
| 198 | + } |
| 199 | + |
| 200 | + @override |
| 201 | + Map<String, Object?> toJson(Map<Constant, int> constants) => _toJson( |
| 202 | + _type, |
| 203 | + value.map((key, constant) => MapEntry(key, constants[constant]!)), |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +/// A constant instance of a class with its fields |
| 208 | +/// |
| 209 | +/// Only as far as they can also be represented by constants. This is more or |
| 210 | +/// less the same as a [MapConstant]. |
| 211 | +final class InstanceConstant extends Constant { |
| 212 | + /// The type identifier for JSON serialization. |
| 213 | + static const _type = 'Instance'; |
| 214 | + |
| 215 | + /// The fields of this instance, mapped from field name to [Constant] value. |
| 216 | + final Map<String, Constant> fields; |
| 217 | + |
| 218 | + /// Creates an [InstanceConstant] object with the given [fields]. |
| 219 | + const InstanceConstant({required this.fields}); |
| 220 | + |
| 221 | + /// Creates an [InstanceConstant] object from JSON. |
| 222 | + /// |
| 223 | + /// [json] is a map representing the JSON structure. |
| 224 | + /// [constants] is a list of [Constant] objects that are referenced by index |
| 225 | + /// in the JSON. |
| 226 | + factory InstanceConstant.fromJson( |
| 227 | + Map<String, Object?> json, |
| 228 | + List<Constant> constants, |
| 229 | + ) { |
| 230 | + return InstanceConstant( |
| 231 | + fields: json.map( |
| 232 | + (key, constantIndex) => MapEntry(key, constants[constantIndex as int]), |
| 233 | + ), |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + @override |
| 238 | + Map<String, Object?> toJson(Map<Constant, int> constants) => _toJson( |
| 239 | + _type, |
| 240 | + fields.isNotEmpty |
| 241 | + ? fields.map( |
| 242 | + (name, constantIndex) => MapEntry(name, constants[constantIndex]!), |
| 243 | + ) |
| 244 | + : null, |
| 245 | + ); |
| 246 | + |
| 247 | + @override |
| 248 | + bool operator ==(Object other) { |
| 249 | + if (identical(this, other)) return true; |
| 250 | + |
| 251 | + return other is InstanceConstant && deepEquals(other.fields, fields); |
| 252 | + } |
| 253 | + |
| 254 | + @override |
| 255 | + int get hashCode => deepHash(fields); |
| 256 | +} |
| 257 | + |
| 258 | +/// Helper to create the JSON structure of constants by storing the value with |
| 259 | +/// the type. |
| 260 | +Map<String, Object?> _toJson(String type, Object? value) { |
| 261 | + return {_typeKey: type, if (value != null) _valueKey: value}; |
| 262 | +} |
0 commit comments