Line data Source code
1 : // Copyright (C) 2020 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.server.cache.serialize.entities; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import com.google.gerrit.entities.AccountsSection; 21 : import com.google.gerrit.entities.CachedProjectConfig; 22 : import com.google.gerrit.entities.ConfiguredMimeTypes; 23 : import com.google.gerrit.entities.PermissionRule; 24 : import com.google.gerrit.server.cache.proto.Cache; 25 : import com.google.gerrit.server.cache.serialize.ObjectIdConverter; 26 : import java.util.Optional; 27 : 28 : /** Helper to (de)serialize values for caches. */ 29 : public class CachedProjectConfigSerializer { 30 : public static CachedProjectConfig deserialize(Cache.CachedProjectConfigProto proto) { 31 : CachedProjectConfig.Builder builder = 32 16 : CachedProjectConfig.builder() 33 16 : .setProject(ProjectSerializer.deserialize(proto.getProject())) 34 16 : .setMaxObjectSizeLimit(proto.getMaxObjectSizeLimit()) 35 16 : .setCheckReceivedObjects(proto.getCheckReceivedObjects()); 36 16 : if (proto.hasBranchOrderSection()) { 37 1 : builder.setBranchOrderSection( 38 1 : Optional.of(BranchOrderSectionSerializer.deserialize(proto.getBranchOrderSection()))); 39 : } 40 16 : ImmutableList<ConfiguredMimeTypes.TypeMatcher> matchers = 41 16 : proto.getMimeTypesList().stream() 42 16 : .map(ConfiguredMimeTypeSerializer::deserialize) 43 16 : .collect(toImmutableList()); 44 16 : builder.setMimeTypes(ConfiguredMimeTypes.create(matchers)); 45 16 : if (!proto.getRulesId().isEmpty()) { 46 1 : builder.setRulesId( 47 1 : Optional.of(ObjectIdConverter.create().fromByteString(proto.getRulesId()))); 48 : } 49 16 : if (!proto.getRevision().isEmpty()) { 50 16 : builder.setRevision( 51 16 : Optional.of(ObjectIdConverter.create().fromByteString(proto.getRevision()))); 52 : } 53 16 : proto 54 16 : .getExtensionPanelsMap() 55 16 : .entrySet() 56 16 : .forEach( 57 : panelSection -> { 58 1 : builder 59 1 : .extensionPanelSectionsBuilder() 60 1 : .put( 61 1 : panelSection.getKey(), 62 1 : panelSection.getValue().getSectionList().stream().collect(toImmutableList())); 63 1 : }); 64 16 : ImmutableList<PermissionRule> accounts = 65 16 : proto.getAccountsSectionList().stream() 66 16 : .map(PermissionRuleSerializer::deserialize) 67 16 : .collect(toImmutableList()); 68 16 : builder.setAccountsSection(AccountsSection.create(accounts)); 69 : 70 16 : proto.getGroupListList().stream() 71 16 : .map(GroupReferenceSerializer::deserialize) 72 16 : .forEach(builder::addGroup); 73 16 : proto.getAccessSectionsList().stream() 74 16 : .map(AccessSectionSerializer::deserialize) 75 16 : .forEach(builder::addAccessSection); 76 16 : proto.getContributorAgreementsList().stream() 77 16 : .map(ContributorAgreementSerializer::deserialize) 78 16 : .forEach(builder::addContributorAgreement); 79 16 : proto.getNotifyConfigsList().stream() 80 16 : .map(NotifyConfigSerializer::deserialize) 81 16 : .forEach(builder::addNotifySection); 82 16 : proto.getLabelSectionsList().stream() 83 16 : .map(LabelTypeSerializer::deserialize) 84 16 : .forEach(builder::addLabelSection); 85 16 : proto.getSubmitRequirementSectionsList().stream() 86 16 : .map(SubmitRequirementSerializer::deserialize) 87 16 : .forEach(builder::addSubmitRequirementSection); 88 16 : proto.getSubscribeSectionsList().stream() 89 16 : .map(SubscribeSectionSerializer::deserialize) 90 16 : .forEach(builder::addSubscribeSection); 91 16 : proto.getCommentLinksList().stream() 92 16 : .map(StoredCommentLinkInfoSerializer::deserialize) 93 16 : .forEach(builder::addCommentLinkSection); 94 16 : proto 95 16 : .getPluginConfigsMap() 96 16 : .entrySet() 97 16 : .forEach(e -> builder.addPluginConfig(e.getKey(), e.getValue())); 98 16 : proto 99 16 : .getProjectLevelConfigsMap() 100 16 : .entrySet() 101 16 : .forEach(e -> builder.addProjectLevelConfig(e.getKey(), e.getValue())); 102 : 103 16 : return builder.build(); 104 : } 105 : 106 : public static Cache.CachedProjectConfigProto serialize(CachedProjectConfig autoValue) { 107 : Cache.CachedProjectConfigProto.Builder builder = 108 16 : Cache.CachedProjectConfigProto.newBuilder() 109 16 : .setProject(ProjectSerializer.serialize(autoValue.getProject())) 110 16 : .setMaxObjectSizeLimit(autoValue.getMaxObjectSizeLimit()) 111 16 : .setCheckReceivedObjects(autoValue.getCheckReceivedObjects()); 112 : 113 16 : if (autoValue.getBranchOrderSection().isPresent()) { 114 1 : builder.setBranchOrderSection( 115 1 : BranchOrderSectionSerializer.serialize(autoValue.getBranchOrderSection().get())); 116 : } 117 16 : autoValue.getMimeTypes().matchers().stream() 118 16 : .map(ConfiguredMimeTypeSerializer::serialize) 119 16 : .forEach(builder::addMimeTypes); 120 : 121 16 : if (autoValue.getRulesId().isPresent()) { 122 1 : builder.setRulesId(ObjectIdConverter.create().toByteString(autoValue.getRulesId().get())); 123 : } 124 16 : if (autoValue.getRevision().isPresent()) { 125 16 : builder.setRevision(ObjectIdConverter.create().toByteString(autoValue.getRevision().get())); 126 : } 127 : 128 16 : autoValue 129 16 : .getExtensionPanelSections() 130 16 : .entrySet() 131 16 : .forEach( 132 : panelSection -> { 133 1 : builder.putExtensionPanels( 134 1 : panelSection.getKey(), 135 1 : Cache.CachedProjectConfigProto.ExtensionPanelSectionProto.newBuilder() 136 1 : .addAllSection(panelSection.getValue()) 137 1 : .build()); 138 1 : }); 139 16 : autoValue.getAccountsSection().getSameGroupVisibility().stream() 140 16 : .map(PermissionRuleSerializer::serialize) 141 16 : .forEach(builder::addAccountsSection); 142 : 143 16 : autoValue.getGroups().values().stream() 144 16 : .map(GroupReferenceSerializer::serialize) 145 16 : .forEach(builder::addGroupList); 146 16 : autoValue.getAccessSections().values().stream() 147 16 : .map(AccessSectionSerializer::serialize) 148 16 : .forEach(builder::addAccessSections); 149 16 : autoValue.getContributorAgreements().values().stream() 150 16 : .map(ContributorAgreementSerializer::serialize) 151 16 : .forEach(builder::addContributorAgreements); 152 16 : autoValue.getNotifySections().values().stream() 153 16 : .map(NotifyConfigSerializer::serialize) 154 16 : .forEach(builder::addNotifyConfigs); 155 16 : autoValue.getLabelSections().values().stream() 156 16 : .map(LabelTypeSerializer::serialize) 157 16 : .forEach(builder::addLabelSections); 158 16 : autoValue.getSubmitRequirementSections().values().stream() 159 16 : .map(SubmitRequirementSerializer::serialize) 160 16 : .forEach(builder::addSubmitRequirementSections); 161 16 : autoValue.getSubscribeSections().values().stream() 162 16 : .map(SubscribeSectionSerializer::serialize) 163 16 : .forEach(builder::addSubscribeSections); 164 16 : autoValue.getCommentLinkSections().values().stream() 165 16 : .map(StoredCommentLinkInfoSerializer::serialize) 166 16 : .forEach(builder::addCommentLinks); 167 16 : builder.putAllPluginConfigs(autoValue.getPluginConfigs()); 168 16 : builder.putAllProjectLevelConfigs(autoValue.getProjectLevelConfigs()); 169 : 170 16 : return builder.build(); 171 : } 172 : 173 : private CachedProjectConfigSerializer() {} 174 : }