|
| 1 | +// Copyright 2025 The Kube Resource Orchestrator Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package compat |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | +) |
| 20 | + |
| 21 | +// ChangeType represents the type of schema change |
| 22 | +type ChangeType string |
| 23 | + |
| 24 | +const ( |
| 25 | + // Breaking change types |
| 26 | + PropertyRemoved ChangeType = "PROPERTY_REMOVED" |
| 27 | + TypeChanged ChangeType = "TYPE_CHANGED" |
| 28 | + RequiredAdded ChangeType = "REQUIRED_ADDED" |
| 29 | + EnumRestricted ChangeType = "ENUM_RESTRICTED" |
| 30 | + PatternChanged ChangeType = "PATTERN_CHANGED" |
| 31 | + PatternAdded ChangeType = "PATTERN_ADDED" |
| 32 | + |
| 33 | + // Non-breaking change types |
| 34 | + PropertyAdded ChangeType = "PROPERTY_ADDED" |
| 35 | + DescriptionChanged ChangeType = "DESCRIPTION_CHANGED" |
| 36 | + DefaultChanged ChangeType = "DEFAULT_CHANGED" |
| 37 | + RequiredRemoved ChangeType = "REQUIRED_REMOVED" |
| 38 | + EnumExpanded ChangeType = "ENUM_EXPANDED" |
| 39 | + PatternRemoved ChangeType = "PATTERN_REMOVED" |
| 40 | +) |
| 41 | + |
| 42 | +// Change represents a single schema change |
| 43 | +type Change struct { |
| 44 | + // Path is the JSON path to the changed property |
| 45 | + Path string |
| 46 | + // ChangeType is the type of change |
| 47 | + ChangeType ChangeType |
| 48 | + // OldValue is the string representation of the old value (if applicable) |
| 49 | + OldValue string |
| 50 | + // NewValue is the string representation of the new value (if applicable) |
| 51 | + NewValue string |
| 52 | +} |
| 53 | + |
| 54 | +// Report contains the full analysis of schema differences |
| 55 | +type Report struct { |
| 56 | + // BreakingChanges are changes that break backward compatibility |
| 57 | + BreakingChanges []Change |
| 58 | + // NonBreakingChanges are changes that maintain backward compatibility |
| 59 | + NonBreakingChanges []Change |
| 60 | +} |
| 61 | + |
| 62 | +// IsCompatible returns true if no breaking changes were detected |
| 63 | +func (r *Report) IsCompatible() bool { |
| 64 | + return len(r.BreakingChanges) == 0 |
| 65 | +} |
| 66 | + |
| 67 | +// HasBreakingChanges returns true if breaking changes were detected |
| 68 | +func (r *Report) HasBreakingChanges() bool { |
| 69 | + return len(r.BreakingChanges) > 0 |
| 70 | +} |
| 71 | + |
| 72 | +// HasChanges returns true if any changes were detected |
| 73 | +func (r *Report) HasChanges() bool { |
| 74 | + return len(r.BreakingChanges) > 0 || len(r.NonBreakingChanges) > 0 |
| 75 | +} |
| 76 | + |
| 77 | +const maxBreakingChangesSummary = 3 |
| 78 | + |
| 79 | +// SummarizeBreakingChanges returns a user-friendly summary of breaking changes |
| 80 | +func (r *Report) String() string { |
| 81 | + if !r.HasBreakingChanges() { |
| 82 | + return "no breaking changes" |
| 83 | + } |
| 84 | + |
| 85 | + changeDescs := make([]string, 0, maxBreakingChangesSummary) |
| 86 | + |
| 87 | + for i, change := range r.BreakingChanges { |
| 88 | + // Cut off the summary if there are too many breaking changes |
| 89 | + if i >= maxBreakingChangesSummary { |
| 90 | + remaining := len(r.BreakingChanges) - i |
| 91 | + if remaining > 0 { |
| 92 | + changeDescs = append(changeDescs, fmt.Sprintf("and %d more changes", remaining)) |
| 93 | + } |
| 94 | + break |
| 95 | + } |
| 96 | + changeDescs = append(changeDescs, change.Description()) |
| 97 | + } |
| 98 | + |
| 99 | + return strings.Join(changeDescs, "; ") |
| 100 | +} |
| 101 | + |
| 102 | +// AddBreakingChange adds a breaking change to the result with automatically generated description |
| 103 | +func (r *Report) AddBreakingChange(path string, changeType ChangeType, oldValue, newValue string) { |
| 104 | + r.BreakingChanges = append(r.BreakingChanges, Change{ |
| 105 | + Path: path, |
| 106 | + ChangeType: changeType, |
| 107 | + OldValue: oldValue, |
| 108 | + NewValue: newValue, |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +// AddNonBreakingChange adds a non-breaking change to the result with automatically generated description |
| 113 | +func (r *Report) AddNonBreakingChange(path string, changeType ChangeType, oldValue, newValue string) { |
| 114 | + r.NonBreakingChanges = append(r.NonBreakingChanges, Change{ |
| 115 | + Path: path, |
| 116 | + ChangeType: changeType, |
| 117 | + OldValue: oldValue, |
| 118 | + NewValue: newValue, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +// lastPathComponent extracts the last component from a JSON path |
| 123 | +func lastPathComponent(path string) string { |
| 124 | + parts := strings.Split(path, ".") |
| 125 | + if len(parts) == 0 { |
| 126 | + return path |
| 127 | + } |
| 128 | + return parts[len(parts)-1] |
| 129 | +} |
| 130 | + |
| 131 | +// Description generates a human-readable description based on the change type |
| 132 | +func (c Change) Description() string { |
| 133 | + propName := lastPathComponent(c.Path) |
| 134 | + |
| 135 | + switch c.ChangeType { |
| 136 | + case PropertyRemoved: |
| 137 | + return fmt.Sprintf("Property %s was removed", propName) |
| 138 | + case PropertyAdded: |
| 139 | + if c.NewValue == "required" { |
| 140 | + return fmt.Sprintf("Required property %s was added", propName) |
| 141 | + } |
| 142 | + return fmt.Sprintf("Optional property %s was added", propName) |
| 143 | + case TypeChanged: |
| 144 | + return fmt.Sprintf("Type changed from %s to %s", c.OldValue, c.NewValue) |
| 145 | + case RequiredAdded: |
| 146 | + return fmt.Sprintf("Field %s is newly required", c.NewValue) |
| 147 | + case RequiredRemoved: |
| 148 | + return fmt.Sprintf("Field %s is no longer required", c.OldValue) |
| 149 | + case EnumRestricted: |
| 150 | + return fmt.Sprintf("Enum value %s was removed", c.OldValue) |
| 151 | + case EnumExpanded: |
| 152 | + return fmt.Sprintf("Enum value %s was added", c.NewValue) |
| 153 | + case PatternChanged: |
| 154 | + return fmt.Sprintf("Validation pattern changed from %s to %s", c.OldValue, c.NewValue) |
| 155 | + case PatternAdded: |
| 156 | + return fmt.Sprintf("Validation pattern %s was added", c.NewValue) |
| 157 | + case PatternRemoved: |
| 158 | + return fmt.Sprintf("Validation pattern %s was removed", c.OldValue) |
| 159 | + case DescriptionChanged: |
| 160 | + return "Description field was changed" |
| 161 | + case DefaultChanged: |
| 162 | + return "Default value was changed" |
| 163 | + default: |
| 164 | + return fmt.Sprintf("Unknown change to %s", c.Path) |
| 165 | + } |
| 166 | +} |
0 commit comments