Skip to content

Instantly share code, notes, and snippets.

@mrmeku
Last active May 31, 2018 00:12
Show Gist options
  • Save mrmeku/70d5581a7ffd9c7c654e4efc30493517 to your computer and use it in GitHub Desktop.
Save mrmeku/70d5581a7ffd9c7c654e4efc30493517 to your computer and use it in GitHub Desktop.
Example of how to extend the abstract class output of java_proto_library
package backend;
import static schema.Manufacturer.TOYOTA;
import static schema.Manufacturer.HONDA;
import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;
import schema.Car;
import schema.GetCarsRequest;
import schema.Manufacturer;
import schema.GetCarsResponse;
public class CarService extends schema.CarService {
private static final Car newCar(String model, Manufacturer manufacturer) {
return Car.newBuilder().setModel(model).setManufacturer(manufacturer).build();
}
@Override
public void getCars(
RpcController controller, GetCarsRequest request, RpcCallback<GetCarsResponse> done) {
switch (request.getManufacturer()) {
case TOYOTA:
done.run(GetCarsResponse.newBuilder()
.addCars(newCar("Corolla", TOYOTA))
.addCars(newCar("Camry", TOYOTA))
.build());
break;
case HONDA:
done.run(GetCarsResponse.newBuilder()
.addCars(newCar("Civic", HONDA))
.addCars(newCar("Accord", HONDA))
.build());
break;
default:
// UNKOWN_MANUFACTURER
done.run(GetCarsResponse.getDefaultInstance());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment