Skip to content

Add TrySetValue and TryGetValue#322

Merged
Turnerj merged 2 commits intoRehanSaeed:mainfrom
Turnerj:dynamic-getter-setter
Aug 24, 2021
Merged

Add TrySetValue and TryGetValue#322
Turnerj merged 2 commits intoRehanSaeed:mainfrom
Turnerj:dynamic-getter-setter

Conversation

@Turnerj
Copy link
Copy Markdown
Collaborator

@Turnerj Turnerj commented Aug 21, 2021

Introduces two new methods TrySetValue and TryGetValue which allow dynamic access to get and set values on a schema object. Both methods are generated via our source generator rather than doing something weird with reflection.

While TrySetValue is straight forward, TryGetValue requires a generic parameter to identify which piece of data you want. This is required to have a typed-value be the result of the call. If you specify a type that is invalid for the property, the method fails gracefully.

The methods are also case insensitive.

Open to any feedback or suggestions you have @RehanSaeed 🙂

@Turnerj Turnerj added enhancement Issues describing an enhancement or pull requests adding an enhancement. minor Pull requests requiring a minor version update according to semantic versioning. labels Aug 21, 2021
@Turnerj Turnerj requested a review from RehanSaeed August 21, 2021 16:23
@Turnerj
Copy link
Copy Markdown
Collaborator Author

Turnerj commented Aug 22, 2021

There is probably another method I could add too: TryGetValue(string property, out IValues result)
This being non-generic allows simple access to any type of value on the property.

Actually, I'm just going to add that now to this PR.

Copy link
Copy Markdown
Owner

@RehanSaeed RehanSaeed left a comment

Choose a reason for hiding this comment

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

Seems reasonable. Is there a specific reason you need this feature? I can't think of any obvious use cases.

@Turnerj
Copy link
Copy Markdown
Collaborator Author

Turnerj commented Aug 23, 2021

Seems reasonable. Is there a specific reason you need this feature? I can't think of any obvious use cases.

The primary motivation for me is that it makes it easier to create Schema objects at runtime - especially when the properties are provided from unreliable 3rd party data (for example, parsing Microdata from HTML into Schema objects).

Besides these methods, there are a couple of other changes I'd like to make for other methods of reading/writing/converting schema objects easier too. For example, moving some of the string-conversion logic in ValuesJsonConverter to a separate class that can be access directly (again, useful for things like converting Microdata).

@Turnerj
Copy link
Copy Markdown
Collaborator Author

Turnerj commented Aug 23, 2021

... there are a couple of other changes I'd like to make for other methods ...

I should clarify with this - I was originally going to include some of that in this PR but some of the changes would overlap with the changes for System.Text.Json support and would make merging a bit of a pain.

@Turnerj Turnerj merged commit 6640d76 into RehanSaeed:main Aug 24, 2021
@Turnerj Turnerj deleted the dynamic-getter-setter branch August 24, 2021 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Issues describing an enhancement or pull requests adding an enhancement. minor Pull requests requiring a minor version update according to semantic versioning.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants