Search in sources :

Example 1 with DealiasingTypeVisitor

use of com.palantir.conjure.visitor.DealiasingTypeVisitor 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 DealiasingTypeVisitor

use of com.palantir.conjure.visitor.DealiasingTypeVisitor in project conjure by palantir.

the class ConjureParserUtils method parseEndpoint.

private static EndpointDefinition parseEndpoint(String name, com.palantir.conjure.parser.services.EndpointDefinition def, PathString basePath, Optional<AuthType> defaultAuth, ReferenceTypeResolver typeResolver, DealiasingTypeVisitor dealiasingVisitor) {
    HttpPath httpPath = parseHttpPath(def, basePath);
    EndpointDefinition endpoint = EndpointDefinition.builder().endpointName(EndpointName.of(name)).httpMethod(HttpMethod.valueOf(def.http().method())).httpPath(httpPath).auth(def.auth().map(ConjureParserUtils::parseAuthType).orElse(defaultAuth)).args(parseArgs(def.args(), httpPath, typeResolver)).tags(def.tags().stream().peek(tag -> Preconditions.checkArgument(!tag.isEmpty(), "tag must not be empty")).collect(Collectors.toSet())).markers(parseMarkers(def.markers(), typeResolver)).returns(def.returns().map(t -> t.visit(new ConjureTypeParserVisitor(typeResolver)))).docs(def.docs().map(Documentation::of)).deprecated(def.deprecated().map(Documentation::of)).build();
    EndpointDefinitionValidator.validateAll(endpoint, dealiasingVisitor);
    return endpoint;
}
Also used : HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) PathString(com.palantir.conjure.parser.services.PathString) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinitionValidator(com.palantir.conjure.defs.validator.ServiceDefinitionValidator) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) EnumValueDefinitionValidator(com.palantir.conjure.defs.validator.EnumValueDefinitionValidator) ConjureDefinitionValidator(com.palantir.conjure.defs.validator.ConjureDefinitionValidator) NamedTypesDefinition(com.palantir.conjure.parser.types.NamedTypesDefinition) Map(java.util.Map) ObjectDefinitionValidator(com.palantir.conjure.defs.validator.ObjectDefinitionValidator) ArgumentName(com.palantir.conjure.spec.ArgumentName) AnnotatedConjureSourceFile(com.palantir.conjure.parser.AnnotatedConjureSourceFile) ParameterId(com.palantir.conjure.spec.ParameterId) EndpointDefinitionValidator(com.palantir.conjure.defs.validator.EndpointDefinitionValidator) LogSafetyDefinition(com.palantir.conjure.parser.LogSafetyDefinition) ObjectDefinition(com.palantir.conjure.spec.ObjectDefinition) ConjureImports(com.palantir.conjure.parser.types.reference.ConjureImports) AuthType(com.palantir.conjure.spec.AuthType) Collection(java.util.Collection) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Set(java.util.Set) BodyParameterType(com.palantir.conjure.spec.BodyParameterType) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) FieldDefinitionValidator(com.palantir.conjure.defs.validator.FieldDefinitionValidator) ParameterType(com.palantir.conjure.spec.ParameterType) List(java.util.List) AliasDefinition(com.palantir.conjure.spec.AliasDefinition) UnsafeArg(com.palantir.logsafe.UnsafeArg) ReferenceTypeResolver(com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver) Documentation(com.palantir.conjure.spec.Documentation) ConjureRuntimeException(com.palantir.conjure.exceptions.ConjureRuntimeException) Optional(java.util.Optional) HttpPathValidator(com.palantir.conjure.defs.validator.HttpPathValidator) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) HttpMethod(com.palantir.conjure.spec.HttpMethod) LogSafety(com.palantir.conjure.spec.LogSafety) EnumDefinitionValidator(com.palantir.conjure.defs.validator.EnumDefinitionValidator) FieldNameValidator(com.palantir.conjure.defs.validator.FieldNameValidator) ErrorDefinition(com.palantir.conjure.spec.ErrorDefinition) Type(com.palantir.conjure.spec.Type) PathParameterType(com.palantir.conjure.spec.PathParameterType) TypeDefinitionVisitor(com.palantir.conjure.visitor.TypeDefinitionVisitor) HashMap(java.util.HashMap) Namespace(com.palantir.conjure.parser.types.names.Namespace) HttpPath(com.palantir.conjure.spec.HttpPath) ConjureIllegalArgumentException(com.palantir.conjure.exceptions.ConjureIllegalArgumentException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) ErrorNamespace(com.palantir.conjure.spec.ErrorNamespace) TypeNameValidator(com.palantir.conjure.defs.validator.TypeNameValidator) PackageValidator(com.palantir.conjure.defs.validator.PackageValidator) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) ErrorDefinitionValidator(com.palantir.conjure.defs.validator.ErrorDefinitionValidator) ParameterName(com.palantir.conjure.parser.services.ParameterName) EndpointName(com.palantir.conjure.spec.EndpointName) QueryParameterType(com.palantir.conjure.spec.QueryParameterType) TypeName(com.palantir.conjure.spec.TypeName) FieldName(com.palantir.conjure.spec.FieldName) ConjureSourceFile(com.palantir.conjure.parser.ConjureSourceFile) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) ConjurePackage(com.palantir.conjure.parser.types.names.ConjurePackage) UnionDefinitionValidator(com.palantir.conjure.defs.validator.UnionDefinitionValidator) HeaderParameterType(com.palantir.conjure.spec.HeaderParameterType) Preconditions(com.palantir.logsafe.Preconditions) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) HttpPath(com.palantir.conjure.spec.HttpPath) Documentation(com.palantir.conjure.spec.Documentation) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition)

