serializeVariant function

dynamic serializeVariant(
  1. Type self,
  2. SerialBuffer buffer,
  3. dynamic data, {
  4. SerializerState? state,
  5. dynamic allowExtensions = true,
})

Implementation

serializeVariant(Type self, SerialBuffer buffer, dynamic data, {SerializerState? state, allowExtensions = true}) {
  if (state == null) state = SerializerState();
  if (!(data is List) || data.length != 2 || !(data[0] is String)) {
    throw 'expected variant: ["type", value]';
  }
  var a = (data[0]) as String;
  var b = (data[1]) as Object;
  var i = self.fields.indexWhere((field) => field.name == a);
  if (i < 0) {
    throw 'type "$b" is not valid for variant';
  }
  buffer.pushVaruint32(i);
  self.fields[i].type!.serialize?.call(self.fields[i].type, buffer, b, state: state, allowExtensions: allowExtensions);
}