Skip to content

Refactor client generator #97

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import com.google.auto.service.AutoService;
import com.jacobmountain.graphql.client.annotations.GraphQLClient;
import com.jacobmountain.graphql.client.exceptions.SchemaNotFoundException;
import com.jacobmountain.graphql.client.utils.Schema;
import com.jacobmountain.graphql.client.utils.StringUtils;
import lombok.AllArgsConstructor;
import com.jacobmountain.graphql.client.code.ClientGenerator;
import lombok.SneakyThrows;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.processing.*;
Expand All @@ -18,10 +14,8 @@
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -37,6 +31,8 @@ public class GraphQLClientProcessor extends AbstractProcessor {

private Filer filer;

private ClientGenerator clientGenerator;

private Messager messager;

private Path root;
Expand All @@ -46,6 +42,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.filer = processingEnv.getFiler();
this.messager = processingEnv.getMessager();
this.clientGenerator = new ClientGenerator(this.filer);
}

@Override
Expand All @@ -59,7 +56,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
final List<Input> interfaces = elements.stream()
.map(el -> (TypeElement) el)
.map(Input::new)
.map(type -> new Input(type, getRoot(), processingEnv.getElementUtils().getPackageOf(type).toString()))
.collect(toList());
interfaces.stream()
.filter(new OncePerSchemaPredicate())
Expand All @@ -84,11 +81,10 @@ private void generateJavaDataClasses(Input input) {
dtoGenerator.generate(input.getSchema().types().values());
}

@SneakyThrows
private void generateClientImplementation(Input client) {
GraphQLClient annotation = client.getAnnotation();
log.info("Generating java implementation of {}", client.element.getSimpleName());
new ClientGenerator(this.filer, client.getTypeMapper(), client.getPackage(), client.getDtoPackage(), client.getSchema(), annotation.reactive())
.generate(client.element, annotation.implSuffix());
log.info("Generating java implementation of {}", client.getElement().getSimpleName());
clientGenerator.generate(client);
}

@SneakyThrows
Expand All @@ -106,51 +102,4 @@ private Path getRoot() {
return root;
}

@Value
@AllArgsConstructor
private class Input {

TypeElement element;

GraphQLClient getAnnotation() {
return element.getAnnotation(GraphQLClient.class);
}

TypeMapper getTypeMapper() {
return new TypeMapper(getDtoPackage(), getAnnotation().mapping());
}

String getDtoPackage() {
return String.join(".", Arrays.asList(
getPackage(),
getAnnotation().dtoPackage()
));
}

String getPackage() {
return processingEnv.getElementUtils().getPackageOf(element).toString();
}

Schema getSchema() {
String value = getAnnotation().schema();
File file = getSchemaFile();
try {
if (StringUtils.hasLength(value)) {
log.info("Reading schema {}", file);
return new Schema(getSchemaFile());
}
} catch (Exception e) {
e.printStackTrace();
}
throw new SchemaNotFoundException(file.getPath());
}

File getSchemaFile() {
return getRoot().resolve(getAnnotation().schema())
.toAbsolutePath()
.toFile();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.jacobmountain.graphql.client;

import com.jacobmountain.graphql.client.annotations.GraphQLClient;
import com.jacobmountain.graphql.client.exceptions.SchemaNotFoundException;
import com.jacobmountain.graphql.client.utils.Schema;
import com.jacobmountain.graphql.client.utils.StringUtils;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;

import javax.lang.model.element.TypeElement;
import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;

@Slf4j
@Value
public class Input {

TypeElement element;

@Getter(value = AccessLevel.PRIVATE)
String packageStr;

Path root;

public Input(TypeElement element, Path root, String packageStr) {
this.element = element;
this.packageStr = packageStr;
this.root = root;
}

public GraphQLClient getAnnotation() {
return element.getAnnotation(GraphQLClient.class);
}

public TypeMapper getTypeMapper() {
return new TypeMapper(getDtoPackage(), getAnnotation().mapping());
}

public String getDtoPackage() {
return String.join(".", Arrays.asList(
getPackage(),
getAnnotation().dtoPackage()
));
}

public String getPackage() {
return packageStr;
}

public Schema getSchema() {
String value = getAnnotation().schema();
File file = getSchemaFile();
try {
if (StringUtils.hasLength(value)) {
log.info("Reading schema {}", file);
return new Schema(getSchemaFile());
}
} catch (Exception e) {
e.printStackTrace();
}
throw new SchemaNotFoundException(file.getPath());
}

public File getSchemaFile() {
return root.resolve(getAnnotation().schema())
.toAbsolutePath()
.toFile();
}

public boolean isReactive() {
return getAnnotation().reactive();
}

}
Loading