struct static method

BcsType<Map<String, dynamic>, dynamic> struct(
  1. String name,
  2. Map<String, BcsType> fields, [
  3. BcsTypeOptions<Map<String, dynamic>, Map<String, dynamic>>? options
])

Implementation

static BcsType<Map<String, dynamic>, dynamic> struct(
  String name,
  Map<String, BcsType> fields,
  [BcsTypeOptions<Map<String, dynamic>, Map<String, dynamic>>? options]
) {
  final canonicalOrder = fields.entries.toList();

  return BcsType<Map<String, dynamic>, dynamic>(
    name: name,
    serializedSize: (values, {BcsWriterOptions? options}) {
      int total = 0;
      for (final entry in canonicalOrder) {
        final size = entry.value.serializedSize(values[entry.key]);
        if (size == null) {
          return null;
        }
        total += size;
      }
      return total;
    },
    read: (reader) {
      final result = <String, dynamic>{};
      for (final entry in canonicalOrder) {
        result[entry.key] = entry.value.read(reader);
      }
      return result;
    },
    write: (val, writer) {
      final value = val is Map ? val : val.toJson();
      for (final entry in canonicalOrder) {
        entry.value.write(value[entry.key], writer);
      }
    },
    validate: (value) {
      options?.validate?.call(value);
    },
  );
}