Add serializer for StoredCommentLinkInfo

This commit adds a serializer for the StoredCommentLinkInfo entitiy.
The eventual goal is that we serialize CachedProjectConfig. The
entity is too large to be serialized directly, though, so we
divide and conquer.

This commit moves the AutoValue representation to the entities
package to allow the serializer packages to keep its dependencies
minimal.

Change-Id: I860e2c4c1a845d2e518c1a331711e1bc66ac85be
This commit is contained in:
Patrick Hiesel
2020-07-16 12:49:49 +02:00
parent 1c6840566c
commit 0a9b9db8f1
9 changed files with 124 additions and 3 deletions

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.project;
package com.google.gerrit.entities;
import static com.google.common.base.Preconditions.checkArgument;
@@ -68,7 +68,7 @@ public abstract class StoredCommentLinkInfo {
}
/** Creates and returns a new {@link StoredCommentLinkInfo} instance with the same values. */
static StoredCommentLinkInfo fromInfo(CommentLinkInfo src, boolean enabled) {
public static StoredCommentLinkInfo fromInfo(CommentLinkInfo src, boolean enabled) {
return builder(src.name)
.setMatch(src.match)
.setLink(src.link)
@@ -79,7 +79,7 @@ public abstract class StoredCommentLinkInfo {
}
/** Returns an {@link CommentLinkInfo} instance with the same values. */
CommentLinkInfo toInfo() {
public CommentLinkInfo toInfo() {
CommentLinkInfo info = new CommentLinkInfo();
info.name = getName();
info.match = getMatch();

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.base.Strings.emptyToNull;
import static com.google.common.base.Strings.nullToEmpty;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.server.cache.proto.Cache;
/** Helper to (de)serialize values for caches. */
public class StoredCommentLinkInfoSerializer {
public static StoredCommentLinkInfo deserialize(Cache.StoredCommentLinkInfoProto proto) {
return StoredCommentLinkInfo.builder(proto.getName())
.setMatch(emptyToNull(proto.getMatch()))
.setLink(emptyToNull(proto.getLink()))
.setHtml(emptyToNull(proto.getHtml()))
.setEnabled(proto.getEnabled())
.setOverrideOnly(proto.getOverrideOnly())
.build();
}
public static Cache.StoredCommentLinkInfoProto serialize(StoredCommentLinkInfo autoValue) {
return Cache.StoredCommentLinkInfoProto.newBuilder()
.setName(autoValue.getName())
.setMatch(nullToEmpty(autoValue.getMatch()))
.setLink(nullToEmpty(autoValue.getLink()))
.setHtml(nullToEmpty(autoValue.getHtml()))
.setEnabled(autoValue.getEnabled())
.setOverrideOnly(autoValue.getOverrideOnly())
.build();
}
private StoredCommentLinkInfoSerializer() {}
}

View File

@@ -29,6 +29,7 @@ import com.google.gerrit.entities.ConfiguredMimeTypes;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.NotifyConfig;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import java.util.Collection;
import java.util.List;
import java.util.Map;

View File

@@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
import com.google.gerrit.server.config.ConfigUpdatedEvent;
import com.google.gerrit.server.config.ConfigUpdatedEvent.ConfigUpdateEntry;

View File

@@ -54,6 +54,7 @@ import com.google.gerrit.entities.NotifyConfig;
import com.google.gerrit.entities.NotifyConfig.NotifyType;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.exceptions.InvalidNameException;
import com.google.gerrit.extensions.client.InheritableBoolean;
import com.google.gerrit.extensions.client.ProjectState;

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.entities.BranchNameKey;
import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.restapi.ResourceConflictException;

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.cache.serialize.entities;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.cache.serialize.entities.StoredCommentLinkInfoSerializer.deserialize;
import static com.google.gerrit.server.cache.serialize.entities.StoredCommentLinkInfoSerializer.serialize;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import org.junit.Test;
public class StoredCommentLinkInfoSerializerTest {
@Test
public void htmlOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setHtml("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
@Test
public void linkOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setLink("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
@Test
public void overrideOnly_roundTrip() {
StoredCommentLinkInfo autoValue =
StoredCommentLinkInfo.builder("name")
.setEnabled(true)
.setOverrideOnly(true)
.setLink("<p>html")
.setMatch("*")
.build();
assertThat(deserialize(serialize(autoValue))).isEqualTo(autoValue);
}
}

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.entities.BranchOrderSection;
import com.google.gerrit.entities.GroupReference;
import com.google.gerrit.entities.Project;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.entities.StoredCommentLinkInfo;
import com.google.gerrit.extensions.client.InheritableBoolean;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.PluginConfig;

View File

@@ -452,3 +452,14 @@ message SubscribeSectionProto {
repeated string multi_match_ref_specs = 2;
repeated string matching_ref_specs = 3;
}
// Serialized form of com.google.gerrit.entities.StoredCommentLinkInfo.
// Next ID: 7
message StoredCommentLinkInfoProto {
string name = 1;
string match = 2;
string link = 3;
string html = 4;
bool enabled = 5;
bool override_only = 6;
}