Skip to content

Change underlying data structure of resultSet #38

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

Merged

Conversation

thanhnguyen2187
Copy link
Contributor

From map[string]interface{} to OrderedDict.

Should serve as a prototype to proofrock/sqliterg#5.

From `map[string]interface{}` to `OrderedDict`.
@thanhnguyen2187 thanhnguyen2187 marked this pull request as draft February 11, 2024 08:24
@proofrock
Copy link
Owner

proofrock commented Feb 11, 2024

Thanks, and I mean it!!

While this should not be a problem to merge (no regressions, an ordered map is an unordered map, so to speak), I still feel this is not the right way, because in json a map is unordered. I'd rather implement the solution proposed in the issue discussion, in due time.

Anyway, I'll merge this, allow me some time to review.

Thanks again!

@thanhnguyen2187 thanhnguyen2187 marked this pull request as ready for review February 11, 2024 10:18
@thanhnguyen2187
Copy link
Contributor Author

thanhnguyen2187 commented Feb 11, 2024

Suggestion: you might want to look at this library (https://github.com/stretchr/testify) for assertion, and test suite setting up plus tearing down. I think depending on the way Go collects tests for setting up and tearing down can increase the complexity of the code and debugging a bit 😅.

@proofrock
Copy link
Owner

proofrock commented Feb 12, 2024

I think it's solid, and thanks. What is the reason for getDefault[...]()? What makes it necessary?

@proofrock proofrock self-assigned this Feb 12, 2024
@proofrock proofrock added the enhancement New feature or request label Feb 12, 2024
@proofrock
Copy link
Owner

Tests pass. Thanks, and merging now.

@proofrock proofrock merged commit 9c86937 into proofrock:main Feb 12, 2024
@proofrock
Copy link
Owner

proofrock commented Feb 12, 2024

Please notice that after this, I cannot implement the solution for proofrock/sqliterg#5 anymore, because getDefault "wants" a map. Is getDefault really necessary?

EDIT: I can work around it, sorry

@thanhnguyen2187
Copy link
Contributor Author

OrderedDict.Get() returns the value if found and ok, which means in a lot of places, I would have to create many temporary variables whenever I invoke .Get. I use getDefault to reduce the boilerplate in this case. The underlying logic should not matter that much, I think.

@proofrock
Copy link
Owner

proofrock commented Feb 12, 2024

I see and 👍. What I plan to do is to use a generic interface{} to replace with a map or a list as needed; but of course I cannot pass it to getDefault. I need to cast it explicitly in the relevant tests, but I would have to do it anyway.

@proofrock
Copy link
Owner

@thanhnguyen2187

This works fine, but in practice it introduces an inconsistency. The json that it produces is ordered, but when it parses the same json it attempts to parse it unordered.

This is apparent if you substitute []interface{} to []orderedmap.OrderedMap, as type for ResultSet; problem is, this is the way I need to implement the "resultset as a list" feature. Depending on the input parameter, I need to populate ResultSet with a map or a list.

But then all tests fail, because when it parses the json it parses a map[string]interface{}...

What if we maintain the unordered map (as I said, json is unordered) and add a list of column headers as an additional response field? You could read the order from this list and get them from the map. And I would do also the resultset-as-list.

This should cover your use case, and it would be more... coherent.

@proofrock
Copy link
Owner

proofrock commented Feb 12, 2024

Alternate: since it's not strictly necessary in the tests to have an orderedmap, let it deserialize to map[string]interface{} and be done with it, putting a very big comment documenting the inconsistency.

To tell the truth, I don't like it too much. But I'll do this for now.

@proofrock proofrock added this to the v0.16.0 milestone Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants