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;
}
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;
}
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}");
}
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}");
}
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;
}
Aggregations