<commit_before>package hooks

import "github.com/gernest/ngorm/engine"

//Hook is a function that is executed at a particular point in time. This allows
//doing additional transformation of any portion of the engine.Engine, and it
//can be a way to overide default behaviour.
type Hook func(e *engine.Engine) error<commit_msg>[hooks] Start rough ideas on how hooks will look like<commit_after>package hooks

import (
	"sync"

	"github.com/gernest/ngorm/engine"
)

//Hook is a nterface that is executed at a particular point in time. This allows
//doing additional transformation of any portion of the engine.Engine, and it
//can be a way to overide default behaviour.
type Hook interface {
	Name() string
	Exec(e *engine.Engine) error
}

type Hooks struct {
	h  map[string]Hook
	mu sync.RWMutex
}

func (h *Hooks) Set(hk Hook) {
	h.mu.Lock()
	h.h[hk.Name()] = hk
	h.mu.Unlock()
}

func (h *Hooks) Get(name string) (Hook, bool) {
	h.mu.RLock()
	hk, ok := h.h[name]
	h.mu.RUnlock()
	return hk, ok
}

func NewHooks() *Hooks {
	return &Hooks{h: make(map[string]Hook)}
}

type simpleHook struct {
	name string
	e    func(*engine.Engine) error
}

func (s *simpleHook) Name() string {
	return s.name
}

func (s *simpleHook) Exec(e *engine.Engine) error {
	return s.e(e)
}

func HookFunc(name string, f func(*engine.Engine) error) Hook {
	return &simpleHook{name: name, e: f}
}
{'fraction_non_alphanumeric': 0.1183345507669832, 'fraction_numerical': 0.0, 'mean_word_length': 4.018315018315018, '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': '7340636', 'n_tokens_mistral': 486, 'n_tokens_neox': 479, 'n_words': 170}