Skip to content

Commit

Permalink
fix: start turning on werror for code. (#32)
Browse files Browse the repository at this point in the history
I've touched up a few easy to fix places, but have yet to fix everything / all modules. Might be wise to do this on a module by module basis.
  • Loading branch information
John Plaisted authored Nov 4, 2020
1 parent 6669cd3 commit 23d8832
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
22 changes: 22 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,34 @@ project.ext.externalDependency = [
apply plugin: 'com.diffplug.spotless'
apply plugin: 'org.shipkit.shipkit-auto-version'

// TODO expand this to all projects and then delete this allow list. This list is letting us fix errors over time rather
// than in one big change.
def wErrorProjects = [
project(':core-models'),
// project(':dao-api'),
// project(':dao-api-impl'),
// project(':restli-resources'),
project(':testing'),
project(':validators')
]

allprojects {
group = "com.linkedin.datahub-gma"

apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'checkstyle'

gradle.projectsEvaluated {
if (wErrorProjects.contains(project)) {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:all" << "-Werror" <<
"-Xlint:-deprecation" << // TODO
"-Xlint:-processing" << // TODO we have annotations like @Nonnull that need a processor
"-Xlint:-serial" // I don't think we care about having custom Exception subclasses be serializable...
}
}
}
}

if (!project.hasProperty('disableShipkit')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ public <T> T getAs(int index, Class<T> clazz) {
} else if (Long.TYPE.equals(clazz) || Long.class.equals(clazz)) {
result = Long.valueOf(value);
} else if (Enum.class.isAssignableFrom(clazz)) {
final Class<? extends Enum> enumClazz = clazz.asSubclass(Enum.class);
@SuppressWarnings("unchecked")
Enum<?> enumValue = Enum.valueOf(enumClazz, value);
result = enumValue;
result = getEnumValue(clazz, value);
} else if (DataTemplateUtil.hasCoercer(clazz)) {
result = DataTemplateUtil.coerceOutput(value, clazz);
} else {
Expand All @@ -169,6 +166,15 @@ public <T> T getAs(int index, Class<T> clazz) {
return rv;
}

/**
* Helper method to capture E.
*/
private <E extends Enum<E>> Enum<E> getEnumValue(Class<?> clazz, String value) {
@SuppressWarnings("unchecked")
final Class<E> enumClazz = (Class<E>) clazz.asSubclass(Enum.class);
return Enum.valueOf(enumClazz, value);
}

public int size() {
return _tuple.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public Object coerceInput(T object) throws ClassCastException {
return object.toString();
}

@SuppressWarnings("unchecked")
public T coerceOutput(Object object) throws TemplateOutputCastException {
try {
return (T) Urn.createFromString((String) object);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkedin.metadata.validator;

import com.linkedin.common.urn.Urn;
import com.linkedin.data.schema.ArrayDataSchema;
import com.linkedin.data.schema.DataSchema;
import com.linkedin.data.schema.RecordDataSchema;
Expand Down Expand Up @@ -60,10 +61,11 @@ public static void validateSnapshotSchema(@Nonnull Class<? extends RecordTemplat
*
* @param snapshotClasses a collection of snapshot classes.
*/
public static void validateUniqueUrn(@Nonnull Collection<? extends Class> snapshotClasses) {
Set<Class> urnClasses = new HashSet<>();
public static void validateUniqueUrn(@Nonnull Collection<Class<? extends RecordTemplate>> snapshotClasses) {
final Set<Class<Urn>> urnClasses = new HashSet<>();
snapshotClasses.forEach(snapshotClass -> {
Class urnClass = ValidationUtils.getUrnClass(ValidationUtils.getRecordSchema(snapshotClass).getField("urn"));
final Class<Urn> urnClass =
ValidationUtils.getUrnClass(ValidationUtils.getRecordSchema(snapshotClass).getField("urn"));
if (urnClasses.contains(urnClass)) {
ValidationUtils.invalidSchema("URN class %s in %s has already been claimed by another snapshot.", urnClass,
snapshotClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ public static boolean isValidUrnField(@Nonnull RecordDataSchema.Field field, @No
/**
* Returns the Java class for an URN typeref field.
*/
public static Class getUrnClass(@Nonnull RecordDataSchema.Field field) {
public static Class<Urn> getUrnClass(@Nonnull RecordDataSchema.Field field) {
try {
return Class.forName(((DataMap) field.getType().getProperties().get("java")).getString("class"));
@SuppressWarnings("unchecked")
final Class<Urn> clazz =
(Class<Urn>) Class.forName(((DataMap) field.getType().getProperties().get("java")).getString("class"));
return clazz;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 23d8832

Please sign in to comment.