Use simple class name of a class in stringify() (#1435)

* Keep CLASS_REGISTRY and CLASS_NAME_REGISTRY up to date

* Use simple class name in vkey string
This commit is contained in:
Rachel Guan 2021-12-07 11:49:50 -05:00 committed by GitHub
parent 66d246b723
commit c0e6770ba1
3 changed files with 23 additions and 25 deletions

View file

@ -48,7 +48,8 @@ public class ClassPathManager {
@VisibleForTesting @VisibleForTesting
public static void addTestEntityClass(Class<?> clazz) { public static void addTestEntityClass(Class<?> clazz) {
CLASS_REGISTRY.put(com.googlecode.objectify.Key.getKind(clazz), clazz); CLASS_REGISTRY.put(clazz.getSimpleName(), clazz);
CLASS_NAME_REGISTRY.put(clazz, clazz.getSimpleName());
} }
public static <T> Class<T> getClass(String className) { public static <T> Class<T> getClass(String className) {

View file

@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.model.BackupGroupRoot; import google.registry.model.BackupGroupRoot;
import google.registry.model.ImmutableObject; import google.registry.model.ImmutableObject;
import google.registry.model.common.ClassPathManager;
import google.registry.model.translators.VKeyTranslatorFactory; import google.registry.model.translators.VKeyTranslatorFactory;
import google.registry.util.SerializeUtils; import google.registry.util.SerializeUtils;
import java.io.Serializable; import java.io.Serializable;
@ -137,9 +138,9 @@ public class VKey<T> extends ImmutableObject implements Serializable {
* *
* <p>Example of a Vkey string by fromWebsafeKey(): "agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM" * <p>Example of a Vkey string by fromWebsafeKey(): "agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"
* *
* <p>Example of a vkey string by stringify(): "google.registry.testing.TestObject@sql:rO0ABX" + * <p>Example of a vkey string by stringify(): "kind:TestObject@sql:rO0ABXQAA2Zvbw" +
* "QAA2Zvbw@ofy:agR0ZXN0cjELEg9FbnRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M", * "@ofy:agR0ZXN0cjELEg9FbnRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M", where sql
* where sql key and ofy key are values are encoded in Base64. * key and ofy key values are encoded in Base64.
*/ */
public static <T> VKey<T> create(String keyString) throws Exception { public static <T> VKey<T> create(String keyString) throws Exception {
if (!keyString.startsWith(CLASS_TYPE + KV_SEPARATOR)) { if (!keyString.startsWith(CLASS_TYPE + KV_SEPARATOR)) {
@ -149,7 +150,7 @@ public class VKey<T> extends ImmutableObject implements Serializable {
ImmutableMap<String, String> kvs = ImmutableMap<String, String> kvs =
ImmutableMap.copyOf( ImmutableMap.copyOf(
Splitter.on(DELIMITER).withKeyValueSeparator(KV_SEPARATOR).split(keyString)); Splitter.on(DELIMITER).withKeyValueSeparator(KV_SEPARATOR).split(keyString));
Class classType = Class.forName(kvs.get(CLASS_TYPE)); Class classType = ClassPathManager.getClass(kvs.get(CLASS_TYPE));
if (kvs.containsKey(SQL_LOOKUP_KEY) && kvs.containsKey(OFY_LOOKUP_KEY)) { if (kvs.containsKey(SQL_LOOKUP_KEY) && kvs.containsKey(OFY_LOOKUP_KEY)) {
return VKey.create( return VKey.create(
@ -290,7 +291,7 @@ public class VKey<T> extends ImmutableObject implements Serializable {
/** /**
* Constructs the string representation of a {@link VKey}. * Constructs the string representation of a {@link VKey}.
* *
* <p>The string representation of a vkey contains its type, and sql key or ofy key, or both. Each * <p>The string representation of a vkey contains its kind, and sql key or ofy key, or both. Each
* of the keys is first serialized into a byte array then encoded via Base64 into a web safe * of the keys is first serialized into a byte array then encoded via Base64 into a web safe
* string. * string.
* *
@ -302,7 +303,7 @@ public class VKey<T> extends ImmutableObject implements Serializable {
*/ */
public String stringify() { public String stringify() {
// class type is required to create a vkey // class type is required to create a vkey
String key = CLASS_TYPE + KV_SEPARATOR + getKind().getName(); String key = CLASS_TYPE + KV_SEPARATOR + ClassPathManager.getClassName(getKind());
if (maybeGetSqlKey().isPresent()) { if (maybeGetSqlKey().isPresent()) {
key += DELIMITER + SQL_LOOKUP_KEY + KV_SEPARATOR + SerializeUtils.stringify(getSqlKey()); key += DELIMITER + SQL_LOOKUP_KEY + KV_SEPARATOR + SerializeUtils.stringify(getSqlKey());
} }
@ -315,7 +316,7 @@ public class VKey<T> extends ImmutableObject implements Serializable {
/** /**
* Constructs the readable string representation of a {@link VKey}. * Constructs the readable string representation of a {@link VKey}.
* *
* <p>This readable string representation of a vkey contains its type and its sql key or ofy key, * <p>This readable string representation of a vkey contains its kind and its sql key or ofy key,
* or both. * or both.
*/ */
@Override @Override

View file

@ -161,14 +161,13 @@ class VKeyTest {
@Test @Test
void testStringify_sqlOnlyVKey() throws Exception { void testStringify_sqlOnlyVKey() throws Exception {
assertThat(VKey.createSql(TestObject.class, "foo").stringify()) assertThat(VKey.createSql(TestObject.class, "foo").stringify())
.isEqualTo("kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw"); .isEqualTo("kind:TestObject@sql:rO0ABXQAA2Zvbw");
} }
@Test @Test
void testStringify_ofyOnlyVKey() throws Exception { void testStringify_ofyOnlyVKey() throws Exception {
assertThat(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo")).stringify()) assertThat(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo")).stringify())
.isEqualTo( .isEqualTo("kind:TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M");
"kind:google.registry.testing.TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M");
} }
@Test @Test
@ -178,8 +177,8 @@ class VKeyTest {
VKey<DomainBase> vkey = VKey.fromWebsafeKey(key.getString()); VKey<DomainBase> vkey = VKey.fromWebsafeKey(key.getString());
assertThat(vkey.stringify()) assertThat(vkey.stringify())
.isEqualTo( .isEqualTo(
"kind:google.registry.model.domain.DomainBas" "kind:DomainBase"
+ "e@sql:rO0ABXQABlJPSUQtMQ" + "@sql:rO0ABXQABlJPSUQtMQ"
+ "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"); + "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM");
} }
@ -188,7 +187,7 @@ class VKeyTest {
assertThat( assertThat(
VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo"))).stringify()) VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo"))).stringify())
.isEqualTo( .isEqualTo(
"kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9FbnRpdH" "kind:TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9FbnRpdH"
+ "lHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"); + "lHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M");
} }
@ -197,22 +196,20 @@ class VKeyTest {
assertThat( assertThat(
VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo"))).stringify()) VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo"))).stringify())
.isEqualTo( .isEqualTo(
"kind:google.registry.testing.TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9FbnRpd" "kind:TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9FbnRpd"
+ "HlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"); + "HlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M");
} }
/** Test create() via different vkey string representations. */ /** Test create() via different vkey string representations. */
@Test @Test
void testCreate_stringifedVKey_sqlOnlyVKeyString() throws Exception { void testCreate_stringifedVKey_sqlOnlyVKeyString() throws Exception {
assertThat(VKey.create("kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw")) assertThat(VKey.create("kind:TestObject@sql:rO0ABXQAA2Zvbw"))
.isEqualTo(VKey.createSql(TestObject.class, "foo")); .isEqualTo(VKey.createSql(TestObject.class, "foo"));
} }
@Test @Test
void testCreate_stringifedVKey_ofyOnlyVKeyString() throws Exception { void testCreate_stringifedVKey_ofyOnlyVKeyString() throws Exception {
assertThat( assertThat(VKey.create("kind:TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M"))
VKey.create(
"kind:google.registry.testing.TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo"))); .isEqualTo(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo")));
} }
@ -220,7 +217,7 @@ class VKeyTest {
void testCreate_stringifedVKey_asymmetricVKeyString() throws Exception { void testCreate_stringifedVKey_asymmetricVKeyString() throws Exception {
assertThat( assertThat(
VKey.create( VKey.create(
"kind:google.registry.testing.TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9Fb" "kind:TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9Fb"
+ "nRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M")) + "nRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo")))); .isEqualTo(VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo"))));
} }
@ -229,7 +226,7 @@ class VKeyTest {
void testCreate_stringifedVKey_sqlAndOfyVKeyString() throws Exception { void testCreate_stringifedVKey_sqlAndOfyVKeyString() throws Exception {
assertThat( assertThat(
VKey.create( VKey.create(
"kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9Fbn" "kind:TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9Fbn"
+ "RpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M")) + "RpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo")))); .isEqualTo(VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo"))));
} }
@ -238,7 +235,7 @@ class VKeyTest {
void testCreate_stringifyVkey_fromWebsafeKey() throws Exception { void testCreate_stringifyVkey_fromWebsafeKey() throws Exception {
assertThat( assertThat(
VKey.create( VKey.create(
"kind:google.registry.model.domain.DomainBase@sql:rO0ABXQABlJPSUQtMQ" "kind:DomainBase@sql:rO0ABXQABlJPSUQtMQ"
+ "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM")) + "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"))
.isEqualTo( .isEqualTo(
VKey.fromWebsafeKey( VKey.fromWebsafeKey(
@ -257,11 +254,10 @@ class VKeyTest {
void testCreate_invalidStringifiedVKey_failure() throws Exception { void testCreate_invalidStringifiedVKey_failure() throws Exception {
IllegalArgumentException thrown = IllegalArgumentException thrown =
assertThrows( assertThrows(
IllegalArgumentException.class, IllegalArgumentException.class, () -> VKey.create("kind:TestObject@sq:l@ofya:bc"));
() -> VKey.create("kind:google.registry.testing.TestObject@sq:l@ofya:bc"));
assertThat(thrown) assertThat(thrown)
.hasMessageThat() .hasMessageThat()
.contains("Cannot parse key string: kind:google.registry.testing.TestObject@sq:l@ofya:bc"); .contains("Cannot parse key string: kind:TestObject@sq:l@ofya:bc");
} }
@Test @Test