<commit_before>package reflector

import (
	"errors"
	"reflect"
)

type StructReflector interface {
	// Interface returns the struct as interface{}.
	Interface() interface{}

	// Value returns the raw reflect.Value.
	Value() Reflector

	// Type returns the raw reflect.Type.
	Type() reflect.Type

	// New creates a new instance of the struct and returns an reflector.
	New() StructReflector

	// Returns a Reflector for a field, or nil if the field does not exist.
	Field(fieldName string) Reflector

	// Fields returns a map of fields, allowing you to easily iterate over all fields.
	Fields() map[string]Reflector

	// HasField returns true if the struct has a field with the specified name.
	HasField(fieldName string) bool

	// FieldValue returns the value of a struct field, or an error if the field
	// does not exist.
	FieldValue(fieldName string) (interface{}, error)

	// UFieldValue returns the value of a struct field, or nil if the field
	// does not exist.
	UFieldValue(fieldName string) interface{}

	SetFieldValue(fieldName string, value interface{}, convert ...bool) error
	SetField(fieldName string, value Reflector, convert ...bool) error

	// ToMap recursively converts the struct to a map[string]interface{} map.
	// You can optionally omit zero or empty values.
	ToMap(omitZero, omitEmpty bool) map[string]interface{}

	// FromMap sets struct fields from a map[string]interface{} map.
	//
	// You can optionally enable conversion of types that do not match by
	// passing true as a second argument.
	//
	// An error will be returned if values have a type mismatch, or, if
	// conversion is enabled, if a field conversion fails.
	FromMap(data map[string]interface{}, convert ...bool) error
}

type structReflector struct {
	item       Reflector
	structItem Reflector
	isPtr      bool
}

// Ensure that structReflector implements StructReflector.
var _ StructReflector = (*structReflector)(nil)

// Struct builds a new StructReflector.
// You may pass in a struct or a pointer to a struct.
func newStructReflector(v Reflector) (StructReflector, error) {
	if v.IsStruct() {
		return &structReflector{
			item:       v,
			structItem: v,
		}, nil
	} else if v.IsStructPtr() {
		if v.IsNil() {
			newStruct := New(v.Type().Elem())
			if err := v.Set(newStruct); err != nil {
				return nil, err
			}
		}

		return &structReflector{
			item:       v,
			structItem: v.Elem(),
			isPtr:      true,<commit_msg>Struct constructor now dereferences interfaces<commit_after>package reflector

import (
	"errors"
	"reflect"
)

type StructReflector interface {
	// Interface returns the struct as interface{}.
	Interface() interface{}

	// Value returns the raw reflect.Value.
	Value() Reflector

	// Type returns the raw reflect.Type.
	Type() reflect.Type

	// New creates a new instance of the struct and returns an reflector.
	New() StructReflector

	// Returns a Reflector for a field, or nil if the field does not exist.
	Field(fieldName string) Reflector

	// Fields returns a map of fields, allowing you to easily iterate over all fields.
	Fields() map[string]Reflector

	// HasField returns true if the struct has a field with the specified name.
	HasField(fieldName string) bool

	// FieldValue returns the value of a struct field, or an error if the field
	// does not exist.
	FieldValue(fieldName string) (interface{}, error)

	// UFieldValue returns the value of a struct field, or nil if the field
	// does not exist.
	UFieldValue(fieldName string) interface{}

	SetFieldValue(fieldName string, value interface{}, convert ...bool) error
	SetField(fieldName string, value Reflector, convert ...bool) error

	// ToMap recursively converts the struct to a map[string]interface{} map.
	// You can optionally omit zero or empty values.
	ToMap(omitZero, omitEmpty bool) map[string]interface{}

	// FromMap sets struct fields from a map[string]interface{} map.
	//
	// You can optionally enable conversion of types that do not match by
	// passing true as a second argument.
	//
	// An error will be returned if values have a type mismatch, or, if
	// conversion is enabled, if a field conversion fails.
	FromMap(data map[string]interface{}, convert ...bool) error
}

type structReflector struct {
	item       Reflector
	structItem Reflector
	isPtr      bool
}

// Ensure that structReflector implements StructReflector.
var _ StructReflector = (*structReflector)(nil)

// Struct builds a new StructReflector.
// You may pass in a struct or a pointer to a struct.
func newStructReflector(v Reflector) (StructReflector, error) {
	// Dereference interfaces.
	if v.IsInterface() {
		if v.IsNil() {
			return nil, errors.New(ERR_INVALID_VALUE)
		}
		v = v.Elem()
	}

	if v.IsStruct() {
		return &structReflector{
			item:       v,
			structItem: v,
		}, nil
	} else if v.IsStructPtr() {
		if v.IsNil() {
			newStruct := New(v.Type().Elem())
			if err := v.Set(newStruct); err != nil {
				return nil, err
			}
		}

		return &structReflector{
			item:       v,
			structItem: v.Elem(),
			isPtr:      true,
{'fraction_non_alphanumeric': 0.09353809333865178, 'fraction_numerical': 0.0, 'mean_word_length': 3.4856887298747763, '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': '279911', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1481, 'n_words': 596}