// Copyright 2024 Patial Tech (Ankit Patial). // // This file is part of code.patial.tech/go/appcore, which is MIT licensed. // See http://opensource.org/licenses/MIT package structs import ( "reflect" ) func Map(obj any) map[string]any { result := make(map[string]any) val := reflect.ValueOf(obj) if val.Kind() == reflect.Pointer { val = val.Elem() } typ := val.Type() for i := range val.NumField() { fieldName := typ.Field(i).Name field := val.Field(i) fieldValueKind := field.Kind() var fieldValue any switch fieldValueKind { case reflect.Struct: fieldValue = Map(field.Interface()) case reflect.Pointer: if !field.IsNil() && field.Elem().Kind() == reflect.Struct { fieldValue = Map(field.Elem().Interface()) } else { fieldValue = field.Interface() } default: fieldValue = field.Interface() } result[fieldName] = fieldValue } return result }