Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: permits to specify the placement of overwrites
Related #9908
Related #7648
  • Loading branch information
Patrick8639 committed May 21, 2024
commit f95688d41118bd042b43b2e6e4f2a8dec1e254a7
33 changes: 33 additions & 0 deletions src/Docfx.Common/EntityMergers/MergePlacement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.\r
// The .NET Foundation licenses this file to you under the MIT license.


namespace Docfx.Common.EntityMergers;


/// <summary>
/// The placement for a merge.
/// </summary>
internal enum MergePlacement
{

/// <summary>
/// The placement is not specified.
/// </summary>
None,

/// <summary>
/// The override must be placed after the original content.
/// </summary>
After,

/// <summary>
/// The override must be placed before the original content.
/// </summary>
Before,

/// <summary>
/// The override must replace the original content.
/// </summary>
Replace
}
45 changes: 45 additions & 0 deletions src/Docfx.Common/EntityMergers/ReflectionEntityMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,53 @@ public void Merge(ref object source, object overrides, IMergeContext context)
{
return;
}


// Gets the placement of the override
var placement = MergePlacement.None;

{
if (overrides is IItemWithMetadata ovr
&& ovr.Metadata.TryGetValue("placement", out var placementValue))
{
placement =
placementValue switch
{
"after" => MergePlacement.After,
"before" => MergePlacement.Before,
"replace" => MergePlacement.Replace,
_ => MergePlacement.None
};
}
}


foreach (var prop in Props)
{

// Placement specified in the override file
if (placement != MergePlacement.None
&& prop.Prop.Name is "Remarks" or "Summary")
{
var o = prop.Prop.GetValue(overrides);

if (o is null)
continue;

var s = prop.Prop.GetValue(source);

s = placement switch
{
MergePlacement.After => $"{s}{o}",
MergePlacement.Before => $"{o}{s}",
MergePlacement.Replace => o.ToString()
};

prop.Prop.SetValue(source, s);
}


// Placement specified in the property
switch (prop.Option)
{
case MergeOption.Merge:
Expand Down
20 changes: 20 additions & 0 deletions src/Docfx.Common/IItemWithMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.\r
// The .NET Foundation licenses this file to you under the MIT license.


namespace Docfx.Common;


/// <summary>
/// An item that contains metadate
/// </summary>
public interface IItemWithMetadata
{

/// <summary>
/// Gets the metadata.
/// </summary>
/// <value>The metadata.</value>
Dictionary<string, object> Metadata { get; }

}
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/ManagedReference/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Docfx.DataContracts.ManagedReference;

public class ItemViewModel : IOverwriteDocumentViewModel
public class ItemViewModel : IOverwriteDocumentViewModel, IItemWithMetadata
{
[YamlMember(Alias = Constants.PropertyName.Uid)]
[JsonProperty(Constants.PropertyName.Uid)]
Expand Down