func compareDocumentFields()

in pkg/approvaltest/sort.go [78:107]


func compareDocumentFields(i, j json.RawMessage) int {
	// NOTE(axw) we should remove this event type derivation and comparison
	// in the future, and sort purely on fields. We're doing this to avoid
	// reordering all the approval files while removing `processor.event`.
	// If/when we change sort order, we should add a tool for re-sorting
	// *.approved.json files.
	if n := getEventType(i) - getEventType(j); n != 0 {
		return int(n)
	}
	for _, field := range docSortFields {
		path := strings.ReplaceAll(field, ".", "\\.")
		ri := gjson.GetBytes(i, path)
		rj := gjson.GetBytes(j, path)
		if ri.Exists() && rj.Exists() {
			// 'fields' always returns an array
			// of values, but all of the fields
			// we sort on are single value fields.
			ri = ri.Array()[0]
			rj = rj.Array()[0]
		}
		if ri.Less(rj, true) {
			return -1
		}
		if rj.Less(ri, true) {
			return 1
		}
	}
	// All sort fields are equivalent, so compare bytes.
	return bytes.Compare(i, j)
}