<commit_before>package cqrs

import (
	"log"
	"reflect"
	"strings"
)

var methodHandlerPrefix = "Handle"

// HandlersCache is a map of types to functions that will be used to route event sourcing events
type HandlersCache map[reflect.Type]func(source interface{}, event interface{})

// TypeCache is a map of strings to reflect.Type structures
type TypeCache map[string]reflect.Type

// TypeRegistry providers a helper registry for mapping event types and handlers after performance json serializaton
type TypeRegistry interface {
	GetHandlers(interface{}) HandlersCache
	GetTypeByName(string) (reflect.Type, bool)
	RegisterAggregate(aggregate interface{}, events ...interface{})
	RegisterEvents(events ...interface{})
	RegisterType(interface{})
}

type defaultTypeRegistry struct {
	HandlersDirectory map[reflect.Type]HandlersCache
	Types             TypeCache
}

var cachedRegistry *defaultTypeRegistry

// NewTypeRegistry constructs a new TypeRegistry
func NewTypeRegistry() TypeRegistry {
	return newTypeRegistry()
}

func newTypeRegistry() *defaultTypeRegistry {
	if cachedRegistry == nil {
		handlersDirectory := make(map[reflect.Type]HandlersCache, 0)
		types := make(TypeCache, 0)

		cachedRegistry = &defaultTypeRegistry{handlersDirectory, types}
	}

	return cachedRegistry
}

func (r *defaultTypeRegistry) GetHandlers(source interface{}) HandlersCache {
	sourceType := reflect.TypeOf(source)
	var handlers HandlersCache
	if value, ok := r.HandlersDirectory[sourceType]; ok {
		handlers = value
	} else {
		handlers = createHandlersCache(source)

		r.HandlersDirectory[sourceType] = handlers
	}

	return handlers
}

func (r *defaultTypeRegistry) GetTypeByName(typeName string) (reflect.Type, bool) {
	if typeValue, ok := r.Types[typeName]; ok {
		return typeValue, ok
	}

	log.Println("Could not find type registration for type  - ", typeName)
	log.Println("The registry looks as follows:")
	for key := range r.Types {
		log.Println(key)
	}<commit_msg>Remove verbose log message<commit_after>package cqrs

import (
	"log"
	"reflect"
	"strings"
)

// DebugTypeRegistry if set to true logs type resolution failures
var DebugTypeRegistry = false
var methodHandlerPrefix = "Handle"

// HandlersCache is a map of types to functions that will be used to route event sourcing events
type HandlersCache map[reflect.Type]func(source interface{}, event interface{})

// TypeCache is a map of strings to reflect.Type structures
type TypeCache map[string]reflect.Type

// TypeRegistry providers a helper registry for mapping event types and handlers after performance json serializaton
type TypeRegistry interface {
	GetHandlers(interface{}) HandlersCache
	GetTypeByName(string) (reflect.Type, bool)
	RegisterAggregate(aggregate interface{}, events ...interface{})
	RegisterEvents(events ...interface{})
	RegisterType(interface{})
}

type defaultTypeRegistry struct {
	HandlersDirectory map[reflect.Type]HandlersCache
	Types             TypeCache
}

var cachedRegistry *defaultTypeRegistry

// NewTypeRegistry constructs a new TypeRegistry
func NewTypeRegistry() TypeRegistry {
	return newTypeRegistry()
}

func newTypeRegistry() *defaultTypeRegistry {
	if cachedRegistry == nil {
		handlersDirectory := make(map[reflect.Type]HandlersCache, 0)
		types := make(TypeCache, 0)

		cachedRegistry = &defaultTypeRegistry{handlersDirectory, types}
	}

	return cachedRegistry
}

func (r *defaultTypeRegistry) GetHandlers(source interface{}) HandlersCache {
	sourceType := reflect.TypeOf(source)
	var handlers HandlersCache
	if value, ok := r.HandlersDirectory[sourceType]; ok {
		handlers = value
	} else {
		handlers = createHandlersCache(source)

		r.HandlersDirectory[sourceType] = handlers
	}

	return handlers
}

func (r *defaultTypeRegistry) GetTypeByName(typeName string) (reflect.Type, bool) {
	if typeValue, ok := r.Types[typeName]; ok {
		return typeValue, ok
	}

	if DebugTypeRegistry {
		log.Println("Could not find type registration for type  - ", typeName)
		log.Println("The registry looks as follows:")
		for key := range r.Types {
			log.Println(key)
		}
	}
{'fraction_non_alphanumeric': 0.09081959143490032, 'fraction_numerical': 0.0009844942160964804, 'mean_word_length': 5.242703533026114, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7510770', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1142, 'n_words': 355}