package utils

import (
	"errors"
	"reflect"
)

func StructToMap(s any) (map[string]any, error) {
	result := make(map[string]any)

	val := reflect.ValueOf(s)

	if val.Kind() != reflect.Struct {
		return nil, errors.New("provided value is not struct")
	}

	for i := range val.NumField() {
		field := val.Type().Field(i)
		value := val.Field(i)

		if value.IsZero() || value.IsNil() {
			continue
		}

		result[field.Name] = value.Interface()
	}

	return result, nil
}
