<commit_before>package bot

import lua "github.com/yuin/gopher-lua"

// RegisterConfig will register the Config struct to Lua
func RegisterConfig(state *lua.LState) {
	// Create the Metatable
	meta := state.NewTypeMetatable("config")
	state.SetGlobal("config", meta)

	// Bind our functions
	state.SetField(meta, "__index", state.NewFunction(configIndex))
}

// PushConfig will push the given config to the Lua stack
func PushConfig(config *Config, state *lua.LState) {
	// Create the config user data
	data := state.NewUserData()
	data.Value = config

	// Set the Metatable
	state.SetMetatable(data, state.GetTypeMetatable("config"))

	// Push the config
	state.Push(data)
}

func checkConfig(state *lua.LState) *Config {
	data := state.CheckUserData(1)
	if v, ok := data.Value.(*Config); ok {
		return v
	}
	state.ArgError(1, "config expected")
	return nil
}

func configIndex(state *lua.LState) int {
	config := checkConfig(state)
	key := state.CheckString(2)

	switch key {
	case "Hostname":
		state.Push(lua.LString(config.Hostname))
	case "Port":
		state.Push(lua.LNumber(config.Port))
	case "Secure":
		state.Push(lua.LBool(config.Secure))
	case "InsecureSkipVerify":
		state.Push(lua.LBool(config.InsecureSkipVerify))
	case "Nick":
		state.Push(lua.LString(config.Nick))
	case "User":
		state.Push(lua.LString(config.User))
	case "Name":
		state.Push(lua.LString(config.Name))
	case "Channels":
		channels := state.NewTable()
		for key, value := range config.Channels {
			channels.RawSetInt(key, lua.LString(value))
		}
		state.Push(channels)
	case "Timeout":
		state.Push(lua.LNumber(config.Timeout))
	case "TimeoutLimit":
		state.Push(lua.LNumber(config.TimeoutLimit))
	case "ReconnectLimit":
		state.Push(lua.LNumber(config.ReconnectLimit))<commit_msg>fixed error handling in config bindings<commit_after>package bot

import (
	"reflect"

	log "github.com/Sirupsen/logrus"
	"github.com/jriddick/geoffrey/helper"
	"github.com/yuin/gopher-lua"
)

// RegisterConfig will register the Config struct to Lua
func RegisterConfig(state *lua.LState) {
	// Create the Metatable
	meta := state.NewTypeMetatable("config")
	state.SetGlobal("config", meta)

	// Bind our functions
	state.SetField(meta, "__index", state.NewFunction(configIndex))
}

// PushConfig will push the given config to the Lua stack
func PushConfig(config *Config, state *lua.LState) {
	// Create the config user data
	data := state.NewUserData()
	data.Value = config

	// Set the Metatable
	state.SetMetatable(data, state.GetTypeMetatable("config"))

	// Push the config
	state.Push(data)
}

func checkConfig(state *lua.LState) *Config {
	// Get the logger
	logger := log.WithField("file", state.Where(1))

	// Try to get the userdata
	data := helper.GetUserData(1, state)
	if data != nil {
		if v, ok := data.Value.(*Config); ok {
			return v
		}

		logger.Errorf("Expected userdata type Config but we got '%s'", reflect.TypeOf(data.Value).Name())
	}

	return nil
}

func configIndex(state *lua.LState) int {
	config := checkConfig(state)
	key := helper.GetString(2, state)

	if config == nil || key == nil {
		return 0
	}

	switch *key {
	case "Hostname":
		state.Push(lua.LString(config.Hostname))
	case "Port":
		state.Push(lua.LNumber(config.Port))
	case "Secure":
		state.Push(lua.LBool(config.Secure))
	case "InsecureSkipVerify":
		state.Push(lua.LBool(config.InsecureSkipVerify))
	case "Nick":
		state.Push(lua.LString(config.Nick))
	case "User":
		state.Push(lua.LString(config.User))
	case "Name":
		state.Push(lua.LString(config.Name))
	case "Channels":
		channels := state.NewTable()
		for key, value := range config.Channels {
			channels.RawSetInt(key, lua.LString(value))
		}
		state.Push(channels)
	case "Timeout":
		state.Push(lua.LNumber(config.Timeout))
	case "TimeoutLimit":
		state.Push(lua.LNumber(config.TimeoutLimit))
	case "ReconnectLimit":
		state.Push(lua.LNumber(config.ReconnectLimit))
{'fraction_non_alphanumeric': 0.1412400308721379, 'fraction_numerical': 0.0018008747105737071, 'mean_word_length': 4.594244604316547, 'pattern_counts': {'":': 22, '<': 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': '619786', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1344, 'n_words': 300}