primes-network / eosdart

EOS API Client in Dart Language
MIT License
32 stars 28 forks source link

optionalOf serialize bug #31

Open xiangxn opened 4 years ago

xiangxn commented 4 years ago

Data of type "string?" cannot be serialized when it is null.

void serializeStruct(Type self, SerialBuffer buffer, Object data,
{SerializerState state, allowExtensions = true}) {
  if (state == null) state = SerializerState();
  // try {
  if (self.base != null) {
    self.base.serialize(self.base, buffer, data,
        state: state, allowExtensions: allowExtensions);
  }
  var dy = data as dynamic;
  for (var field in self.fields) {
    if (dy[field.name] != null) {
      if (state.skippedBinaryExtension) {
        throw 'unexpected ' + self.name + '.' + field.name;
      }
      field.type.serialize(field.type, buffer, dy[field.name],
          state: state,
          allowExtensions:
              allowExtensions && field == self.fields[self.fields.length - 1]);
    } else {
      if (allowExtensions && field.type.extensionOf != null) {
        state.skippedBinaryExtension = true;
      } else if(field.type.optionalOf != null ) {
        field.type.serialize(field.type, buffer, dy[field.name],state: state);
      } else {
        throw 'missing ' +
            self.name +
            '.' +
            field.name +
            ' (type=' +
            field.type.name +
            ')';
      }
    }
  }

to:

void serializeStruct(Type self, SerialBuffer buffer, Object data,
    {SerializerState state, allowExtensions = true}) {
  if (state == null) state = SerializerState();
  // try {
  if (self.base != null) {
    self.base.serialize(self.base, buffer, data,
        state: state, allowExtensions: allowExtensions);
  }
  var dy = data as dynamic;
  for (var field in self.fields) {
    if (dy[field.name] != null) {
      if (state.skippedBinaryExtension) {
        throw 'unexpected ' + self.name + '.' + field.name;
      }
      field.type.serialize(field.type, buffer, dy[field.name],
          state: state,
          allowExtensions:
              allowExtensions && field == self.fields[self.fields.length - 1]);
    } else {
      if (allowExtensions && field.type.extensionOf != null) {
        state.skippedBinaryExtension = true;
      } else {
        throw 'missing ' +
            self.name +
            '.' +
            field.name +
            ' (type=' +
            field.type.name +
            ')';
      }
    }
  }
fzxu commented 4 years ago

PR is welcomed!