LCOV - code coverage report
Current view: top level - entities/converter - PatchSetProtoConverter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 38 38 100.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          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.common.collect.ImmutableList;
      18             : import com.google.errorprone.annotations.Immutable;
      19             : import com.google.gerrit.entities.Account;
      20             : import com.google.gerrit.entities.PatchSet;
      21             : import com.google.gerrit.proto.Entities;
      22             : import com.google.protobuf.Parser;
      23             : import java.time.Instant;
      24             : import java.util.List;
      25             : import org.eclipse.jgit.lib.ObjectId;
      26             : 
      27         104 : @Immutable
      28         104 : public enum PatchSetProtoConverter implements ProtoConverter<Entities.PatchSet, PatchSet> {
      29         104 :   INSTANCE;
      30             : 
      31         104 :   private final ProtoConverter<Entities.PatchSet_Id, PatchSet.Id> patchSetIdConverter =
      32             :       PatchSetIdProtoConverter.INSTANCE;
      33         104 :   private final ProtoConverter<Entities.ObjectId, ObjectId> objectIdConverter =
      34             :       ObjectIdProtoConverter.INSTANCE;
      35         104 :   private final ProtoConverter<Entities.Account_Id, Account.Id> accountIdConverter =
      36             :       AccountIdProtoConverter.INSTANCE;
      37             : 
      38             :   @Override
      39             :   public Entities.PatchSet toProto(PatchSet patchSet) {
      40             :     Entities.PatchSet.Builder builder =
      41         104 :         Entities.PatchSet.newBuilder()
      42         104 :             .setId(patchSetIdConverter.toProto(patchSet.id()))
      43         104 :             .setCommitId(objectIdConverter.toProto(patchSet.commitId()))
      44         104 :             .setUploaderAccountId(accountIdConverter.toProto(patchSet.uploader()))
      45         104 :             .setCreatedOn(patchSet.createdOn().toEpochMilli());
      46         104 :     List<String> groups = patchSet.groups();
      47         104 :     if (!groups.isEmpty()) {
      48         104 :       builder.setGroups(PatchSet.joinGroups(groups));
      49             :     }
      50         104 :     patchSet.pushCertificate().ifPresent(builder::setPushCertificate);
      51         104 :     patchSet.description().ifPresent(builder::setDescription);
      52         104 :     return builder.build();
      53             :   }
      54             : 
      55             :   @Override
      56             :   public PatchSet fromProto(Entities.PatchSet proto) {
      57             :     PatchSet.Builder builder =
      58         101 :         PatchSet.builder()
      59         101 :             .id(patchSetIdConverter.fromProto(proto.getId()))
      60         101 :             .groups(
      61         101 :                 proto.hasGroups() ? PatchSet.splitGroups(proto.getGroups()) : ImmutableList.of());
      62         101 :     if (proto.hasPushCertificate()) {
      63           1 :       builder.pushCertificate(proto.getPushCertificate());
      64             :     }
      65         101 :     if (proto.hasDescription()) {
      66          24 :       builder.description(proto.getDescription());
      67             :     }
      68             : 
      69             :     // The following fields used to theoretically be nullable in PatchSet, but in practice no
      70             :     // production codepath should have ever serialized an instance that was missing one of these
      71             :     // fields.
      72             :     //
      73             :     // However, since some protos may theoretically be missing these fields, we need to support
      74             :     // them. Populate specific sentinel values for each field as documented in the PatchSet javadoc.
      75             :     // Callers that encounter one of these sentinels will likely fail, for example by failing to
      76             :     // look up the zeroId. They would have also failed back when the fields were nullable, for
      77             :     // example with NPE; the current behavior just fails slightly differently.
      78         101 :     builder
      79         101 :         .commitId(
      80         101 :             proto.hasCommitId()
      81         101 :                 ? objectIdConverter.fromProto(proto.getCommitId())
      82           1 :                 : ObjectId.zeroId())
      83         101 :         .uploader(
      84         101 :             proto.hasUploaderAccountId()
      85         101 :                 ? accountIdConverter.fromProto(proto.getUploaderAccountId())
      86           1 :                 : Account.id(0))
      87         101 :         .createdOn(
      88         101 :             proto.hasCreatedOn() ? Instant.ofEpochMilli(proto.getCreatedOn()) : Instant.EPOCH);
      89             : 
      90         101 :     return builder.build();
      91             :   }
      92             : 
      93             :   @Override
      94             :   public Parser<Entities.PatchSet> getParser() {
      95         100 :     return Entities.PatchSet.parser();
      96             :   }
      97             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750