Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp native code #291

Merged
merged 18 commits into from
Nov 4, 2021
Merged

Revamp native code #291

merged 18 commits into from
Nov 4, 2021

Conversation

kalaiyarasiganeshalingam
Copy link
Contributor

@kalaiyarasiganeshalingam kalaiyarasiganeshalingam commented Oct 22, 2021

Purpose

The toJson() has some gaps with spec. When analyzed the spec, the following gaps were identified:

  • According to the spec, If XML has identical keys, it has to create a JSON array with an identical key. But existing implementation doesn't support XML that has multiple identical keys.
xml x1 = xml `<books>
                      <item>book1</item>
                      <item>book2</item>
                      <item>book3</item>
                      <item1>book1</item1>
                      <item1>book2</item1>
                      <item1>book3</item1>
                  </books>`;

Existing output:

{"books":{"item":"book3","item1":"book3"}}

Expected output:

{
	"books":{
		"item":[
			"book1",
			"book2",
			"book3"
		],
		"item1":[
			"book1",
			"book2",
			"book3"
		]
	}
}
  • If an XML element has an attribute and value, that has to create this output. But existing implementation doesn't create that format.
    Existing output:
{"foo":"5","@key":"value"}

So, this PR contains all these fixes.

Fixes: ballerina-platform/ballerina-library#2126

Examples

Checklist

  • Linked to an issue
  • Updated the changelog
  • Added tests

@codecov
Copy link

codecov bot commented Oct 22, 2021

Codecov Report

Merging #291 (d8cdd2f) into master (fe152b5) will increase coverage by 0.85%.
The diff coverage is 93.02%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #291      +/-   ##
============================================
+ Coverage     87.20%   88.05%   +0.85%     
+ Complexity       89       83       -6     
============================================
  Files             7        7              
  Lines           297      268      -29     
  Branches         64       56       -8     
============================================
- Hits            259      236      -23     
+ Misses           22       18       -4     
+ Partials         16       14       -2     
Impacted Files Coverage Δ
...in/java/io/ballerina/stdlib/xmldata/XmlToJson.java 90.06% <92.30%> (+1.29%) ⬆️
.../io/ballerina/stdlib/xmldata/AttributeManager.java 100.00% <100.00%> (ø)
ballerina/xmldata.bal 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe152b5...d8cdd2f. Read the comment docs.

@niveathika niveathika marked this pull request as draft October 22, 2021 13:30
@niveathika
Copy link
Contributor

Marked as draft since the proposal acceptance is still under review

@kalaiyarasiganeshalingam kalaiyarasiganeshalingam marked this pull request as ready for review October 26, 2021 04:25
List<BXml> newSequence = new ArrayList<BXml>();
for (BXml value: sequence) {
String textValue = value.getTextValue();
if (textValue.isEmpty() || !textValue.trim().isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we checking here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When initialising xml with line break between elements, couldn't get the proper BXml list by using xmlSequence.getChildrenList(). This list contain line breaks as element. So I used this to eliminate line break. Now I have created issue for this. Please find the issue for this: ballerina-platform/ballerina-lang#33497

Comment on lines 64 to 65
private static LinkedHashMap<String, String> attributeMap = new LinkedHashMap<>();
private static LinkedHashMap<String, String> tempAttributeMap = new LinkedHashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static variables ? This is not concurrent safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used in static method. Hence, I am using static variable. Now, I have changed LinkedHashMap to ConcurrentHashMap in 562a4dd.

Copy link

@hasithaa hasithaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is not concurrent safe.

groups: ["toJson"]
}
isolated function testComplexXmlWithNamespace() returns error? {
xml x1 = xml `<ns0:bookStore status="online" xmlns:ns0="http://sample.com/test">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this is not a valid xml (multiple possible root nodes) and therefore should return an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the spec. So, I haven't checked whether this valid or not. Yes, This is not valid XML. We don't need to validate in the xmldata module. it should be validated in the lang lib. Did you create an issue for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
public class AttributeManager {

private ConcurrentHashMap<String, String> concurrentHashMap;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use LinkedHashMap here, since AttributeManager is concurrent safe now. Also we need to preserve the order, so replace other usages with LinkedHashMap as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3481ab2

hasithaa
hasithaa previously approved these changes Nov 1, 2021
Copy link

@hasithaa hasithaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall design looks ok.

"{\"StreetAddress\":\"20, Palm grove, Colombo 3\"}, \"\\n \", " +
"{\"City\":\"Colombo\"}, \"\\n \", {\"Zip\":\"00300\"}, \"\\n \", " +
"{\"Country\":\"LK\"}, \"\\n \"], \"@xmlns\":\"\"}, \"\\n \"], " +
test:assertEquals(convertToJson(e.toString()),"{\"Invoice\":{\"PurchesedItems\":{\"PLine\":[" +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IINM convertToJson is a test util function. Why are there unit tests for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had these as these test utils functions are keeping long times. But It seems these are not useful because the toJson() directly calls the convertToJSON(). We can test this by calling ballerina API. So, we don't need to test the convertToJSON() by using these test utils. So I have removed this util module in 873cc8c.

Revert "Remove testutils module"

This reverts commit 2c0e2a0.

Revert "Revert "Remove testutils module""

This reverts commit 74a7409.
niveathika
niveathika previously approved these changes Nov 3, 2021
@kalaiyarasiganeshalingam kalaiyarasiganeshalingam merged commit 3a35a9a into master Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update the native code to fix the gaps between implementation and spec
4 participants