Search in sources :

Example 6 with ClassTemplateSpec

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);
}
Also used : DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) Map(java.util.Map) File(java.io.File)

Example 7 with ClassTemplateSpec

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);
        }
    }
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) QueryParamBindingMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.QueryParamBindingMethodSpec)

Example 8 with ClassTemplateSpec

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);
    }
}
Also used : PathKeyBindingMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.PathKeyBindingMethodSpec) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 9 with ClassTemplateSpec

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;
}
Also used : ActionBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec)

Example 10 with ClassTemplateSpec

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);
    }
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) ActionParamBindingMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.ActionParamBindingMethodSpec) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Aggregations

ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)17 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)4 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)3 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)3 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)3 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)3 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 CustomInfoSpec (com.linkedin.pegasus.generator.spec.CustomInfoSpec)2 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)2 FinderBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec)2 DataMap (com.linkedin.data.DataMap)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 RecordTemplateSpec (com.linkedin.pegasus.generator.spec.RecordTemplateSpec)1 FinderSchema (com.linkedin.restli.restspec.FinderSchema)1 ActionBuilderSpec (com.linkedin.restli.tools.clientgen.builderspec.ActionBuilderSpec)1