Skip to content

Commit

Permalink
Merge branch 'master' into nullable-annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow-Devil authored Sep 22, 2024
2 parents fb77d66 + 4dbc2fb commit ca4a9bc
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,16 @@ public static String cleanupPathSeparators(String name) {

public static String rewriteVirtualCallTypeName(String value, BType objectType) {
objectType = getImpliedType(objectType);
// The call name will be in the format of`objectTypeName.funcName` for attached functions of imported modules.
// Therefore, We need to remove the type name.
if (!objectType.tsymbol.name.value.isEmpty() && value.startsWith(objectType.tsymbol.name.value)) {
value = value.replace(objectType.tsymbol.name.value + ".", "").trim();
String typeName = objectType.tsymbol.name.value;
Name originalName = objectType.tsymbol.originalName;
if (value.startsWith(typeName)) {
// The call name will be in the format of`objectTypeName.funcName` for attached functions of imported
// modules. Therefore, We need to remove the type name.
value = value.replace(typeName + ".", "").trim();
} else if (originalName != null && value.startsWith(originalName.value)) {
// The call name will be in the format of`objectTypeOriginalName.funcName` for attached functions of
// object definitions. Therefore, We need to remove it.
value = value.replace(originalName + ".", "").trim();
}
return Utils.encodeFunctionIdentifier(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private XMLToRecordConverter() {}

public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields,
@Nullable String textFieldName, boolean withNameSpaces) {
@Nullable String textFieldName, boolean withNameSpaces, boolean withoutAttributes,
boolean withoutAttributeAnnot) {
Map<String, NonTerminalNode> recordToTypeDescNodes = new LinkedHashMap<>();
Map<String, AnnotationNode> recordToAnnotationNodes = new LinkedHashMap<>();
Map<String, Element> recordToElementNodes = new LinkedHashMap<>();
Expand All @@ -117,7 +118,8 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD

Element rootElement = doc.getDocumentElement();
generateRecords(rootElement, isClosed, recordToTypeDescNodes, recordToAnnotationNodes,
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces);
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpaces, withoutAttributes,
withoutAttributeAnnot);
} catch (ParserConfigurationException parserConfigurationException) {
DiagnosticMessage message = DiagnosticMessage.xmlToRecordConverter100(null);
diagnosticMessages.add(message);
Expand Down Expand Up @@ -188,15 +190,16 @@ public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeD
*/
public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields) {
return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true);
return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true, false, false);
}

private static void generateRecords(Element xmlElement, boolean isClosed,
Map<String, NonTerminalNode> recordToTypeDescNodes,
Map<String, AnnotationNode> recordToAnnotationsNodes,
Map<String, Element> recordToElementNodes,
List<DiagnosticMessage> diagnosticMessages,
String textFieldName, boolean withNameSpace) {
String textFieldName, boolean withNameSpace, boolean withoutAttributes,
boolean withoutAttributeAnnot) {
Token recordKeyWord = AbstractNodeFactory.createToken(SyntaxKind.RECORD_KEYWORD);
Token bodyStartDelimiter = AbstractNodeFactory.createToken(isClosed ? SyntaxKind.OPEN_BRACE_PIPE_TOKEN :
SyntaxKind.OPEN_BRACE_TOKEN);
Expand All @@ -205,7 +208,7 @@ private static void generateRecords(Element xmlElement, boolean isClosed,

List<Node> recordFields = getRecordFieldsForXMLElement(xmlElement, isClosed, recordToTypeDescNodes,
recordToAnnotationsNodes, recordToElementNodes, diagnosticMessages, textFieldName,
withNameSpace);
withNameSpace, withoutAttributes, withoutAttributeAnnot);
if (recordToTypeDescNodes.containsKey(xmlNodeName)) {
RecordTypeDescriptorNode previousRecordTypeDescriptorNode =
(RecordTypeDescriptorNode) recordToTypeDescNodes.get(xmlNodeName);
Expand Down Expand Up @@ -238,7 +241,8 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
Map<String, AnnotationNode> recordToAnnotationNodes,
Map<String, Element> recordToElementNodes,
List<DiagnosticMessage> diagnosticMessages,
String textFieldName, boolean withNameSpace) {
String textFieldName, boolean withNameSpace,
boolean withoutAttributes, boolean withoutAttributeAnnot) {
List<Node> recordFields = new ArrayList<>();

String xmlNodeName = xmlElement.getNodeName();
Expand All @@ -250,16 +254,17 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
Element xmlElementNode = (Element) xmlNode;
boolean isLeafXMLElementNode = isLeafXMLElementNode(xmlElementNode);
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (!isLeafXMLElementNode || xmlAttributesMap.getLength() > 1
if (!isLeafXMLElementNode || (!withoutAttributes && (xmlAttributesMap.getLength() > 1
|| (xmlAttributesMap.getLength() == 1
&& !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) {
&& !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))))) {
generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes,
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace);
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace, withoutAttributes,
withoutAttributeAnnot);
}
Map<String, Boolean> prefixMap = hasMultipleFieldsWithSameName(xmlNodeList,
xmlElementNode.getLocalName());
RecordFieldNode recordField = getRecordField(xmlElementNode, false, withNameSpace,
prefixMap.size() > 1);
prefixMap.size() > 1, withoutAttributes);

