Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.entities.converter; 16 : 17 : import com.google.errorprone.annotations.Immutable; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.entities.PatchSetApproval; 20 : import com.google.gerrit.proto.Entities; 21 : import com.google.protobuf.Parser; 22 : import java.time.Instant; 23 : import java.util.Objects; 24 : 25 104 : @Immutable 26 104 : public enum PatchSetApprovalProtoConverter 27 : implements ProtoConverter<Entities.PatchSetApproval, PatchSetApproval> { 28 104 : INSTANCE; 29 : 30 104 : private final ProtoConverter<Entities.PatchSetApproval_Key, PatchSetApproval.Key> 31 : patchSetApprovalKeyProtoConverter = PatchSetApprovalKeyProtoConverter.INSTANCE; 32 104 : private final ProtoConverter<Entities.Account_Id, Account.Id> accountIdConverter = 33 : AccountIdProtoConverter.INSTANCE; 34 : 35 : @Override 36 : public Entities.PatchSetApproval toProto(PatchSetApproval patchSetApproval) { 37 : Entities.PatchSetApproval.Builder builder = 38 69 : Entities.PatchSetApproval.newBuilder() 39 69 : .setKey(patchSetApprovalKeyProtoConverter.toProto(patchSetApproval.key())) 40 69 : .setValue(patchSetApproval.value()) 41 69 : .setGranted(patchSetApproval.granted().toEpochMilli()) 42 69 : .setPostSubmit(patchSetApproval.postSubmit()) 43 69 : .setCopied(patchSetApproval.copied()); 44 : 45 69 : patchSetApproval.uuid().ifPresent(uuid -> builder.setUuid(uuid.get())); 46 69 : patchSetApproval.tag().ifPresent(builder::setTag); 47 69 : Account.Id realAccountId = patchSetApproval.realAccountId(); 48 : // PatchSetApproval#getRealAccountId automatically delegates to PatchSetApproval#getAccountId if 49 : // the real author is not set. However, the previous protobuf representation kept 50 : // 'realAccountId' empty if it wasn't set. To ensure binary compatibility, simulate the previous 51 : // behavior. 52 69 : if (realAccountId != null && !Objects.equals(realAccountId, patchSetApproval.accountId())) { 53 4 : builder.setRealAccountId(accountIdConverter.toProto(realAccountId)); 54 : } 55 : 56 69 : return builder.build(); 57 : } 58 : 59 : @Override 60 : public PatchSetApproval fromProto(Entities.PatchSetApproval proto) { 61 : PatchSetApproval.Builder builder = 62 67 : PatchSetApproval.builder() 63 67 : .key(patchSetApprovalKeyProtoConverter.fromProto(proto.getKey())) 64 67 : .value(proto.getValue()) 65 67 : .granted(Instant.ofEpochMilli(proto.getGranted())) 66 67 : .postSubmit(proto.getPostSubmit()) 67 67 : .copied(proto.getCopied()); 68 67 : if (proto.hasUuid()) { 69 67 : builder.uuid(PatchSetApproval.uuid(proto.getUuid())); 70 : } 71 67 : if (proto.hasTag()) { 72 54 : builder.tag(proto.getTag()); 73 : } 74 67 : if (proto.hasRealAccountId()) { 75 4 : builder.realAccountId(accountIdConverter.fromProto(proto.getRealAccountId())); 76 : } 77 67 : return builder.build(); 78 : } 79 : 80 : @Override 81 : public Parser<Entities.PatchSetApproval> getParser() { 82 65 : return Entities.PatchSetApproval.parser(); 83 : } 84 : }