Search in sources :

Example 1 with ConjureSourceFile

use of com.palantir.conjure.parser.ConjureSourceFile in project conjure by palantir.

the class ConjureParserUtils method parseConjureDef.

static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs) {
    ImmutableList.Builder<ServiceDefinition> servicesBuilder = ImmutableList.builder();
    ImmutableList.Builder<ErrorDefinition> errorsBuilder = ImmutableList.builder();
    ImmutableList.Builder<TypeDefinition> typesBuilder = ImmutableList.builder();
    annotatedParsedDefs.values().forEach(annotatedParsed -> {
        ConjureSourceFile parsed = annotatedParsed.conjureSourceFile();
        try {
            ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(parsed.types(), annotatedParsed.importProviders(), annotatedParsedDefs);
            // Resolve objects first, so we can use them in service validations
            Map<TypeName, TypeDefinition> objects = parseObjects(parsed.types(), typeResolver);
            Map<TypeName, TypeDefinition> importedObjects = parseImportObjects(parsed.types().conjureImports(), annotatedParsedDefs);
            Map<TypeName, TypeDefinition> allObjects = new HashMap<>();
            allObjects.putAll(objects);
            allObjects.putAll(importedObjects);
            DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(allObjects);
            parsed.services().forEach((serviceName, service) -> {
                servicesBuilder.add(parseService(service, TypeName.of(serviceName.name(), parseConjurePackage(service.conjurePackage())), typeResolver, dealiasingVisitor));
            });
            typesBuilder.addAll(objects.values());
            errorsBuilder.addAll(parseErrors(parsed.types().definitions(), typeResolver));
        } catch (RuntimeException e) {
            throw new ConjureRuntimeException(String.format("Encountered error trying to parse file '%s'", annotatedParsed.sourceFile()), e);
        }
    });
    ConjureDefinition definition = ConjureDefinition.builder().version(Conjure.SUPPORTED_IR_VERSION).types(typesBuilder.build()).errors(errorsBuilder.build()).services(servicesBuilder.build()).build();
    ConjureDefinitionValidator.validateAll(definition);
    return definition;
}
Also used : ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) TypeName(com.palantir.conjure.spec.TypeName) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Example 2 with ConjureSourceFile

use of com.palantir.conjure.parser.ConjureSourceFile in project conjure by palantir.

the class ConjureParserUtils method innerParseImportObjects.

private static Map<TypeName, TypeDefinition> innerParseImportObjects(Map<Namespace, ConjureImports> conjureImports, Map<String, AnnotatedConjureSourceFile> externalTypes, Set<String> loadedFiles) {
    Map<TypeName, TypeDefinition> allDefinitions = new HashMap<>();
    conjureImports.values().forEach(conjureImport -> {
        String pathKey = conjureImport.absoluteFile().orElseThrow(() -> new SafeIllegalStateException("Absolute file MUST be resolved as part of parsing stage")).getAbsolutePath();
        // These structures are potentially recursive; load in any given conjure file once
        if (loadedFiles.contains(pathKey)) {
            return;
        }
        loadedFiles.add(pathKey);
        AnnotatedConjureSourceFile annotatedConjureSourceFile = externalTypes.get(pathKey);
        Preconditions.checkNotNull(annotatedConjureSourceFile, "Couldn't find import", UnsafeArg.of("file", conjureImport.file()));
        ConjureSourceFile conjureDef = annotatedConjureSourceFile.conjureSourceFile();
        Map<Namespace, String> importProviders = annotatedConjureSourceFile.importProviders();
        ReferenceTypeResolver importTypeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(conjureDef.types(), importProviders, externalTypes);
        allDefinitions.putAll(parseObjects(conjureDef.types(), importTypeResolver));
        allDefinitions.putAll(innerParseImportObjects(conjureDef.types().conjureImports(), externalTypes, loadedFiles));
    });
    return allDefinitions;
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) PathString(com.palantir.conjure.parser.services.PathString) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) Namespace(com.palantir.conjure.parser.types.names.Namespace) ErrorNamespace(com.palantir.conjure.spec.ErrorNamespace) TypeDefinition(com.palantir.conjure.spec.TypeDefinition)

Example 3 with ConjureSourceFile

use of com.palantir.conjure.parser.ConjureSourceFile in project conjure by palantir.

the class ConjureParserUtils method parseConjureDef.

