common.go 920 字节
package common

import (
	"github.com/linmadan/egglib-go/utils/string_convert"
	"reflect"
)

func SimpleStructToMap(toMapStruct interface{}) map[string]interface{} {
	m := ObjectToMap(toMapStruct)
	if pageNumber, ok := m["pageNumber"]; ok {
		var pageSize int64
		if _, ok := m["pageSize"]; ok {
			pageSize = m["pageSize"].(int64)
		} else {
			pageSize = 20
		}
		m["offset"] = (pageNumber.(int64) - 1) * pageSize
		m["limit"] = pageSize
	}
	return m
}

func ObjectToMap(o interface{}) map[string]interface{} {
	if o == nil {
		return nil
	}
	value := reflect.ValueOf(o)
	if value.Kind() != reflect.Ptr {
		return nil
	}
	elem := value.Elem()
	relType := elem.Type()
	m := make(map[string]interface{})
	for i := 0; i < relType.NumField(); i++ {
		field := relType.Field(i)
		if elem.Field(i).IsZero() {
			continue
		}
		m[string_convert.CamelCase(field.Name, false, false)] = elem.Field(i).Interface()
	}
	return m
}