-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Unpublishing and editing content item with an uninitialized ContentPickerField breaks (Lombiq Technologies: OCORE-92) #11528
Changes from 3 commits
f705711
076f341
b3e9b65
ab6132c
9ec0d2a
1845039
d343e9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace OrchardCore.ContentManagement.Metadata.Models; | ||
|
||
public static class ContentPartFieldDefinitionExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the value of the defined content field from the <paramref name="contentItem"/>. | ||
/// </summary> | ||
public static TField GetContentField<TField>( | ||
this ContentPartFieldDefinition fieldDefinition, | ||
ContentItem contentItem) | ||
where TField : ContentField => | ||
contentItem.Content[fieldDefinition.PartDefinition.Name] is JObject jPart && | ||
jPart[fieldDefinition.Name] is JObject jField && | ||
jField.ToObject<TField>() is { } field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a combined operation for pattern matching introduced in C# 8. It does If I replaced this with jField.ToObject<TField>() != null
? jField.ToObject<TField>()
: null; I think that's less efficient and visually noisier. Please comment if you want C# 8 style, C# 7 style, or the old fashioned null check and explicit assignment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Like pain, after a while you don't realize it's there, so pain isn't real actually. Please keep the old style, and add more lines if necessary for readability or prevent two calls to ToObject. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not like pain, more like learning a new technique. First it's weird and awkward, but it you give it a chance you start seeing the utility in practice and it grows on you. Change is good. But it's you guys' repo, so I abide by your decision and have rewrote the method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I have already eliminated the null check here, but I applied your suggestion to the null check in the other method in this file. However this is starting to get a bit confusing. Does the OC repo have a style guide? I couldn't find it. If not, do you have plans to make one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That links to ASP.Net Core's engineering guidelines. If you can edit that, it would be good to add something about null checking conventions based on this discussion. If not, an OC-specific guide would still be valuable to keep a record of such decisions. Just a thought. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to add documentation about this. We are just giving comments here based on our coding preferences because it is easier to read. Basically, adding documentation will not prevent this from happening so we give our advice on every pull request. Getting code merged into source code can take more time than we think at first. You will get used to it over time. Everyone has comments and suggestions, that is why pull requests exist. |
||
? field | ||
: null; | ||
|
||
/// <summary> | ||
/// Returns each field from <paramref name="fieldDefinitions"/> that exists in <paramref name="contentItem"/> in a | ||
/// tuple along with its <see cref="ContentPartFieldDefinition"/>. | ||
/// </summary> | ||
public static IEnumerable<(ContentPartFieldDefinition Definition, TField Field)> GetContentFields<TField>( | ||
this IEnumerable<ContentPartFieldDefinition> fieldDefinitions, | ||
ContentItem contentItem) | ||
where TField : ContentField | ||
{ | ||
foreach (var fieldDefinition in fieldDefinitions) | ||
{ | ||
if (fieldDefinition.GetContentField<TField>(contentItem) is not { } field) continue; | ||
yield return (fieldDefinition, field); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some thought here. Could we do the same but without using Newtonsoft.Json and replace it with System.Text.Json while we are at it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ContentItem.Content
points toOrchardCore.ContentManagement.ContentElement.Data
which is a NewtonsoftJObject
. This class doesn't do any serializing, just access this existing property so trying to use System.Text.Json here would require conversion. Better table it untilContentElement
itself is ported to STJ, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, forget it for now then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will Linq be concern here for allocation and performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We pass on the enumerables without using an intermediary list like before, so that should improve allocation. I'm not sure about the performance, but we don't do anything complex so it should be fine. Also these providers are only called when you update a content item's field; that's relatively rare so you shouldn't be concerned.