Example 3 with DealiasingTypeVisitor

use of com.palantir.conjure.visitor.DealiasingTypeVisitor in project conjure by palantir.

the class EndpointDefinitionTest method testNoOptionalBinaryBodyParamValidator_throughAlias.

@Test
public void testNoOptionalBinaryBodyParamValidator_throughAlias() {
    TypeName typeName = TypeName.of("OptionalBinary", "foo");
    EndpointDefinition definition = EndpointDefinition.builder().args(bodyArgBuilder().argName(ArgumentName.of("someName")).type(Type.reference(typeName)).build()).endpointName(ENDPOINT_NAME).httpMethod(HttpMethod.POST).httpPath(HttpPath.of("/a/path")).build();
    DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(ImmutableMap.of(typeName, TypeDefinition.alias(AliasDefinition.builder().typeName(typeName).alias(Type.optional(OptionalType.of(Type.primitive(PrimitiveType.BINARY)))).docs(Documentation.of("")).build())));
    assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition, dealiasingVisitor)).isInstanceOf(IllegalStateException.class).hasMessage("Endpoint BODY argument must not be optional<binary> or alias thereof: " + "test{http: POST /a/path}");
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) Test(org.junit.jupiter.api.Test)

Example 4 with DealiasingTypeVisitor

use of com.palantir.conjure.visitor.DealiasingTypeVisitor in project conjure by palantir.

the class EndpointDefinitionTest method testComplexHeaderObject.

@Test
public void testComplexHeaderObject() {
    TypeName typeName = TypeName.of("SomeObject", "com.palantir.foo");
    EndpointDefinition.Builder definition = EndpointDefinition.builder().args(ArgumentDefinition.builder().argName(ArgumentName.of("someName")).type(Type.reference(typeName)).paramType(ParameterType.header(HeaderParameterType.of(ParameterId.of("SomeId")))).build()).endpointName(ENDPOINT_NAME).httpMethod(HttpMethod.GET).httpPath(HttpPath.of("/a/path"));
    DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(ImmutableMap.of(typeName, TypeDefinition.object(ObjectDefinition.of(typeName, ImmutableList.of(), Documentation.of("")))));
    assertThatThrownBy(() -> EndpointDefinitionValidator.validateAll(definition.build(), dealiasingVisitor)).isInstanceOf(IllegalStateException.class).hasMessage("Header parameters must be enums, primitives, aliases or optional primitive: " + "\"someName\" is not allowed on endpoint test{http: GET /a/path}");
}
Also used : TypeName(com.palantir.conjure.spec.TypeName) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) Test(org.junit.jupiter.api.Test)

Example 5 with DealiasingTypeVisitor

use of com.palantir.conjure.visitor.DealiasingTypeVisitor in project conjure-python by palantir.

the class ConjurePythonGenerator method generate.

