<commit_before>package config

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"reflect"
)

var (
	cfg         = make(map[string]interface{})
	KeyNotExist = errors.New("key not exist")
)

// read config file
//
func ReadCfg(fn string) {
	if fn == "" {
		fn = "./config.json"
	}
	content, err := ioutil.ReadFile(fn)
	if err != nil {
		panic(err.Error())
	}
	err = json.Unmarshal(content, &cfg)
	if err != nil {
		panic(err.Error())
	}
}

// Get
// if the key do not exist, return nil
func Get(key string) interface{} {
	if v, ok := cfg[key]; ok {
		return v
	}

	return nil
}

// GetMust
// if the key do not exist, panic
func GetMust(key string) interface{} {
	v, ok := cfg[key]
	if !ok {
		panic("Not found " + key + " in config.")
	}

	return v
}

// GetString
// Get string type by key
func GetString(key string) string {
	v := Get(key)
	if v == nil {
		return ""
	}
	if s, ok := v.(string); ok {
		return s
	}

	return ""
}

// GetString
// Get string type by key, if not exist, panic
func GetStringMust(key string) string {
	v := Get(key)
	if v == nil {
		panic("Not found key " + key)
		return ""
	}
	if s, ok := v.(string); ok {
		return s
	}

	panic("Cannot convert key " + key + " to string.")
	return ""
}

// GetString
// Get string type by key, if key not exist, return the default
func GetStringDefault(key, def string) string {
	s := GetString(key)
	if s == "" {
		return def
	}
	return s
}

// GetInt
func GetInt(key string) (int64, bool) {
	v := Get(key)
	if v == nil {
		return 0, false
	}
	if i, ok := v.(float64); ok {
		return int64(i), true
	}

	return 0, false
}

func GetInt64Default(key string, dv int64) int64 {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(float64); ok {
		return int64(i)
	}

	return dv
}

func GetIntDefault(key string, dv int) int {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(float64); ok {
		return int(i)
	}

	return dv
}

func GetFloat(key string) (float64, bool) {
	v := Get(key)
	if v == nil {
		return 0, false
	}
	if i, ok := v.(float64); ok {
		return i, true
	}

	return 0.0, false
}

func GetBoolean(key string) (bool, bool) {
	v := Get(key)
	if v == nil {
		return false, false
	}

	if i, ok := v.(bool); ok {
		return i, true
	}

	return false, false
}

func GetBooleanDefault(key string, dv bool) bool {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(bool); ok {
		return i
	}

	return dv
}

// Scan
//
func Scan(key string, dest interface{}) (err error) {
	val, ok := cfg[key]
	if !ok {
		return KeyNotExist
	}

	rv := reflect.ValueOf(dest)
	if rv.Kind() != reflect.Ptr || rv.IsNil() {
		return fmt.Errorf("param must be pointer and not nil")
	}

	return scan(val, rv)
}<commit_msg>add ScanConf<commit_after>package config

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"reflect"
)

type Config map[string]interface{}

var (
	cfg         = make(map[string]interface{})
	KeyNotExist = errors.New("key not exist")
)

// read config file
//
func ReadCfg(fn string) {
	if fn == "" {
		fn = "./config.json"
	}
	content, err := ioutil.ReadFile(fn)
	if err != nil {
		panic(err.Error())
	}
	err = json.Unmarshal(content, &cfg)
	if err != nil {
		panic(err.Error())
	}
}

// read config file, return Config
func ReadConfigFile(fn string) (Config, error) {
	var c Config

	content, err := ioutil.ReadFile(fn)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(content, &c)
	if err != nil {
		return nil, err
	}

	return c, nil
}

// Get
// if the key do not exist, return nil
func Get(key string) interface{} {
	if v, ok := cfg[key]; ok {
		return v
	}

	return nil
}

// GetMust
// if the key do not exist, panic
func GetMust(key string) interface{} {
	v, ok := cfg[key]
	if !ok {
		panic("Not found " + key + " in config.")
	}

	return v
}

// GetString
// Get string type by key
func GetString(key string) string {
	v := Get(key)
	if v == nil {
		return ""
	}
	if s, ok := v.(string); ok {
		return s
	}

	return ""
}

// GetString
// Get string type by key, if not exist, panic
func GetStringMust(key string) string {
	v := Get(key)
	if v == nil {
		panic("Not found key " + key)
		return ""
	}
	if s, ok := v.(string); ok {
		return s
	}

	panic("Cannot convert key " + key + " to string.")
	return ""
}

// GetString
// Get string type by key, if key not exist, return the default
func GetStringDefault(key, def string) string {
	s := GetString(key)
	if s == "" {
		return def
	}
	return s
}

// GetInt
func GetInt(key string) (int64, bool) {
	v := Get(key)
	if v == nil {
		return 0, false
	}
	if i, ok := v.(float64); ok {
		return int64(i), true
	}

	return 0, false
}

func GetInt64Default(key string, dv int64) int64 {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(float64); ok {
		return int64(i)
	}

	return dv
}

func GetIntDefault(key string, dv int) int {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(float64); ok {
		return int(i)
	}

	return dv
}

func GetFloat(key string) (float64, bool) {
	v := Get(key)
	if v == nil {
		return 0, false
	}
	if i, ok := v.(float64); ok {
		return i, true
	}

	return 0.0, false
}

func GetBoolean(key string) (bool, bool) {
	v := Get(key)
	if v == nil {
		return false, false
	}

	if i, ok := v.(bool); ok {
		return i, true
	}

	return false, false
}

func GetBooleanDefault(key string, dv bool) bool {
	v := Get(key)
	if v == nil {
		return dv
	}
	if i, ok := v.(bool); ok {
		return i
	}

	return dv
}

// Scan
//
func Scan(key string, dest interface{}) (err error) {
	val, ok := cfg[key]
	if !ok {
		return KeyNotExist
	}

	rv := reflect.ValueOf(dest)
	if rv.Kind() != reflect.Ptr || rv.IsNil() {
		return fmt.Errorf("param must be pointer and not nil")
	}

	return scan(val, rv)
}

// ScanConfig
func (c Config) ScanConfig(key string, dest interface{}) error {
	val, ok := c[key]
	if !ok {
		return KeyNotExist
	}

	rv := reflect.ValueOf(dest)
	if rv.Kind() != reflect.Ptr || rv.IsNil() {
		return fmt.Errorf("param must be pointer and not nil")
	}

	return scan(val, rv)
}
{'fraction_non_alphanumeric': 0.15626043405676127, 'fraction_numerical': 0.009015025041736227, 'mean_word_length': 2.8232291001914485, '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': '6973166', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2336, 'n_words': 759}