<commit_before>package reflector

import (
	"fmt"
	"reflect"
	"time"
)

type (
	CompareResult struct {
		FieldName string
		OldValue  interface{}
		NewValue  interface{}
		IsSlice   bool
	}
)

func MakeMapFromDifferences(differences []*CompareResult) (map[string]interface{}, map[string]interface{}) {
	updatesMap, changesMap := make(map[string]interface{}), make(map[string]interface{})
	for _, difference := range differences {
		if !difference.IsSlice {
			changesMap["From"+difference.FieldName] = difference.OldValue
			changesMap["To"+difference.FieldName] = difference.NewValue
			updatesMap[difference.FieldName] = difference.NewValue
		}
	}
	return updatesMap, changesMap
}

func Compare(oldStruct interface{}, newStruct interface{}, onlyFields []string) ([]*CompareResult, error) {
	if oldStruct == nil || newStruct == nil {
		return nil, fmt.Errorf("Comparator : One of the inputs cannot be nil. struct1: %v, struct2 : %v ", oldStruct, newStruct)
	}

	//Get values of the structs
	oldValue, newValue := reflect.ValueOf(oldStruct), reflect.ValueOf(newStruct)

	//Handle pointers, if a non-pointer struct is passed in, Indirect will just return the element
	oldValue, newValue = reflect.Indirect(oldValue), reflect.Indirect(newValue)
	if !oldValue.IsValid() || !newValue.IsValid() {
		return nil, fmt.Errorf("Comparator : Types cannot be nil. SRC (%v) invalid = %v - DEST (%v) invalid %v", oldStruct, oldValue.IsValid(), newStruct, newValue.IsValid())
	}

	//Cache v1 struct type
	oldType := oldValue.Type()
	newType := newValue.Type()
	//Verify both v1 and v2 are the same type
	if oldType != newType {
		return nil, fmt.Errorf("Comparator : Structs must be the same type. Struct1 %v - Stuct2 -%v", oldType, newType)
	}

	//Verify v1 is a struct, if v1 is a struct then v2 is also a struct because we have already verified the types are equal
	if oldValue.Kind() != reflect.Struct || newValue.Kind() != reflect.Struct {
		return nil, fmt.Errorf("Comparator : Types must both be structs.  Kind1: %v, Kind2 :v", oldValue.Kind(), newValue.Kind())
	}

	//Initialize differences to ensure length of 0 on return
	differences := make([]*CompareResult, 0)

	for i, numFields := 0, oldValue.NumField(); i < numFields; i++ {

		//Get values of the structure's fields
		oldField, newField := oldValue.Field(i), newValue.Field(i)

		//Get a reference to the field type
		fieldType := oldType.Field(i)
		fieldKind := fieldType.Type.Kind()
		currentFieldName := fieldType.Name

		if fieldType.PkgPath != "" {
			//If the field name is unexported, skip
			continue
		}

		//Handle nil pointers, if a non-pointer field is passed in, Indirect will just return the element
		oldField, newField = reflect.Indirect(oldField), reflect.Indirect(newField)
<commit_msg>Fixed missing % in comparator<commit_after>package reflector

import (
	"fmt"
	"reflect"
	"time"
)

type (
	CompareResult struct {
		FieldName string
		OldValue  interface{}
		NewValue  interface{}
		IsSlice   bool
	}
)

func MakeMapFromDifferences(differences []*CompareResult) (map[string]interface{}, map[string]interface{}) {
	updatesMap, changesMap := make(map[string]interface{}), make(map[string]interface{})
	for _, difference := range differences {
		if !difference.IsSlice {
			changesMap["From"+difference.FieldName] = difference.OldValue
			changesMap["To"+difference.FieldName] = difference.NewValue
			updatesMap[difference.FieldName] = difference.NewValue
		}
	}
	return updatesMap, changesMap
}

func Compare(oldStruct interface{}, newStruct interface{}, onlyFields []string) ([]*CompareResult, error) {
	if oldStruct == nil || newStruct == nil {
		return nil, fmt.Errorf("Comparator : One of the inputs cannot be nil. struct1: %v, struct2 : %v ", oldStruct, newStruct)
	}

	//Get values of the structs
	oldValue, newValue := reflect.ValueOf(oldStruct), reflect.ValueOf(newStruct)

	//Handle pointers, if a non-pointer struct is passed in, Indirect will just return the element
	oldValue, newValue = reflect.Indirect(oldValue), reflect.Indirect(newValue)
	if !oldValue.IsValid() || !newValue.IsValid() {
		return nil, fmt.Errorf("Comparator : Types cannot be nil. SRC (%v) invalid = %v - DEST (%v) invalid %v", oldStruct, oldValue.IsValid(), newStruct, newValue.IsValid())
	}

	//Cache v1 struct type
	oldType := oldValue.Type()
	newType := newValue.Type()
	//Verify both v1 and v2 are the same type
	if oldType != newType {
		return nil, fmt.Errorf("Comparator : Structs must be the same type. Struct1 %v - Stuct2 -%v", oldType, newType)
	}

	//Verify v1 is a struct, if v1 is a struct then v2 is also a struct because we have already verified the types are equal
	if oldValue.Kind() != reflect.Struct || newValue.Kind() != reflect.Struct {
		return nil, fmt.Errorf("Comparator : Types must both be structs.  Kind1: %v, Kind2 :%v", oldValue.Kind(), newValue.Kind())
	}

	//Initialize differences to ensure length of 0 on return
	differences := make([]*CompareResult, 0)

	for i, numFields := 0, oldValue.NumField(); i < numFields; i++ {

		//Get values of the structure's fields
		oldField, newField := oldValue.Field(i), newValue.Field(i)

		//Get a reference to the field type
		fieldType := oldType.Field(i)
		fieldKind := fieldType.Type.Kind()
		currentFieldName := fieldType.Name

		if fieldType.PkgPath != "" {
			//If the field name is unexported, skip
			continue
		}

		//Handle nil pointers, if a non-pointer field is passed in, Indirect will just return the element
		oldField, newField = reflect.Indirect(oldField), reflect.Indirect(newField)

{'fraction_non_alphanumeric': 0.12230866654604668, 'fraction_numerical': 0.005427899402931066, 'mean_word_length': 4.467853610286845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '51831', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1699, 'n_words': 553}