vendredi 21 décembre 2018

Converting between protobuf messages

I have two protobuff models, called Car and MotorCar, too big to share. I receive one message in the Car format and I need to convert it to the MotorCar format and send it on. They contain some similar fields and nested objects with same names and some which are completely different i.e MotorCar has a concept of Engine, car does not but I need to look it up to provide it, based on the car model. I am unsure what approach to take for doing this work. Do I just use a mapper and fill out the extra objects that way:

public class Mapper {

    public MotorCar from (Car carMessage) {
        MotorCar.Builder motorCar = MotorCar.newBuilder();
        motorCar.setModel(carMessage.getModelName());
        ...
        motorCar.setEngine(getEngine(carMessage.getModelName()))
        return motorCar.build()
    }

    private Engine getEngine(String model) {
        ...
    }
}

My issue with this is that it will create a massive class with lots of setters. I have looked at the adapter pattern but I am unsure how to implement it for this scenario of protobuff messages without an interface class.

Any help appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire