0

I am part of a team that use Protostuff to read/write data. This is the mergeFrom method of a class extends from a Schema class:

@Override
    public void mergeFrom(Input input, Entity entity) throws IOException {
        for(int number = input.readFieldNumber(this); ; number = input.readFieldNumber(this))
        {
            if ( number == 0 ) {
                break;
            } else if ( number == FIELD_DEFINITION_ID ) {
                Schema idmSchema = entity.getSchema();

                // Definition id
                int definitionId = input.readUInt32();
                NodeDefinition defn = idmSchema.getById(definitionId);
                if ( defn == null ) {
                    throw new ProtostuffException("Invalid definition id "+definitionId);
                }
                Node<?> node = defn.createNode();
                entity.add(node);

                // Node
                readAndCheckFieldNumber(input, FIELD_NODE);
                input.mergeObject(node, getSchema(node.getClass()));


            } else if ( number == FIELD_CHILD_NODE_STATE ){
                //Node state
                int intState = input.readInt32();
                State state = State.parseState(intState);
                readAndCheckFieldNumber(input, FIELD_CHILD_DEFINITION_ID);
                int childDefnId = input.readInt32();
                Schema schema = entity.getSchema();
                NodeDefinition childDefn = schema.getById(childDefnId);
                entity.childStates.put(childDefn.getName(), state);
            } else {
                throw new ProtostuffException("Unexpected field number");
            }
        }
    }

My current task is now to skipp reading the data, where the schema of that data is already deleted. I am looking at handleUnknownField method of Input interface.

Is there any one had already experienced with this?

komelgman
  • 6,949
  • 2
  • 18
  • 18
swdev
  • 4,997
  • 8
  • 64
  • 106

1 Answers1

1

The solution is very basic indeed. Looking at this reference : Schema, and inspecting the mergeFrom code :

public void mergeFrom(Input input, User user) throws IOException
{
    while(true)
    {
        int number = input.readFieldNumber(this);
        switch(number)
        {
            case 0:
                return;
            case 1:
                user.setEmail(input.readString());
                break;
            case 2:
                user.setFirstName(input.readString());
                break;
            case 3:
                user.setLastName(input.readString());
                break;
            case 4:
                if(message.friends == null)
                    message.friends = new ArrayList<User>();
                message.friends.add(input.mergeObject(null, this));
                break;
            default:
                input.handleUnknownField(number, this);
        }
    }
}

I think I should just use the input.handleUnknownField(number, this);. And it solve my problem correctly, this way :

...
} else if ( number == FIELD_DEFINITION_ID ) {
            Schema idmSchema = entity.getSchema();

            // Definition id
            int definitionId = input.readUInt32();
            NodeDefinition defn = idmSchema.getById(definitionId);
            if ( defn == null ) {
                input.handleUnknownField(number, this);
                continue;
            }
            Node<?> node = defn.createNode();
            entity.add(node);

            // Node
            readAndCheckFieldNumber(input, FIELD_NODE);
            input.mergeObject(node, getSchema(node.getClass()));

        }
...

Thanks, I hope this QA benefit to others!

swdev
  • 4,997
  • 8
  • 64
  • 106