| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using System.ComponentModel.DataAnnotations; |
| | | 3 | | |
| | | 4 | | namespace DotNetApiDiff.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a change detected between two API members |
| | | 8 | | /// </summary> |
| | | 9 | | public class ApiChange |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Type of change (Added, Removed, Modified, Excluded) |
| | | 13 | | /// </summary> |
| | 154 | 14 | | public ChangeType Type { get; set; } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The source member (from the original assembly) |
| | | 18 | | /// </summary> |
| | 42 | 19 | | public ApiMember? SourceMember { get; set; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The target member (from the new assembly) |
| | | 23 | | /// </summary> |
| | 49 | 24 | | public ApiMember? TargetMember { get; set; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Whether this change is considered breaking |
| | | 28 | | /// </summary> |
| | 56 | 29 | | public bool IsBreakingChange { get; set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Human-readable description of the change |
| | | 33 | | /// </summary> |
| | | 34 | | [Required] |
| | 135 | 35 | | public string Description { get; set; } = string.Empty; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Additional details about the change |
| | | 39 | | /// </summary> |
| | 74 | 40 | | public List<string> Details { get; set; } = new List<string>(); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Validates the ApiChange instance |
| | | 44 | | /// </summary> |
| | | 45 | | /// <returns>True if valid, false otherwise</returns> |
| | | 46 | | public bool IsValid() |
| | 21 | 47 | | { |
| | | 48 | | // Must have a description |
| | 21 | 49 | | if (string.IsNullOrWhiteSpace(Description)) |
| | 2 | 50 | | { |
| | 2 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Must have at least one member for most change types |
| | 19 | 55 | | if (Type != ChangeType.Added && Type != ChangeType.Removed && |
| | 19 | 56 | | SourceMember == null && TargetMember == null) |
| | 0 | 57 | | { |
| | 0 | 58 | | return false; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | // Added changes should have target member |
| | 19 | 62 | | if (Type == ChangeType.Added && TargetMember == null) |
| | 4 | 63 | | { |
| | 4 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Removed changes should have source member |
| | 15 | 68 | | if (Type == ChangeType.Removed && SourceMember == null) |
| | 2 | 69 | | { |
| | 2 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Modified changes should have both members |
| | 13 | 74 | | if (Type == ChangeType.Modified && (SourceMember == null || TargetMember == null)) |
| | 2 | 75 | | { |
| | 2 | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | 11 | 79 | | return true; |
| | 21 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the primary member name for this change |
| | | 84 | | /// </summary> |
| | | 85 | | public string GetMemberName() |
| | 5 | 86 | | { |
| | 5 | 87 | | return TargetMember?.FullName ?? SourceMember?.FullName ?? "Unknown"; |
| | 5 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets a string representation of the change |
| | | 92 | | /// </summary> |
| | | 93 | | public override string ToString() |
| | 2 | 94 | | { |
| | 2 | 95 | | var memberName = GetMemberName(); |
| | 2 | 96 | | var breakingIndicator = IsBreakingChange ? " [BREAKING]" : string.Empty; |
| | 2 | 97 | | return $"{Type}: {memberName}{breakingIndicator}"; |
| | 2 | 98 | | } |
| | | 99 | | } |