<commit_before>package utils

import (
	"errors"
	"reflect"
	"strings"
)

func StringInArray(key string, array []string) bool {
	if key == "" || len(array) == 0 {
		return false
	}
	for _, v := range array {
		if strings.ToLower(key) == strings.ToLower(v) {
			return true
		}
	}
	return false
}

func StructToMap(data interface{}, lowerKey bool, except ...string) (m map[string]interface{}, err error) {
	m = make(map[string]interface{}, 0)
	if data == nil {
		err = errors.New("struct to map error data is nil")
		return
	}
	v := reflect.ValueOf(data)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	if v.Kind() != reflect.Struct {
		err = errors.New("data must be struct")
		return
	}
	t := v.Type()
	for i := 0; i < v.NumField(); i++ {
		f := v.Field(i)
		if StringInArray(t.Field(i).Name, except) {
			continue
		}
		if lowerKey {
			m[strings.ToLower(t.Field(i).Name)] = f.Interface()
		} else {
			m[t.Field(i).Name] = f.Interface()
		}
	}
	return
}<commit_msg>modify method StructToMap<commit_after>package utils

import (
	"errors"
	"reflect"
	"strings"
)

func StringInArray(key string, array []string) bool {
	if key == "" || len(array) == 0 {
		return false
	}
	for _, v := range array {
		if strings.ToLower(key) == strings.ToLower(v) {
			return true
		}
	}
	return false
}

func StructToMap(data interface{}, lowerKey, bExcept bool, fields ...string) (m map[string]interface{}, err error) {
	m = make(map[string]interface{}, 0)
	if data == nil {
		err = errors.New("struct to map error data is nil")
		return
	}
	v := reflect.ValueOf(data)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	if v.Kind() != reflect.Struct {
		err = errors.New("data must be struct")
		return
	}
	t := v.Type()
	for i := 0; i < v.NumField(); i++ {
		f := v.Field(i)
		if bExcept {
			if StringInArray(t.Field(i).Name, fields) {
				continue
			}
		} else {
			if !StringInArray(t.Field(i).Name, fields) {
				continue
			}
		}
		if lowerKey {
			m[strings.ToLower(t.Field(i).Name)] = f.Interface()
		} else {
			m[t.Field(i).Name] = f.Interface()
		}
	}
	return
}
{'fraction_non_alphanumeric': 0.17249757045675412, 'fraction_numerical': 0.0029154518950437317, 'mean_word_length': 3.184959349593496, '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': '3893736', 'n_tokens_mistral': 853, 'n_tokens_neox': 788, 'n_words': 196}