<commit_before>package flect

import (
	"reflect"
	"strings"
	"github.com/eyecuelab/kit/functools"
)

type tagOpts []string

func (opts tagOpts) HasOption(name string) bool {
	return functools.StringSliceContains(opts, name)
}

func TagValues(t reflect.StructTag, name string) (value string, opts tagOpts, ok bool) {
	fullValue, ok := t.Lookup(name)
	if !ok {
		return
	}
	splitValue := strings.Split(fullValue, ",")
	for i := 1; i < len(splitValue); i++ {
		opts = append(opts, splitValue[i])
	}
	return splitValue[0], opts, true
}

func GroupValuesByTagOption(tag string, excludeEmpties bool, structs ...interface{}) map[string]map[string]interface{} {
	toScan := make([]reflect.Value, len(structs))
	optsMap := make(map[string]map[string]interface{})

	for i, s := range structs {
		toScan[i] = reflect.ValueOf(s)
	}

	for _, scan := range toScan {
		for i := 0; i < scan.NumField(); i++ {
			tagValue, tagOpts, ok := TagValues(scan.Type().Field(i).Tag, tag)
			if ok {
				attrValue := scan.Field(i).Interface()
				if IsZeroOfType(attrValue) && excludeEmpties {
					continue
				}
				for _, tagOpt := range tagOpts {
					if optsMap[tagOpt] == nil {
						optsMap[tagOpt] = make(map[string]interface{})
					}
					optsMap[tagOpt][tagValue] = attrValue
				}
			}
		}
	}
	return optsMap
}<commit_msg>refactored `GroupValuesByTagOption` to two different functions to remove flag arg<commit_after>package flect

import (
	"reflect"
	"strings"

	"github.com/eyecuelab/kit/functools"
)

type tagOpts []string

func (opts tagOpts) HasOption(name string) bool {
	return functools.StringSliceContains(opts, name)
}

func TagValues(t reflect.StructTag, name string) (value string, opts tagOpts, ok bool) {
	fullValue, ok := t.Lookup(name)
	if !ok {
		return
	}
	splitValue := strings.Split(fullValue, ",")
	for i := 1; i < len(splitValue); i++ {
		opts = append(opts, splitValue[i])
	}
	return splitValue[0], opts, true
}

func GroupValuesByTagOption(tag string, structs ...interface{}) map[string]map[string]interface{} {
	toScan := make([]reflect.Value, len(structs))
	optsMap := make(map[string]map[string]interface{})

	for i, s := range structs {
		toScan[i] = reflect.ValueOf(s)
	}

	for _, scan := range toScan {
		for i := 0; i < scan.NumField(); i++ {
			tagValue, tagOpts, ok := TagValues(scan.Type().Field(i).Tag, tag)
			if ok {
				attrValue := scan.Field(i).Interface()
				for _, tagOpt := range tagOpts {
					if optsMap[tagOpt] == nil {
						optsMap[tagOpt] = make(map[string]interface{})
					}
					optsMap[tagOpt][tagValue] = attrValue
				}
			}
		}
	}
	return optsMap
}

func GroupNonEmptyValuesByTagOption(tag string, structs ...interface{}) map[string]map[string]interface{} {
	toScan := make([]reflect.Value, len(structs))
	optsMap := make(map[string]map[string]interface{})

	for i, s := range structs {
		toScan[i] = reflect.ValueOf(s)
	}

	for _, scan := range toScan {
		for i := 0; i < scan.NumField(); i++ {
			tagValue, tagOpts, ok := TagValues(scan.Type().Field(i).Tag, tag)
			if ok {
				attrValue := scan.Field(i).Interface()
				if IsZeroOfType(attrValue) {
					continue
				}
				for _, tagOpt := range tagOpts {
					if optsMap[tagOpt] == nil {
						optsMap[tagOpt] = make(map[string]interface{})
					}
					optsMap[tagOpt][tagValue] = attrValue
				}
			}
		}
	}
	return optsMap
}
{'fraction_non_alphanumeric': 0.15167017755040627, 'fraction_numerical': 0.0021065302437556425, 'mean_word_length': 3.8954344624447717, 'pattern_counts': {'":': 0, '<': 8, '<?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': '3165055', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1179, 'n_words': 268}