use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class PegasusDataTemplateGenerator method run.
public static GeneratorResult run(String resolverPath, String defaultPackage, final boolean generateImported, String targetDirectoryPath, String[] sources) throws IOException {
final DataSchemaParser schemaParser = new DataSchemaParser(resolverPath);
final TemplateSpecGenerator specGenerator = new TemplateSpecGenerator(schemaParser.getSchemaResolver());
final JavaDataTemplateGenerator dataTemplateGenerator = new JavaDataTemplateGenerator(defaultPackage);
for (DataSchema predefinedSchema : JavaDataTemplateGenerator.PredefinedJavaClasses.keySet()) {
specGenerator.registerDefinedSchema(predefinedSchema);
}
final DataSchemaParser.ParseResult parseResult = schemaParser.parseSources(sources);
for (Map.Entry<DataSchema, DataSchemaLocation> entry : parseResult.getSchemaAndLocations().entrySet()) {
specGenerator.generate(entry.getKey(), entry.getValue());
}
for (ClassTemplateSpec spec : specGenerator.getGeneratedSpecs()) {
dataTemplateGenerator.generate(spec);
}
final JavaCodeUtil.PersistentClassChecker checker = new DataTemplatePersistentClassChecker(generateImported, specGenerator, dataTemplateGenerator, parseResult.getSourceFiles());
final File targetDirectory = new File(targetDirectoryPath);
final List<File> targetFiles = JavaCodeUtil.targetFiles(targetDirectory, dataTemplateGenerator.getCodeModel(), JavaCodeUtil.classLoaderFromResolverPath(schemaParser.getResolverPath()), checker);
final List<File> modifiedFiles;
if (FileUtil.upToDate(parseResult.getSourceFiles(), targetFiles)) {
modifiedFiles = Collections.emptyList();
_log.info("Target files are up-to-date: " + targetFiles);
} else {
modifiedFiles = targetFiles;
_log.info("Generating " + targetFiles.size() + " files: " + targetFiles);
validateDefinedClassRegistration(dataTemplateGenerator.getCodeModel(), dataTemplateGenerator.getGeneratedClasses().keySet());
dataTemplateGenerator.getCodeModel().build(new FileCodeWriter(targetDirectory, true));
}
return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateQueryParamBindingMethods.
private void generateQueryParamBindingMethods(ParameterSchemaArray parameters, RequestBuilderSpec requestBuilderClass) {
for (ParameterSchema param : parameters) {
final String paramName = param.getName();
final DataSchema typeSchema = RestSpecCodec.textToSchema(param.getType(), _schemaResolver);
final ClassTemplateSpec typeClassSpec = schemaToTemplateSpec(typeSchema);
final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
final String setMethodName = RestLiToolsUtils.nameCamelCase(paramName + "Param");
final QueryParamBindingMethodSpec spec = new QueryParamBindingMethodSpec(setMethodName);
spec.setParamName(paramName);
spec.setNeedAddParamMethod(false);
spec.setOptional(isOptional);
spec.setArgType(typeClassSpec);
spec.setDoc(param.getDoc());
requestBuilderClass.addQueryParamMethod(spec);
if (typeSchema instanceof ArrayDataSchema) {
final DataSchema itemsSchema = ((ArrayDataSchema) typeSchema).getItems();
final ClassTemplateSpec itemSpecClass = schemaToTemplateSpec(itemsSchema);
final String addMethodName = RestLiToolsUtils.nameCamelCase("add" + RestLiToolsUtils.normalizeCaps(paramName) + "Param");
final QueryParamBindingMethodSpec addSpec = new QueryParamBindingMethodSpec(addMethodName);
addSpec.setParamName(paramName);
addSpec.setNeedAddParamMethod(true);
addSpec.setOptional(isOptional);
addSpec.setArgType(itemSpecClass);
addSpec.setDoc(param.getDoc());
requestBuilderClass.addQueryParamMethod(addSpec);
}
}
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generatePathKeyBindingMethods.
private void generatePathKeyBindingMethods(List<String> pathKeys, RequestBuilderSpec builderClass, Map<String, String> pathKeyTypes) {
for (String pathKey : pathKeys) {
PathKeyBindingMethodSpec spec = new PathKeyBindingMethodSpec();
spec.setPathKey(pathKey);
spec.setMethodName(RestLiToolsUtils.nameCamelCase(pathKey + "Key"));
final ClassTemplateSpec argTypeClassSpec = classToTemplateSpec(pathKeyTypes.get(pathKey));
spec.setArgType(argTypeClassSpec);
builderClass.addPathKeyMethod(spec);
}
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateActionRequestBuilder.
private ActionBuilderSpec generateActionRequestBuilder(ResourceSchema resource, String baseBuilderClass, String keyClass, String returnType, String requestBuilderName, String clientPackage, ActionSchema schema) {
ActionBuilderSpec actionBuilderClass = new ActionBuilderSpec(clientPackage, requestBuilderName, baseBuilderClass, resource, schema.getName());
actionBuilderClass.setAnnotations(schema.getAnnotations() == null ? null : schema.getAnnotations().data());
final ClassTemplateSpec keyClassSpec = classToTemplateSpec(keyClass);
actionBuilderClass.setKeyClass(keyClassSpec);
final ClassTemplateSpec returnClassSpec = classToTemplateSpec(returnType);
actionBuilderClass.setValueClass(returnClassSpec);
registerBuilderSpec(actionBuilderClass);
return actionBuilderClass;
}
use of com.linkedin.pegasus.generator.spec.ClassTemplateSpec in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateActionParamBindingMethods.
private void generateActionParamBindingMethods(ParameterSchemaArray parameters, ActionBuilderSpec requestBuilderClass) {
for (ParameterSchema param : parameters) {
final String paramName = param.getName();
final ClassTemplateSpec typeClassSpec = classToTemplateSpec(param.getType());
final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
final String setMethodName = RestLiToolsUtils.nameCamelCase(paramName + "Param");
ActionParamBindingMethodSpec spec = new ActionParamBindingMethodSpec(setMethodName);
spec.setParamName(paramName);
spec.setOptional(isOptional);
spec.setArgType(typeClassSpec);
spec.setDoc(param.getDoc());
requestBuilderClass.addActionParamMethod(spec);
}
}
Aggregations