Skip to content

Commit

Permalink
Use conditional logic to select the error message for an unmatched id…
Browse files Browse the repository at this point in the history
…entifier based on whether it was a case-insensitive match (#1406)

* Copy changes in 598-non-resolved-case-mismatch-warning into a fresh branch.

* Spotless.

* Move inner class to outer.  New enum.  Factory methods.  Optionals.

* Spotless.

* More optimization.

* More optimization.  Spotless.

* Don't return a warning.  Only use conditional logic to determine what the error message should be based on whether it's due to a case insensitive match.

* Reverse change to make warning public, since this is no longer needed.

* Small tweak.
  • Loading branch information
lukedegruchy authored Aug 30, 2024
1 parent 100ebe3 commit 05c2bd3
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ public void removeExpression(ExpressionDef expDef) {
}
}

public Element resolve(String identifier) {
public ResolvedIdentifierContext resolve(String identifier) {
return compiledLibrary.resolve(identifier);
}

Expand Down Expand Up @@ -2370,95 +2370,99 @@ public Expression resolveIdentifier(String identifier, boolean mustResolve) {
return operandRef;
}

Element element = resolve(identifier);
final ResolvedIdentifierContext resolvedIdentifierContext = resolve(identifier);
final Optional<Element> optElement = resolvedIdentifierContext.getExactMatchElement();

if (element instanceof ExpressionDef) {
checkLiteralContext();
ExpressionRef expressionRef = of.createExpressionRef().withName(((ExpressionDef) element).getName());
expressionRef.setResultType(getExpressionDefResultType((ExpressionDef) element));
if (expressionRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to expression %s because its definition contains errors.",
expressionRef.getName()));
if (optElement.isPresent()) {
final Element element = optElement.get();
if (element instanceof ExpressionDef) {
checkLiteralContext();
ExpressionRef expressionRef = of.createExpressionRef().withName(((ExpressionDef) element).getName());
expressionRef.setResultType(getExpressionDefResultType((ExpressionDef) element));
if (expressionRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to expression %s because its definition contains errors.",
expressionRef.getName()));
}
return expressionRef;
}
return expressionRef;
}

if (element instanceof ParameterDef) {
checkLiteralContext();
ParameterRef parameterRef = of.createParameterRef().withName(((ParameterDef) element).getName());
parameterRef.setResultType(element.getResultType());
if (parameterRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to parameter %s because its definition contains errors.",
parameterRef.getName()));
if (element instanceof ParameterDef) {
checkLiteralContext();
ParameterRef parameterRef = of.createParameterRef().withName(((ParameterDef) element).getName());
parameterRef.setResultType(element.getResultType());
if (parameterRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to parameter %s because its definition contains errors.",
parameterRef.getName()));
}
return parameterRef;
}
return parameterRef;
}

if (element instanceof ValueSetDef) {
checkLiteralContext();
ValueSetRef valuesetRef = of.createValueSetRef().withName(((ValueSetDef) element).getName());
valuesetRef.setResultType(element.getResultType());
if (valuesetRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to valueset %s because its definition contains errors.",
valuesetRef.getName()));
}
if (isCompatibleWith("1.5")) {
valuesetRef.setPreserve(true);
if (element instanceof ValueSetDef) {
checkLiteralContext();
ValueSetRef valuesetRef = of.createValueSetRef().withName(((ValueSetDef) element).getName());
valuesetRef.setResultType(element.getResultType());
if (valuesetRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to valueset %s because its definition contains errors.",
valuesetRef.getName()));
}
if (isCompatibleWith("1.5")) {
valuesetRef.setPreserve(true);
}
return valuesetRef;
}
return valuesetRef;
}

if (element instanceof CodeSystemDef) {
checkLiteralContext();
CodeSystemRef codesystemRef = of.createCodeSystemRef().withName(((CodeSystemDef) element).getName());
codesystemRef.setResultType(element.getResultType());
if (codesystemRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to codesystem %s because its definition contains errors.",
codesystemRef.getName()));
if (element instanceof CodeSystemDef) {
checkLiteralContext();
CodeSystemRef codesystemRef = of.createCodeSystemRef().withName(((CodeSystemDef) element).getName());
codesystemRef.setResultType(element.getResultType());
if (codesystemRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to codesystem %s because its definition contains errors.",
codesystemRef.getName()));
}
return codesystemRef;
}
return codesystemRef;
}

if (element instanceof CodeDef) {
checkLiteralContext();
CodeRef codeRef = of.createCodeRef().withName(((CodeDef) element).getName());
codeRef.setResultType(element.getResultType());
if (codeRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to code %s because its definition contains errors.",
codeRef.getName()));
if (element instanceof CodeDef) {
checkLiteralContext();
CodeRef codeRef = of.createCodeRef().withName(((CodeDef) element).getName());
codeRef.setResultType(element.getResultType());
if (codeRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to code %s because its definition contains errors.",
codeRef.getName()));
}
return codeRef;
}
return codeRef;
}

