ngocnicholas / airtable.net

Airtable .NET API Client
MIT License
138 stars 34 forks source link

ENHANCEMENT: Parsing fields manually should have generic helpers #65

Closed marchy closed 1 year ago

marchy commented 1 year ago

After the move to System.Text.Json, parsing out fields manually has become really cumbersome due to an extra cast to JsonElement needed, which has to be wrapped in braces (due to the forced casting syntax) and then the new .GetString() / GetBoolean() etc. methods used to parse the appropriate values.

This also leaks the System.Text.Json abstraction outwards through the Airtable.NET library, which is further out than it ought to be.

Example:

string? name = ((JsonElement?)GetField( "Name" ))?.GetString()

Instead I propose generic GetField<T> methods added that hide away all the casting complexity – and indeed hide away the System.Text.Json abstraction altogether.

Suggested:

string? name = regionRecord.GetField<string?>( "Name" )
marchy commented 1 year ago

I've submitted up a PR (https://github.com/ngocnicholas/airtable.net/pull/66) that implements helpers for both primitive fields as well as arrays, lists, collections and enumerables, while also exposing an GetFieldAsJson(..) if you do want a convenience to access the System.Text.Json JsonElement directly (as opposed to getting an object back that you have to cast each time).

marchy commented 1 year ago

Has this been pushed out in a new version yet by chance?

ngocnicholas commented 1 year ago

A new version of Airtable.net will be released next week.

ngocnicholas commented 1 year ago

Enhancements included in new release of 1.3.0.