Skip to content

feat: add asdict() and to_json() methods to Gitlab Objects #1872

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
merged 2 commits into from
Jul 20, 2022

Conversation

JohnVillalovos
Copy link
Member

@JohnVillalovos JohnVillalovos commented Feb 2, 2022

Add an asdict() method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The asdict() method name was chosen as both the dataclasses and
attrs libraries have an asdict() function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a to_json() method that returns a JSON string
representation of the object.

Closes: #1116

@JohnVillalovos JohnVillalovos changed the title feat: add a as_dict() method to the Gitlab Objects feat: add an as_dict() method to the Gitlab Objects Feb 2, 2022
@JohnVillalovos JohnVillalovos changed the title feat: add an as_dict() method to the Gitlab Objects feat: add an asdict() method to the Gitlab Objects Feb 2, 2022
@codecov-commenter
Copy link

codecov-commenter commented Feb 2, 2022

Codecov Report

Merging #1872 (08ac071) into main (f6b6e18) will decrease coverage by 0.01%.
The diff coverage is 93.75%.

@@            Coverage Diff             @@
##             main    #1872      +/-   ##
==========================================
- Coverage   95.41%   95.39%   -0.02%     
==========================================
  Files          79       79              
  Lines        5166     5170       +4     
==========================================
+ Hits         4929     4932       +3     
- Misses        237      238       +1     
Flag Coverage Δ
api_func_v4 81.21% <81.25%> (+0.07%) ⬆️
cli_func_v4 82.70% <81.25%> (+0.07%) ⬆️
unit 87.04% <93.75%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
gitlab/base.py 99.49% <93.75%> (-0.51%) ⬇️

@nejch
Copy link
Member

nejch commented Feb 2, 2022

The more I compare this to .attributes, the more I think it's doing something very very similar and the property does have more info available.

What if we add a method like to_json() that reuses our existing property to address #1116? That's another common pattern (see pandas), and we could potentially pass args to dumps for things like indenting etc.

I think this would make the example in the issue quite pretty as well:

with open('data.txt', 'w') as f:
    f.write(src_prj.labels.list(all=True)[0].to_json())

@nejch
Copy link
Member

nejch commented Feb 2, 2022

The more I compare this to .attributes, the more I think it's doing something very very similar and the property does have more info available.

What if we add a method like to_json() that reuses our existing property to address #1116? That's another common pattern (see pandas), and we could potentially pass args to dumps for things like indenting etc.

I think this would make the example in the issue quite pretty as well:

with open('data.txt', 'w') as f:
    f.write(src_prj.labels.list(all=True)[0].to_json())

Going back again, seems like the library supported both json and dict method for v3 with a custom encoder back in 2017. So maybe we could just re-add both:

def json(self):
"""Dump the object as json.
Returns:
str: The json string.
"""
return json.dumps(self, cls=jsonEncoder)
def as_dict(self):
"""Dump the object as a dict."""
return {k: v for k, v in six.iteritems(self.__dict__)
if (not isinstance(v, BaseManager) and not k[0] == '_')}

@JohnVillalovos JohnVillalovos force-pushed the jlvillal/as_dict branch 2 times, most recently from f2ed5e5 to f229f7f Compare February 2, 2022 22:27
@JohnVillalovos
Copy link
Member Author

Going back again, seems like the library supported both json and dict method for v3 with a custom encoder back in 2017. So maybe we could just re-add both:

Sounds good to me. I've got asdict in there. Let me see about to_json

@JohnVillalovos JohnVillalovos marked this pull request as draft February 12, 2022 16:54
@JohnVillalovos JohnVillalovos force-pushed the jlvillal/as_dict branch 3 times, most recently from 4a64a56 to 973e68f Compare July 6, 2022 03:19
@JohnVillalovos JohnVillalovos marked this pull request as ready for review July 6, 2022 03:21
@JohnVillalovos JohnVillalovos changed the title feat: add an asdict() method to the Gitlab Objects feat: add asdict() and to_json() methods to Gitlab Objects Jul 6, 2022
@JohnVillalovos JohnVillalovos requested a review from nejch July 6, 2022 03:22
@JohnVillalovos JohnVillalovos force-pushed the jlvillal/as_dict branch 6 times, most recently from ef5aed2 to ec643b7 Compare July 12, 2022 15:09
@max-wittig
Copy link
Member

Very cool!

Previously the `attributes` method would show the original values in a
Gitlab Object even if they had been updated. Correct this so that the
updated value will be returned.

Also use copy.deepcopy() to ensure that modifying the dictionary returned can
not also modify the object.
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
@nejch nejch merged commit fcbced8 into main Jul 20, 2022
@nejch nejch deleted the jlvillal/as_dict branch July 20, 2022 20:11
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.

ProjectLabel objects are not JSON serializable
4 participants