if (element instanceof ConceptDef) {
checkLiteralContext();
ConceptRef conceptRef = of.createConceptRef().withName(((ConceptDef) element).getName());
conceptRef.setResultType(element.getResultType());
if (conceptRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to concept %s because its definition contains error.",
conceptRef.getName()));
if (element instanceof ConceptDef) {
checkLiteralContext();
ConceptRef conceptRef = of.createConceptRef().withName(((ConceptDef) element).getName());
conceptRef.setResultType(element.getResultType());
if (conceptRef.getResultType() == null) {
// ERROR:
throw new IllegalArgumentException(String.format(
"Could not validate reference to concept %s because its definition contains error.",
conceptRef.getName()));
}
return conceptRef;
}
return conceptRef;
}

if (element instanceof IncludeDef) {
checkLiteralContext();
LibraryRef libraryRef = new LibraryRef();
libraryRef.setLocalId(of.nextId());
libraryRef.setLibraryName(((IncludeDef) element).getLocalIdentifier());
return libraryRef;
if (element instanceof IncludeDef) {
checkLiteralContext();
LibraryRef libraryRef = new LibraryRef();
libraryRef.setLocalId(of.nextId());
libraryRef.setLibraryName(((IncludeDef) element).getLocalIdentifier());
return libraryRef;
}
}

// If no other resolution occurs, and we are in a specific context, and there is a parameter with the same name
Expand All @@ -2477,8 +2481,11 @@ public Expression resolveIdentifier(String identifier, boolean mustResolve) {

if (mustResolve) {
// ERROR:
throw new IllegalArgumentException(
String.format("Could not resolve identifier %s in the current library.", identifier));
final String exceptionMessage = resolvedIdentifierContext
.warnCaseInsensitiveIfApplicable()
.orElse(String.format("Could not resolve identifier %s in the current library.", identifier));

throw new IllegalArgumentException(exceptionMessage);
}

return null;
Expand Down Expand Up @@ -2524,9 +2531,11 @@ private static String lookupElementWarning(Object element) {
*/
public ParameterRef resolveImplicitContext() {
if (!inLiteralContext() && inSpecificContext()) {
Element contextElement = resolve(currentExpressionContext());
if (contextElement instanceof ParameterDef) {
ParameterDef contextParameter = (ParameterDef) contextElement;
ResolvedIdentifierContext resolvedIdentifierContext = resolve(currentExpressionContext());
final Optional<ParameterDef> optParameterDef =
resolvedIdentifierContext.getElementOfType(ParameterDef.class);
if (optParameterDef.isPresent()) {
final ParameterDef contextParameter = optParameterDef.get();

checkLiteralContext();
ParameterRef parameterRef = of.createParameterRef().withName(contextParameter.getName());
Expand Down Expand Up @@ -2903,53 +2912,62 @@ public Expression resolveAccessor(Expression left, String memberIdentifier) {
String libraryName = ((LibraryRef) left).getLibraryName();
CompiledLibrary referencedLibrary = resolveLibrary(libraryName);

Element element = referencedLibrary.resolve(memberIdentifier);
ResolvedIdentifierContext resolvedIdentifierContext = referencedLibrary.resolve(memberIdentifier);

if (element instanceof ExpressionDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ExpressionDef) element).getAccessLevel());
Expression result =
of.createExpressionRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(getExpressionDefResultType((ExpressionDef) element));
return result;
}
final Optional<Element> optElement = resolvedIdentifierContext.getExactMatchElement();

if (element instanceof ParameterDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ParameterDef) element).getAccessLevel());
Expression result =
of.createParameterRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}
if (optElement.isPresent()) {
final Element element = optElement.get();

if (element instanceof ValueSetDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ValueSetDef) element).getAccessLevel());
ValueSetRef result =
of.createValueSetRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}
if (element instanceof ExpressionDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ExpressionDef) element).getAccessLevel());
Expression result = of.createExpressionRef()
.withLibraryName(libraryName)
.withName(memberIdentifier);
result.setResultType(getExpressionDefResultType((ExpressionDef) element));
return result;
}

if (element instanceof CodeSystemDef) {
checkAccessLevel(libraryName, memberIdentifier, ((CodeSystemDef) element).getAccessLevel());
CodeSystemRef result =
of.createCodeSystemRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}
if (element instanceof ParameterDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ParameterDef) element).getAccessLevel());
Expression result =
of.createParameterRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}

if (element instanceof CodeDef) {
checkAccessLevel(libraryName, memberIdentifier, ((CodeDef) element).getAccessLevel());
CodeRef result = of.createCodeRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}
if (element instanceof ValueSetDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ValueSetDef) element).getAccessLevel());
ValueSetRef result =
of.createValueSetRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}

if (element instanceof ConceptDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ConceptDef) element).getAccessLevel());
ConceptRef result =
of.createConceptRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
if (element instanceof CodeSystemDef) {
checkAccessLevel(libraryName, memberIdentifier, ((CodeSystemDef) element).getAccessLevel());
CodeSystemRef result = of.createCodeSystemRef()
.withLibraryName(libraryName)
.withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}

if (element instanceof CodeDef) {
checkAccessLevel(libraryName, memberIdentifier, ((CodeDef) element).getAccessLevel());
CodeRef result =
of.createCodeRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}

if (element instanceof ConceptDef) {
checkAccessLevel(libraryName, memberIdentifier, ((ConceptDef) element).getAccessLevel());
ConceptRef result =
of.createConceptRef().withLibraryName(libraryName).withName(memberIdentifier);
result.setResultType(element.getResultType());
return result;
}
}

// ERROR:
Expand Down
Loading

0 comments on commit 05c2bd3

Please sign in to comment.