[Feature Request] Default/fallback value when an item is not available yet
See original GitHub issueIs your feature request related to a problem? Please describe.
Currently, when calling GetItemAsync<T>(key)
, a NullReferenceException
is thrown if T
is not nullable.
Describe the solution you’d like There should be an overload for specifying the default value instead of throwing. For example:
GetItemAsync<T>(string key, T fallbackValue);
GetItemAsync<T>(string key, Func<T> fallbackValueFunc);
GetItemAsStringAsync(string key, string fallbackValue);
GetItemAsStringAsync(string key, Func<string> fallbackValue);
Describe alternatives you’ve considered
An alternative is to return null
instead of throwing an exception like JsonSerializer.Deserialize<T>
is doing though honestly that is bad because most of the time, you expect non-null values more.
My current workaround:
await GetItemAsync<T?>(key) ?? new(); // Or something to generate the fallback value
Additional context None
Issue Analytics
- State:
- Created 6 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Allow developers to specify a default/fallback value for a ...
The main caveat I'm seeing is that expressions like ["==", key, value] currently take two string parameters with vastly different interpretation ...
Read more >Return a default value if a dictionary key is not available
I know that I can check for the key, but I am looking for something more explicit. Is there a way to just...
Read more >Enabling Item level language fallback
Field level language fallback is disabled by default, so you must enable it on each relevant website and on the template field definition...
Read more >Enable and set up language fallback
Enable item-level fallback To enable language fallback on a template, navigate to the relevant template and click the _Standard Values item. On the...
Read more >Language fallback
Item -level fallback enables you to set up an 'empty' item version in a given language so that it falls back to another...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe it’s still a breaking change as developers could have warning set as errors. In which case marking the return type as nullable could trigger a NRT warning, or in their case, an error.
This is a great spot. Could you raise this as a separate issue? This would also count as a breaking change so at least we’re batching them up! 😄
Thanks for the clarification, @datvm. I agree, the method signature is wrong and misleading. You’re also correct it would be a breaking change. I think this can be done, but I’d like to roll it up with any other breaking changes for the next major release.