if (withNameSpace && xmlElementNode.getPrefix() != null) {
int indexOfRecordFieldNode = IntStream.range(0, recordFields.size())
Expand Down Expand Up @@ -316,18 +321,18 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
AnnotationNode xmlNSNode = getXMLNamespaceNode(prefix, xmlNode.getNodeValue());
recordToAnnotationNodes.put(xmlNodeName, xmlNSNode);
} else if (!isLeafXMLElementNode(xmlElement) && !XMLNS_PREFIX.equals(xmlNode.getPrefix())
&& !XMLNS_PREFIX.equals(xmlNode.getLocalName())) {
&& !XMLNS_PREFIX.equals(xmlNode.getLocalName()) && !withoutAttributes) {
if (elementNames.contains(xmlNode.getNodeName())) {
continue;
}
Node recordField = getRecordField(xmlNode, withNameSpace);
Node recordField = getRecordField(xmlNode, withNameSpace, withoutAttributeAnnot);
recordFields.add(recordField);
}
}
}
int attributeLength = xmlElement.getAttributes().getLength();
org.w3c.dom.Node attributeItem = xmlElement.getAttributes().item(0);
if (isLeafXMLElementNode(xmlElement) && attributeLength > 0) {
if (isLeafXMLElementNode(xmlElement) && attributeLength > 0 && !withoutAttributes) {
if (attributeLength == 1 && attributeItem.getPrefix() != null
&& XMLNS_PREFIX.equals(attributeItem.getPrefix())) {
return recordFields;
Expand All @@ -346,7 +351,7 @@ private static List<Node> getRecordFieldsForXMLElement(Element xmlElement, boole
if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getLocalName())) {
Node recordField = getRecordField(xmlAttributeNode, withNameSpace);
Node recordField = getRecordField(xmlAttributeNode, withNameSpace, withoutAttributeAnnot);
recordFields.add(recordField);
}
}
Expand Down Expand Up @@ -468,7 +473,8 @@ private static List<Node> updateRecordFields(Map<String, RecordFieldNode> previo
}

private static RecordFieldNode getRecordField(Element xmlElementNode, boolean isOptionalField,
boolean withNameSpace, boolean sameFieldExists) {
boolean withNameSpace, boolean sameFieldExists,
boolean withoutAttributes) {
Token typeName;
Token questionMarkToken = AbstractNodeFactory.createToken(SyntaxKind.QUESTION_MARK_TOKEN);
IdentifierToken fieldName =
Expand All @@ -479,7 +485,7 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 ||
(xmlAttributesMap.getLength() == 1
&& XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())))) {
&& XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())) || withoutAttributes)) {
typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue());
} else {
// At the moment all are considered as Objects here
Expand Down Expand Up @@ -508,7 +514,8 @@ private static RecordFieldNode getRecordField(Element xmlElementNode, boolean is
metadataNode, null, fieldTypeName, fieldName, optionalFieldToken, semicolonToken);
}

private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withNamespace) {
private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean withNamespace,
boolean withoutAttributeAnnot) {
Token typeName = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD);
TypeDescriptorNode fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(typeName.kind(), typeName);
IdentifierToken fieldName =
Expand All @@ -521,7 +528,8 @@ private static Node getRecordField(org.w3c.dom.Node xmlAttributeNode, boolean wi
}
annotations.add(getXMLAttributeNode());
NodeList<AnnotationNode> annotationNodes = NodeFactory.createNodeList(annotations);
MetadataNode metadataNode = NodeFactory.createMetadataNode(null, annotationNodes);
MetadataNode metadataNode = withoutAttributeAnnot ? null :
NodeFactory.createMetadataNode(null, annotationNodes);

if (xmlAttributeNode.getPrefix() != null &&
xmlAttributeNode.getPrefix().equals(XMLNS_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public CompletableFuture<XMLToRecordResponse> convert(XMLToRecordRequest request
boolean forceFormatRecordFields = request.getForceFormatRecordFields();
String textFieldName = request.getTextFieldName();
boolean withNameSpace = request.getIsWithNameSpace();
boolean withoutAttributes = request.getWithoutAttributes();
boolean withoutAttributeAnnot = request.getWithoutAttributeAnnot();

return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields,
textFieldName, withNameSpace);
textFieldName, withNameSpace, withoutAttributes, withoutAttributeAnnot);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ public class XMLToRecordRequest {
private final boolean forceFormatRecordFields;
private final String textFieldName;
private final boolean withNameSpace;
private final boolean withoutAttributes;
private final boolean withoutAttributeAnnot;

public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields, @Nullable String textFieldName, boolean withNameSpace) {
boolean forceFormatRecordFields, @Nullable String textFieldName, boolean withNameSpace,
boolean withoutAttributes, boolean withoutAttributeAnnot) {
this.xmlValue = xmlValue;
this.isRecordTypeDesc = isRecordTypeDesc;
this.isClosed = isClosed;
this.forceFormatRecordFields = forceFormatRecordFields;
this.textFieldName = textFieldName;
this.withNameSpace = withNameSpace;
this.withoutAttributes = withoutAttributes;
this.withoutAttributeAnnot = withoutAttributeAnnot;
}

public String getXmlValue() {
Expand All @@ -67,4 +72,12 @@ public String getTextFieldName() {
public boolean getIsWithNameSpace() {
return withNameSpace;
}

public boolean getWithoutAttributes() {
return withoutAttributes;
}

public boolean getWithoutAttributeAnnot() {
return withoutAttributeAnnot;
}
}
Loading

0 comments on commit ca4a9bc

Please sign in to comment.