use of com.google.common.base.CaseFormat.UPPER_CAMEL in project curiostack by curioswitch.
the class GenerateApiServerTask method exec.
@TaskAction
public void exec() {
var ant = getAnt();
ant.invokeMethod("echo", ImmutableMap.of("message", "Generating an API server (see " + "https://codelabs.developers.curioswitch.org/defining-a-grpc-api/#1) for details."));
ant.invokeMethod("input", ImmutableMap.of("message", "What is name of the project? Two artifacts, {name}-api and {name}-server will " + "be generated. {name}-server will be used in deployments and should be relatively " + "concise. The name should be hyphen-case", "addproperty", "name"));
ant.invokeMethod("input", ImmutableMap.of("message", "What is the path that the files should be written to (e.g., path/to/project)?", "addproperty", "outputPath"));
ant.invokeMethod("input", ImmutableMap.of("message", "What is the java package to place code under? " + "API will live under {package}.api and server code will live under " + "{package}.server", "addproperty", "javaPackage"));
ant.invokeMethod("input", ImmutableMap.of("message", "What is the proto package to place the API under? " + "Defaults to same value as the java package + .api.", "addproperty", "protoPackage", "defaultvalue", ant.getProperty("javaPackage") + ".api"));
ant.invokeMethod("input", ImmutableMap.of("message", "What is the name of the API service? This should generally be UpperCamelCase. " + "Defaults to UpperCamelCase version of {name}Service.", "addproperty", "serviceName", "defaultvalue", LOWER_HYPHEN.to(UPPER_CAMEL, (String) ant.getProperty("name")) + "Service"));
var outputPathStr = (String) ant.getProperty("outputPath");
if (outputPathStr.startsWith("/")) {
throw new IllegalArgumentException("outputPath must be a relative path below the repository root, it cannot begin with /");
}
if (outputPathStr.endsWith("/")) {
outputPathStr = outputPathStr.substring(0, outputPathStr.length() - 1);
}
Path outputPath = Paths.get(getProject().getRootDir().getAbsolutePath(), outputPathStr);
if (Files.exists(outputPath)) {
throw new IllegalArgumentException("Output path already exists. If this is correct, remove the directory first.");
}
Path apiOutputPath = outputPath.resolve("api");
Path serverOutputPath = outputPath.resolve("server");
Path copyrightFile = null;
try (var s = Files.list(getProject().file(".baseline/copyright").toPath())) {
copyrightFile = s.sorted().findFirst().orElse(null);
} catch (IOException e) {
// Ignore exceptions trying to read copyright.
}
String copyright = "";
try (var s = Files.lines(copyrightFile)) {
copyright = s.map(line -> line.replace("${today.year}", String.valueOf(Year.now().getValue()))).map(line -> " * " + line).collect(Collectors.joining(lineSeparator(), "/*" + lineSeparator(), lineSeparator() + " */" + lineSeparator()));
} catch (IOException e) {
// Ignore exceptions trying to read copyright.
}
String apiDependency = ':' + (outputPathStr + "/api").replace("/", ":");
String name = (String) ant.getProperty("name");
String protoPackage = (String) ant.getProperty("protoPackage");
String javaPackage = (String) ant.getProperty("javaPackage");
String serviceName = (String) ant.getProperty("serviceName");
Map<String, Object> context = ImmutableMap.<String, Object>builder().put("copyright", copyright).put("name", name).put("proto_package", protoPackage).put("java_package", javaPackage).put("service_name", serviceName).put("api_dependency", apiDependency).build();
render("templates/apiserver/api/build.gradle.kts.tmpl", context, apiOutputPath.resolve("build.gradle.kts"));
render("templates/apiserver/api/service.proto.tmpl", context, apiOutputPath.resolve("src/main/proto/" + protoPackage.replace(".", "/") + "/" + name + "-service.proto"));
render("templates/apiserver/server/build.gradle.kts.tmpl", context, serverOutputPath.resolve("build.gradle.kts"));
render("templates/apiserver/server/Main.java.tmpl", context, serverOutputPath.resolve("src/main/java/" + javaPackage.replace(".", "/") + "/server/" + serviceName + "Main.java"));
render("templates/apiserver/server/ServiceImpl.java.tmpl", context, serverOutputPath.resolve("src/main/java/" + javaPackage.replace(".", "/") + "/server/" + serviceName + ".java"));
}
use of com.google.common.base.CaseFormat.UPPER_CAMEL in project airlift by airlift.
the class ConfigurationFactory method build.
private <T> ConfigurationHolder<T> build(Class<T> configClass, Optional<String> configPrefix, ConfigDefaults<T> configDefaults) {
if (configClass == null) {
throw new NullPointerException("configClass is null");
}
String prefix = configPrefix.map(value -> value + ".").orElse("");
ConfigurationMetadata<T> configurationMetadata = getMetadata(configClass);
configurationMetadata.getProblems().throwIfHasErrors();
T instance = newInstance(configurationMetadata);
configDefaults.setDefaults(instance);
Problems problems = new Problems(monitor);
for (AttributeMetadata attribute : configurationMetadata.getAttributes().values()) {
Problems attributeProblems = new Problems(monitor);
try {
setConfigProperty(instance, attribute, prefix, attributeProblems);
} catch (InvalidConfigurationException e) {
attributeProblems.addError(e.getCause(), e.getMessage());
}
problems.record(attributeProblems);
}
// Check that none of the defunct properties are still in use
if (configClass.isAnnotationPresent(DefunctConfig.class)) {
for (String value : configClass.getAnnotation(DefunctConfig.class).value()) {
String name = prefix + value;
if (!value.isEmpty() && properties.get(name) != null) {
problems.addError("Defunct property '%s' (class [%s]) cannot be configured.", name, configClass.toString());
}
}
}
// if there already problems, don't run the bean validation as it typically reports duplicate errors
problems.throwIfHasErrors();
for (ConstraintViolation<?> violation : validate(instance)) {
String propertyFieldName = violation.getPropertyPath().toString();
// upper case first character to match config attribute name
String attributeName = LOWER_CAMEL.to(UPPER_CAMEL, propertyFieldName);
AttributeMetadata attribute = configurationMetadata.getAttributes().get(attributeName);
if (attribute != null && attribute.getInjectionPoint() != null) {
String propertyName = attribute.getInjectionPoint().getProperty();
if (!prefix.isEmpty()) {
propertyName = prefix + propertyName;
}
problems.addError("Invalid configuration property %s: %s (for class %s.%s)", propertyName, violation.getMessage(), configClass.getName(), violation.getPropertyPath());
} else {
problems.addError("Invalid configuration property with prefix '%s': %s (for class %s.%s)", prefix, violation.getMessage(), configClass.getName(), violation.getPropertyPath());
}
}
problems.throwIfHasErrors();
return new ConfigurationHolder<>(instance, problems);
}
Aggregations