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.gerrit.entities.Permission; 20 : import com.google.gerrit.entities.PermissionRule; 21 : import com.google.gerrit.server.cache.proto.Cache; 22 : 23 : /** Helper to (de)serialize values for caches. */ 24 : public class PermissionSerializer { 25 : public static Permission deserialize(Cache.PermissionProto proto) { 26 16 : Permission.Builder builder = 27 16 : Permission.builder(proto.getName()).setExclusiveGroup(proto.getExclusiveGroup()); 28 16 : proto.getRulesList().stream() 29 16 : .map(PermissionRuleSerializer::deserialize) 30 16 : .map(PermissionRule::toBuilder) 31 16 : .forEach(rule -> builder.add(rule)); 32 16 : return builder.build(); 33 : } 34 : 35 : public static Cache.PermissionProto serialize(Permission autoValue) { 36 16 : return Cache.PermissionProto.newBuilder() 37 16 : .setName(autoValue.getName()) 38 16 : .setExclusiveGroup(autoValue.getExclusiveGroup()) 39 16 : .addAllRules( 40 16 : autoValue.getRules().stream() 41 16 : .map(PermissionRuleSerializer::serialize) 42 16 : .collect(toImmutableList())) 43 16 : .build(); 44 : } 45 : 46 : private PermissionSerializer() {} 47 : }