static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs, SafetyDeclarationRequirements safetyDeclarations) {
    ImmutableList.Builder<ServiceDefinition> servicesBuilder = ImmutableList.builder();
    ImmutableList.Builder<ErrorDefinition> errorsBuilder = ImmutableList.builder();
    ImmutableList.Builder<TypeDefinition> typesBuilder = ImmutableList.builder();
    annotatedParsedDefs.values().forEach(annotatedParsed -> {
        ConjureSourceFile parsed = annotatedParsed.conjureSourceFile();
        try {
            ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(parsed.types(), annotatedParsed.importProviders(), annotatedParsedDefs);
            // Resolve objects first, so we can use them in service validations
            Map<TypeName, TypeDefinition> objects = parseObjects(parsed.types(), typeResolver);
            Map<TypeName, TypeDefinition> importedObjects = parseImportObjects(parsed.types().conjureImports(), annotatedParsedDefs);
            Map<TypeName, TypeDefinition> allObjects = new HashMap<>();
            allObjects.putAll(objects);
            allObjects.putAll(importedObjects);
            DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(allObjects);
            parsed.services().forEach((serviceName, service) -> {
                servicesBuilder.add(parseService(service, TypeName.of(serviceName.name(), parseConjurePackage(service.conjurePackage())), typeResolver, dealiasingVisitor));
            });
            typesBuilder.addAll(objects.values());
            errorsBuilder.addAll(parseErrors(parsed.types().definitions(), typeResolver));
        } catch (RuntimeException e) {
            throw new ConjureRuntimeException(String.format("Encountered error trying to parse file '%s'", annotatedParsed.sourceFile()), e);
        }
    });
    ConjureDefinition definition = ConjureDefinition.builder().version(Conjure.SUPPORTED_IR_VERSION).types(typesBuilder.build()).errors(errorsBuilder.build()).services(servicesBuilder.build()).build();
    ConjureDefinitionValidator.validateAll(definition, safetyDeclarations);
    return definition;
}
Also used : ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) TypeName(com.palantir.conjure.spec.TypeName) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition)

Example 4 with ConjureSourceFile

use of com.palantir.conjure.parser.ConjureSourceFile in project conjure by palantir.

the class ServiceDefinitionTests method testParseConjureFile.

@Test
@SuppressWarnings("deprecation")
public void testParseConjureFile() throws IOException {
    ConjureSourceFile def = ConjureParser.parse(new File("src/test/resources/test-service.yml"));
    assertThat(def).isEqualTo(ConjureSourceFile.builder().types(TypesDefinition.builder().putImports(TypeName.of("ResourceIdentifier"), ExternalTypeDefinition.javaType("com.palantir.ri.ResourceIdentifier", PrimitiveType.STRING)).definitions(NamedTypesDefinition.builder().defaultConjurePackage(ConjurePackage.of("test.api")).putObjects(TypeName.of("SimpleObject"), ObjectTypeDefinition.builder().putFields(FieldName.of("stringField"), FieldDefinition.of(PrimitiveType.STRING)).build()).putObjects(TypeName.of("StringAlias"), AliasTypeDefinition.builder().alias(PrimitiveType.STRING).build()).build()).build()).putServices(TypeName.of("MyTestService"), ServiceDefinition.builder().doNotUseName("Test Service").conjurePackage(ConjurePackage.of("test.api")).putEndpoints("get", EndpointDefinition.builder().http(RequestLineDefinition.of("GET", PathString.of("/get"))).build()).putEndpoints("post", EndpointDefinition.builder().http(RequestLineDefinition.of("POST", PathString.of("/post"))).addTags("test-tag").args(ImmutableMap.of(ParameterName.of("foo"), ArgumentDefinition.builder().paramId(ParameterName.of("Foo")).paramType(ArgumentDefinition.ParamType.HEADER).type(LocalReferenceType.of(TypeName.of("StringAlias"))).addTags("safe").build())).build()).build()).build());
}
Also used : ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) File(java.io.File) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) Test(org.junit.jupiter.api.Test)

Aggregations

ConjureSourceFile (com.palantir.conjure.parser.ConjureSourceFile)4 ReferenceTypeResolver (com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver)3 AnnotatedConjureSourceFile (com.palantir.conjure.parser.AnnotatedConjureSourceFile)3 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)3 TypeName (com.palantir.conjure.spec.TypeName)3 HashMap (java.util.HashMap)3 ImmutableList (com.google.common.collect.ImmutableList)2 ConjureRuntimeException (com.palantir.conjure.exceptions.ConjureRuntimeException)2 ConjureDefinition (com.palantir.conjure.spec.ConjureDefinition)2 ErrorDefinition (com.palantir.conjure.spec.ErrorDefinition)2 ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)2 DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)2 PathString (com.palantir.conjure.parser.services.PathString)1 Namespace (com.palantir.conjure.parser.types.names.Namespace)1 ErrorNamespace (com.palantir.conjure.spec.ErrorNamespace)1 SafeIllegalStateException (com.palantir.logsafe.exceptions.SafeIllegalStateException)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1