Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optionalOf serialize bug #31

Open
xiangxn opened this issue Jun 27, 2020 · 1 comment
Open

optionalOf serialize bug #31

xiangxn opened this issue Jun 27, 2020 · 1 comment

Comments

@xiangxn
Copy link

xiangxn commented Jun 27, 2020

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
Copy link
Contributor

fzxu commented Jun 27, 2020

PR is welcomed!

xiangxn added a commit to xiangxn/eosdart that referenced this issue Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants