LCOV - code coverage report
Current view: top level - entities/converter - ChangeMessageProtoConverter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 37 38 97.4 %
Date: 2022-11-19 15:00:39 Functions: 4 5 80.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.errorprone.annotations.Immutable;
      18             : import com.google.gerrit.entities.Account;
      19             : import com.google.gerrit.entities.ChangeMessage;
      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.Objects;
      25             : 
      26           2 : @Immutable
      27           2 : public enum ChangeMessageProtoConverter
      28             :     implements ProtoConverter<Entities.ChangeMessage, ChangeMessage> {
      29           2 :   INSTANCE;
      30             : 
      31           2 :   private final ProtoConverter<Entities.ChangeMessage_Key, ChangeMessage.Key>
      32             :       changeMessageKeyConverter = ChangeMessageKeyProtoConverter.INSTANCE;
      33           2 :   private final ProtoConverter<Entities.Account_Id, Account.Id> accountIdConverter =
      34             :       AccountIdProtoConverter.INSTANCE;
      35           2 :   private final ProtoConverter<Entities.PatchSet_Id, PatchSet.Id> patchSetIdConverter =
      36             :       PatchSetIdProtoConverter.INSTANCE;
      37             : 
      38             :   @Override
      39             :   public Entities.ChangeMessage toProto(ChangeMessage changeMessage) {
      40             :     Entities.ChangeMessage.Builder builder =
      41           2 :         Entities.ChangeMessage.newBuilder()
      42           2 :             .setKey(changeMessageKeyConverter.toProto(changeMessage.getKey()));
      43           2 :     Account.Id author = changeMessage.getAuthor();
      44           2 :     if (author != null) {
      45           2 :       builder.setAuthorId(accountIdConverter.toProto(author));
      46             :     }
      47           2 :     Instant writtenOn = changeMessage.getWrittenOn();
      48           2 :     if (writtenOn != null) {
      49           2 :       builder.setWrittenOn(writtenOn.toEpochMilli());
      50             :     }
      51             :     // Build proto with template representation of the message. Templates are parsed when message is
      52             :     // extracted from cache.
      53           2 :     String message = changeMessage.getMessage();
      54           2 :     if (message != null) {
      55           1 :       builder.setMessage(message);
      56             :     }
      57           2 :     PatchSet.Id patchSetId = changeMessage.getPatchSetId();
      58           2 :     if (patchSetId != null) {
      59           2 :       builder.setPatchset(patchSetIdConverter.toProto(patchSetId));
      60             :     }
      61           2 :     String tag = changeMessage.getTag();
      62           2 :     if (tag != null) {
      63           1 :       builder.setTag(tag);
      64             :     }
      65           2 :     Account.Id realAuthor = changeMessage.getRealAuthor();
      66             :     // ChangeMessage#getRealAuthor automatically delegates to ChangeMessage#getAuthor if the real
      67             :     // author is not set. However, the previous protobuf representation kept 'realAuthor' empty if
      68             :     // it wasn't set. To ensure binary compatibility, simulate the previous behavior.
      69           2 :     if (realAuthor != null && !Objects.equals(realAuthor, author)) {
      70           1 :       builder.setRealAuthor(accountIdConverter.toProto(realAuthor));
      71             :     }
      72           2 :     return builder.build();
      73             :   }
      74             : 
      75             :   @Override
      76             :   public ChangeMessage fromProto(Entities.ChangeMessage proto) {
      77             :     ChangeMessage.Key key =
      78           2 :         proto.hasKey() ? changeMessageKeyConverter.fromProto(proto.getKey()) : null;
      79             :     Account.Id author =
      80           2 :         proto.hasAuthorId() ? accountIdConverter.fromProto(proto.getAuthorId()) : null;
      81           2 :     Instant writtenOn = proto.hasWrittenOn() ? Instant.ofEpochMilli(proto.getWrittenOn()) : null;
      82             :     PatchSet.Id patchSetId =
      83           2 :         proto.hasPatchset() ? patchSetIdConverter.fromProto(proto.getPatchset()) : null;
      84             :     // Only template representation of the message is stored in entity. Templates should be replaced
      85             :     // before being served to the users.
      86           2 :     String messageTemplate = proto.hasMessage() ? proto.getMessage() : null;
      87           2 :     String tag = proto.hasTag() ? proto.getTag() : null;
      88             :     Account.Id realAuthor =
      89           2 :         proto.hasRealAuthor() ? accountIdConverter.fromProto(proto.getRealAuthor()) : null;
      90           2 :     ChangeMessage changeMessage =
      91           2 :         ChangeMessage.create(key, author, writtenOn, patchSetId, messageTemplate, realAuthor, tag);
      92             : 
      93           2 :     return changeMessage;
      94             :   }
      95             : 
      96             :   @Override
      97             :   public Parser<Entities.ChangeMessage> getParser() {
      98           0 :     return Entities.ChangeMessage.parser();
      99             :   }
     100             : }

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