Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
aashikam committed Aug 18, 2023
1 parent 4d3c129 commit 3808e11
Showing 1 changed file with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,41 @@ private void validateAttachPoint(SyntaxNodeAnalysisContext context) {
SemanticModel semanticModel = context.semanticModel();
ServiceDeclarationNode serviceDeclarationNode = (ServiceDeclarationNode) context.node();
Optional<Symbol> symbol = semanticModel.symbol(serviceDeclarationNode);

if (symbol.isPresent()) {
ServiceDeclarationSymbol serviceDeclarationSymbol = (ServiceDeclarationSymbol) symbol.get();
Optional<ServiceAttachPoint> serviceNameAttachPoint = serviceDeclarationSymbol.attachPoint();
List<AnnotationSymbol> annotations = serviceDeclarationSymbol.annotations();

// Nats gets the subject name from either the service name or the
// service config, so either one of them should be present.
if (annotations.isEmpty()) {
if (serviceNameAttachPoint.isEmpty()) {
// Case 1: No service name and no annotation
reportError(context, CompilationErrors.NO_ANNOTATION, serviceDeclarationNode);
} else if (serviceNameAttachPoint.get().kind() != ServiceAttachPointKind.STRING_LITERAL) {
// Case 2: Service name is not a string and no annotation
reportError(context, CompilationErrors.INVALID_SERVICE_ATTACH_POINT, serviceDeclarationNode);
}
} else if (!hasServiceConfig(annotations)) {
if (serviceNameAttachPoint.isEmpty()) {
// Case 1: No service name and no annotation
reportError(context, CompilationErrors.NO_ANNOTATION, serviceDeclarationNode);
} else if (serviceNameAttachPoint.get().kind() != ServiceAttachPointKind.STRING_LITERAL) {
// Case 2: Service name is not a string and no annotation
reportError(context, CompilationErrors.INVALID_SERVICE_ATTACH_POINT, serviceDeclarationNode);
}
boolean serviceNameIsStringLiteral = serviceNameAttachPoint.isPresent() &&
serviceNameAttachPoint.get().kind() == ServiceAttachPointKind.STRING_LITERAL;

if (annotations.isEmpty() && !serviceNameIsStringLiteral) {
// Case 1: No service name and no annotation
reportError(context, CompilationErrors.INVALID_SERVICE_ATTACH_POINT, serviceDeclarationNode);
} else if (!hasServiceConfig(annotations) && !serviceNameIsStringLiteral) {
// Case 2: Service name is not a string and no annotation
reportError(context, CompilationErrors.NO_ANNOTATION, serviceDeclarationNode);
}
}
}

private void reportError(SyntaxNodeAnalysisContext context, CompilationErrors error, Node locationNode) {
context.reportDiagnostic(PluginUtils.getDiagnostic(error, DiagnosticSeverity.ERROR, locationNode.location()));
}

private boolean hasServiceConfig(List<AnnotationSymbol> annotationSymbols) {
boolean flag = false;
for (AnnotationSymbol annotationSymbol : annotationSymbols) {
Optional<ModuleSymbol> moduleSymbolOptional = annotationSymbol.getModule();
if (moduleSymbolOptional.isPresent()) {
ModuleSymbol moduleSymbol = moduleSymbolOptional.get();
if (moduleSymbol.id().orgName().equals(PluginConstants.PACKAGE_ORG) ||
moduleSymbol.id().moduleName().equals(PluginConstants.PACKAGE_PREFIX)) {
// not checking name as nats has only two annotations and only one is allowed on services.
flag = true;
if (PluginConstants.PACKAGE_ORG.equals(moduleSymbol.id().orgName()) &&
PluginConstants.PACKAGE_PREFIX.equals(moduleSymbol.id().moduleName())) {
// not checking name as rabbitmq has only two annotations and only one is allowed on services.
return true;
}
}
}
return flag;
}

private void reportError(SyntaxNodeAnalysisContext context, CompilationErrors error, Node locationNode) {
context.reportDiagnostic(PluginUtils.getDiagnostic(error, DiagnosticSeverity.ERROR, locationNode.location()));
return false;
}
}

0 comments on commit 3808e11

Please sign in to comment.