Search in sources :

Example 1 with SchemaFormatType

use of com.linkedin.data.schema.SchemaFormatType in project rest.li by linkedin.

the class TestDataTemplateGeneratorCmdLineApp method testRunGenerator.

private void testRunGenerator(String pegasusFilename, Map<String, String> expectedTypeNamesToSourceFileMap, String resolverPath, List<String> resolverDirectories, String rootPath) throws Exception {
    Map<String, File> generatedFiles = generatePegasusDataTemplates(pegasusFilename, resolverPath, resolverDirectories, rootPath);
    Assert.assertEquals(generatedFiles.keySet(), expectedTypeNamesToSourceFileMap.keySet(), "Set of generated files does not match what's expected.");
    for (Map.Entry<String, File> entry : generatedFiles.entrySet()) {
        String pegasusTypeName = entry.getKey();
        File generated = entry.getValue();
        Assert.assertTrue(generated.exists());
        String generatedSource = FileUtils.readFileToString(generated);
        Assert.assertTrue(generatedSource.contains("class " + pegasusTypeName), "Incorrect generated class name.");
        // First, validate that a valid @Generated annotation exists on the class
        final Matcher generatedAnnotationMatcher = GENERATED_ANNOTATION_PATTERN.matcher(generatedSource);
        Assert.assertTrue(generatedAnnotationMatcher.find(), "Unable to find a valid @Generated annotation in the generated source file.");
        // The expected "source location" is the location of the PDL file used to generate the template class
        final String expectedSourceLocation = expectedTypeNamesToSourceFileMap.get(pegasusTypeName);
        // The actual "source location" is the URL in the @Generated annotation comment
        final String actualSourceLocation = generatedAnnotationMatcher.group(1);
        // If the origin file is in the temp folder, truncate to just the relative path within the temp folder
        // (we do this because some temp folder locations can be unpredictable e.g. /var vs. /private/var on MacOS)
        final String expectedRelativeLocation = expectedSourceLocation.substring(Math.max(0, expectedSourceLocation.indexOf(_tempDir.getName())));
        // Finally, assert that the @Generated annotation comment contains the expected relative path
        Assert.assertTrue(actualSourceLocation.contains(expectedRelativeLocation), String.format("Unexpected @Generated annotation. Expected to find \"%s\" in \"%s\".", expectedRelativeLocation, actualSourceLocation));
        SchemaFormatType schemaFormatType = SchemaFormatType.fromFilename(expectedTypeNamesToSourceFileMap.get(pegasusTypeName));
        Assert.assertNotNull(schemaFormatType, "Indeterminable schema format type.");
        // TODO: Collapse into one assertion once the codegen logic uses #parseSchema(String, SchemaFormatType) for PDSC.
        if (schemaFormatType == SchemaFormatType.PDSC) {
            Assert.assertFalse(generatedSource.contains("SchemaFormatType.PDSC"), "Expected no reference to 'SchemaFormatType.PDSC' in schema field initialization.");
        } else {
            Assert.assertTrue(generatedSource.contains("SchemaFormatType." + schemaFormatType.name()), String.format("Expected reference to 'SchemaFormatType.%s' in schema field initialization.", schemaFormatType.name()));
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) SchemaFormatType(com.linkedin.data.schema.SchemaFormatType) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SchemaFormatType

use of com.linkedin.data.schema.SchemaFormatType in project rest.li by linkedin.

the class JavaDataTemplateGenerator method generateSchemaField.

private JFieldVar generateSchemaField(JDefinedClass templateClass, DataSchema schema, SchemaFormatType sourceFormatType) {
    // If format is indeterminable (e.g. from IDL), then use default format
    final SchemaFormatType schemaFormatType = Optional.ofNullable(sourceFormatType).orElse(DEFAULT_SCHEMA_FORMAT_TYPE);
    final JFieldRef schemaFormatTypeRef = _schemaFormatTypeClass.staticRef(schemaFormatType.name());
    final JFieldVar schemaField = templateClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, schema.getClass(), DataTemplateUtil.SCHEMA_FIELD_NAME);
    // Compactly encode the schema text
    String schemaText;
    switch(schemaFormatType) {
        case PDSC:
            schemaText = SchemaToJsonEncoder.schemaToJson(schema, JsonBuilder.Pretty.COMPACT);
            break;
        case PDL:
            schemaText = SchemaToPdlEncoder.schemaToPdl(schema, SchemaToPdlEncoder.EncodingStyle.COMPACT);
            break;
        default:
            // This should never happen if all enum values are handled
            throw new IllegalStateException(String.format("Unrecognized schema format type '%s'", schemaFormatType));
    }
    // Generate the method invocation to parse the schema text
    final JInvocation parseSchemaInvocation = _dataTemplateUtilClass.staticInvoke("parseSchema").arg(getSizeBoundStringLiteral(schemaText));
    // TODO: Eventually use new interface for all formats, postponing adoption for PDSC to avoid build failures.
    if (schemaFormatType != SchemaFormatType.PDSC) {
        parseSchemaInvocation.arg(schemaFormatTypeRef);
    }
    // Generate the schema field initialization
    schemaField.init(JExpr.cast(getCodeModel()._ref(schema.getClass()), parseSchemaInvocation));
    // Using "dataSchema" as method name since "schema" conflicts with RecordTemplate::schema and "getSchema" conflicts
    // with TyperefInfo::getSchema
    final JMethod staticFieldsAccessor = templateClass.method(JMod.PUBLIC | JMod.STATIC, schema.getClass(), "dataSchema");
    staticFieldsAccessor.body()._return(schemaField);
    return schemaField;
}
Also used : JFieldRef(com.sun.codemodel.JFieldRef) JFieldVar(com.sun.codemodel.JFieldVar) SchemaFormatType(com.linkedin.data.schema.SchemaFormatType) JInvocation(com.sun.codemodel.JInvocation) ByteString(com.linkedin.data.ByteString) JMethod(com.sun.codemodel.JMethod)

Example 3 with SchemaFormatType

use of com.linkedin.data.schema.SchemaFormatType in project rest.li by linkedin.

the class TestPegasusDataTemplateGenerator method testRunGenerator.

private void testRunGenerator(String pegasusFilename, Map<String, String> expectedTypeNamesToSourceFileMap, String expectedGeneratedDir) throws Exception {
    Map<String, File> generatedFiles = generatePegasusDataTemplates(pegasusFilename);
    Assert.assertEquals(generatedFiles.keySet(), expectedTypeNamesToSourceFileMap.keySet(), "Set of generated files does not match what's expected.");
    for (Map.Entry<String, File> entry : generatedFiles.entrySet()) {
        String pegasusTypeName = entry.getKey();
        File generated = entry.getValue();
        Assert.assertTrue(generated.exists());
        String generatedSource = FileUtils.readFileToString(generated);
        Assert.assertTrue(generatedSource.contains("class " + pegasusTypeName), "Incorrect generated class name.");
        String expectedGeneratedAnnotation = "Generated from " + expectedGeneratedDir + FS + expectedTypeNamesToSourceFileMap.get(pegasusTypeName);
        Assert.assertTrue(generatedSource.contains(expectedGeneratedAnnotation), "Incorrect @Generated annotation, expected: " + expectedGeneratedAnnotation);
        SchemaFormatType schemaFormatType = SchemaFormatType.fromFilename(expectedTypeNamesToSourceFileMap.get(pegasusTypeName));
        Assert.assertNotNull(schemaFormatType, "Indeterminable schema format type.");
        // TODO: Collapse into one assertion once the codegen logic uses #parseSchema(String, SchemaFormatType) for PDSC.
        if (schemaFormatType == SchemaFormatType.PDSC) {
            Assert.assertFalse(generatedSource.contains("SchemaFormatType.PDSC"), "Expected no reference to 'SchemaFormatType.PDSC' in schema field initialization.");
        } else {
            Assert.assertTrue(generatedSource.contains("SchemaFormatType." + schemaFormatType.name()), String.format("Expected reference to 'SchemaFormatType.%s' in schema field initialization.", schemaFormatType.name()));
        }
    }
}
Also used : SchemaFormatType(com.linkedin.data.schema.SchemaFormatType) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SchemaFormatType (com.linkedin.data.schema.SchemaFormatType)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ByteString (com.linkedin.data.ByteString)1 JFieldRef (com.sun.codemodel.JFieldRef)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 JMethod (com.sun.codemodel.JMethod)1 Matcher (java.util.regex.Matcher)1