MCPcopy
hub / github.com/homeport/dyff / CompareInputFiles

Function CompareInputFiles

pkg/dyff/core.go:111–190  ·  view source on GitHub ↗

CompareInputFiles is one of the convenience main entry points for comparing objects. In this case the representation of an input file, which might contain multiple documents. It returns a report with the list of differences.

(from ytbx.InputFile, to ytbx.InputFile, compareOptions ...CompareOption)

Source from the content-addressed store, hash-verified

109// objects. In this case the representation of an input file, which might
110// contain multiple documents. It returns a report with the list of differences.
111func CompareInputFiles(from ytbx.InputFile, to ytbx.InputFile, compareOptions ...CompareOption) (Report, error) {
112 // initialize the comparator with the tool defaults
113 cmpr := compare{
114 settings: compareSettings{
115 NonStandardIdentifierGuessCountThreshold: 3,
116 IgnoreOrderChanges: false,
117 KubernetesEntityDetection: true,
118 },
119 }
120
121 // apply the optional compare options provided to this function call
122 for _, compareOption := range compareOptions {
123 compareOption(&cmpr.settings)
124 }
125
126 // in case Kubernetes mode is enabled, try to compare documents in the YAML
127 // file by their names rather than just by the order of the documents
128 if cmpr.settings.KubernetesEntityDetection {
129 var fromDocs, toDocs []*yamlv3.Node
130 var fromNames, toNames []string
131
132 for i := range from.Documents {
133 if entry := from.Documents[i]; !isEmptyDocument(entry) {
134 fromDocs = append(fromDocs, entry)
135 if name, err := k8sItem.Name(entry.Content[0]); err == nil {
136 fromNames = append(fromNames, name)
137 }
138 }
139 }
140
141 for i := range to.Documents {
142 if entry := to.Documents[i]; !isEmptyDocument(entry) {
143 toDocs = append(toDocs, entry)
144 if name, err := k8sItem.Name(entry.Content[0]); err == nil {
145 toNames = append(toNames, name)
146 }
147 }
148 }
149
150 // when the look-up of a name for each document in each file worked out, it
151 // means that the documents are most likely Kubernetes resources, so a comparison
152 // using the names can be done, otherwise, leave and continue with default behavior
153 if len(fromNames) == len(fromDocs) && len(toNames) == len(toDocs) {
154 // Reset the docs and names based on the collected details
155 from.Documents, from.Names = fromDocs, fromNames
156 to.Documents, to.Names = toDocs, toNames
157
158 // Compare the document nodes
159 result, err := cmpr.documentNodes(from, to)
160 if err != nil {
161 return Report{}, fmt.Errorf("comparing Kubernetes resources: %w", err)
162 }
163 return Report{from, to, result}, nil
164 }
165 }
166
167 if len(from.Documents) != len(to.Documents) {
168 return Report{}, fmt.Errorf("comparing YAMLs with a different number of documents is currently not supported")

Callers 6

compare_test.goFile · 0.92
compareFunction · 0.92
between.goFile · 0.92
lastApplied.goFile · 0.92

Calls 4

documentNodesMethod · 0.95
objectsMethod · 0.95
isEmptyDocumentFunction · 0.85
NameMethod · 0.65

Tested by 3

compareFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…