Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account for jsonUrl being null in type signature #193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// modified from https://stackoverflow.com/questions/12161366/how-to-serialize-optionalt-classes-with-gson
package edu.wpi.first.nativeutils.vendordeps;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

class OptionalTypeAdapter<E> extends TypeAdapter<Optional<E>> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<T> rawType = (Class<T>) type.getRawType().getClass();
if (rawType != Optional.class) {
return null;
}
final ParameterizedType parameterizedType = (ParameterizedType) type.getType();
final Type actualType = parameterizedType.getActualTypeArguments()[0];
final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(actualType));
return new OptionalTypeAdapter(adapter);
}
Comment on lines +19 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should allow us to parse Optional correctly but has a few warnings to do with unchecked casts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think that is worth adding, for something we don’t really need anyway. Empty string is just fine and we can specify that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a new pr to default it to an empty string and close this one

};
private final TypeAdapter<E> adapter;

public OptionalTypeAdapter(TypeAdapter<E> adapter) {
this.adapter = adapter;
}

@Override
public void write(JsonWriter out, Optional<E> value) throws IOException {
if(value.isPresent()){
adapter.write(out, value.get());
} else {
out.nullValue();
}
}

@Override
public Optional<E> read(JsonReader in) throws IOException {
final JsonToken peek = in.peek();
if(peek != JsonToken.NULL){
return Optional.ofNullable(adapter.read(in));
}

in.nextNull();
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Optional;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -21,6 +22,7 @@
import org.gradle.nativeplatform.plugins.NativeComponentPlugin;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import edu.wpi.first.deployutils.log.ETLogger;
import edu.wpi.first.deployutils.log.ETLoggerFactory;
Expand All @@ -47,7 +49,7 @@ public NamedDomainObjectSet<NamedJsonDependency> getDependencySet() {
}

private final ETLogger log;
private final Gson gson = new Gson();
private final Gson gson;

public static final String DEFAULT_VENDORDEPS_FOLDER_NAME = "vendordeps";
public static final String NATIVEUTILS_VENDOR_FOLDER_PROPERTY = "nativeutils.vendordep.folder.path";
Expand All @@ -69,6 +71,10 @@ public WPIJavaVendorDepsExtension getJavaVendor() {

@Inject
public WPIVendorDepsExtension(Project project) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
builder.registerTypeAdapterFactory(OptionalTypeAdapter.FACTORY);
gson = builder.create();

frcYear = project.getObjects().property(String.class);
frcHome = project.getObjects().directoryProperty();
Expand Down Expand Up @@ -315,7 +321,7 @@ public static class JsonDependency {
public VendorDependency[] conflictsWith;
public String[] mavenUrls;
public String[] extraGroupIds;
public String jsonUrl;
public Optional<String> jsonUrl;
public String fileName;
public String frcYear;
public JavaArtifact[] javaDependencies;
Expand Down