private List<PythonFile> generate(ConjureDefinition conjureDefinition) {
    DealiasingTypeVisitor dealiasingTypeVisitor = new DealiasingTypeVisitor(conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity())));
    PackageNameProcessor implPackageNameProcessor = new ConstantPackageNameProcessor(config.pythonicPackageName().orElse(""));
    List<PackageNameProcessor> packageNameProcessors = new ArrayList<>();
    packageNameProcessors.add(new TwoComponentStrippingPackageNameProcessor());
    packageNameProcessors.add(FlatteningPackageNameProcessor.INSTANCE);
    packageNameProcessors.addAll(config.pythonicPackageName().map(pythonPackageName -> ImmutableSet.of(new PrefixingPackageNameProcessor(pythonPackageName))).orElseGet(ImmutableSet::of));
    PackageNameProcessor definitionPackageNameProcessor = new CompoundPackageNameProcessor(packageNameProcessors);
    TypeNameProcessor implTypeNameProcessor = new PackagePrependingTypeNameProcessor(new CompoundPackageNameProcessor(ImmutableList.of(new TwoComponentStrippingPackageNameProcessor(), FlatteningPackageNameProcessor.INSTANCE)));
    TypeNameProcessor definitionTypeNameProcessor = NameOnlyTypeNameProcessor.INSTANCE;
    List<PythonFile> pythonFiles = new ArrayList<>();
    pythonFiles.add(getImplPythonFile(conjureDefinition, dealiasingTypeVisitor, implPackageNameProcessor, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor));
    List<PythonFile> initFiles = getInitFiles(conjureDefinition, implTypeNameProcessor, definitionPackageNameProcessor, definitionTypeNameProcessor);
    pythonFiles.addAll(initFiles);
    PythonPackage rootPackage = PythonPackage.of(buildPackageNameProcessor().process("."));
    pythonFiles.add(getRootInit(initFiles.stream().map(PythonFile::pythonPackage).collect(Collectors.toSet()), rootPackage));
    return pythonFiles;
}
Also used : ConstantPackageNameProcessor(com.palantir.conjure.python.processors.packagename.ConstantPackageNameProcessor) FlatteningPackageNameProcessor(com.palantir.conjure.python.processors.packagename.FlatteningPackageNameProcessor) PackageNameProcessor(com.palantir.conjure.python.processors.packagename.PackageNameProcessor) TwoComponentStrippingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.TwoComponentStrippingPackageNameProcessor) PrefixingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.PrefixingPackageNameProcessor) CompoundPackageNameProcessor(com.palantir.conjure.python.processors.packagename.CompoundPackageNameProcessor) PythonPackage(com.palantir.conjure.python.poet.PythonPackage) ArrayList(java.util.ArrayList) DealiasingTypeVisitor(com.palantir.conjure.visitor.DealiasingTypeVisitor) TwoComponentStrippingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.TwoComponentStrippingPackageNameProcessor) PythonFile(com.palantir.conjure.python.poet.PythonFile) ConstantPackageNameProcessor(com.palantir.conjure.python.processors.packagename.ConstantPackageNameProcessor) PrefixingPackageNameProcessor(com.palantir.conjure.python.processors.packagename.PrefixingPackageNameProcessor) PackagePrependingTypeNameProcessor(com.palantir.conjure.python.processors.typename.PackagePrependingTypeNameProcessor) NameOnlyTypeNameProcessor(com.palantir.conjure.python.processors.typename.NameOnlyTypeNameProcessor) TypeNameProcessor(com.palantir.conjure.python.processors.typename.TypeNameProcessor) PackagePrependingTypeNameProcessor(com.palantir.conjure.python.processors.typename.PackagePrependingTypeNameProcessor) CompoundPackageNameProcessor(com.palantir.conjure.python.processors.packagename.CompoundPackageNameProcessor)

Aggregations

DealiasingTypeVisitor (com.palantir.conjure.visitor.DealiasingTypeVisitor)6 TypeName (com.palantir.conjure.spec.TypeName)5 ImmutableList (com.google.common.collect.ImmutableList)3 ReferenceTypeResolver (com.palantir.conjure.defs.ConjureTypeParserVisitor.ReferenceTypeResolver)3 ConjureRuntimeException (com.palantir.conjure.exceptions.ConjureRuntimeException)3 AnnotatedConjureSourceFile (com.palantir.conjure.parser.AnnotatedConjureSourceFile)3 ConjureSourceFile (com.palantir.conjure.parser.ConjureSourceFile)3 ConjureDefinition (com.palantir.conjure.spec.ConjureDefinition)3 EndpointDefinition (com.palantir.conjure.spec.EndpointDefinition)3 ErrorDefinition (com.palantir.conjure.spec.ErrorDefinition)3 ServiceDefinition (com.palantir.conjure.spec.ServiceDefinition)3 TypeDefinition (com.palantir.conjure.spec.TypeDefinition)3 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 ConjureDefinitionValidator (com.palantir.conjure.defs.validator.ConjureDefinitionValidator)1 EndpointDefinitionValidator (com.palantir.conjure.defs.validator.EndpointDefinitionValidator)1 EnumDefinitionValidator (com.palantir.conjure.defs.validator.EnumDefinitionValidator)1 EnumValueDefinitionValidator (com.palantir.conjure.defs.validator.EnumValueDefinitionValidator)1 ErrorDefinitionValidator (com.palantir.conjure.defs.validator.ErrorDefinitionValidator)1