github.com/jtzjtz/kit@v1.0.2/convert/entity.go (about)

     1  package convert
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/mitchellh/mapstructure"
     6  	"reflect"
     7  )
     8  
     9  //模型间相互转换,json tag 需一致,支持弱类型解析
    10  func EntityToEntity(oriEntity interface{}, toEntity interface{}) error {
    11  	config := &mapstructure.DecoderConfig{
    12  		TagName:          "json",
    13  		Metadata:         nil,
    14  		Result:           &toEntity,
    15  		WeaklyTypedInput: true,
    16  	}
    17  	decoder, err := mapstructure.NewDecoder(config)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	decoderErr := decoder.Decode(oriEntity)
    22  	if decoderErr != nil {
    23  		return err
    24  	}
    25  	return nil
    26  }
    27  
    28  //convert to map with empty field deleted
    29  func EntityToMap(entitySrc interface{}) map[string]interface{} {
    30  	retMap := make(map[string]interface{})
    31  	oriJson, err := json.Marshal(entitySrc)
    32  	if err == nil {
    33  		_ = json.Unmarshal(oriJson, &retMap)
    34  
    35  	}
    36  	return retMap
    37  }
    38  
    39  func EntityToMapWithEmpty(entitySrc interface{}, emptyFields []string) map[string]interface{} {
    40  	mapResult := EntityToMap(entitySrc)
    41  	emptyValueMap := MapToMapWithEmpty(entitySrc, emptyFields)
    42  
    43  	for k, v := range emptyValueMap {
    44  		mapResult[k] = v
    45  	}
    46  
    47  	return mapResult
    48  }
    49  
    50  func MapToMapWithEmpty(mapEntity interface{}, emptyFields []string) map[string]interface{} {
    51  	var reflectValue reflect.Value
    52  	var reflectValueSrc reflect.Value
    53  	mapResult := make(map[string]interface{})
    54  
    55  	if len(emptyFields) == 0 {
    56  		return mapResult
    57  	}
    58  	if reflect.TypeOf(mapEntity).Kind() == reflect.Ptr {
    59  		reflectValueSrc = reflect.ValueOf(mapEntity)
    60  		if reflectValueSrc.IsNil() {
    61  			return mapResult
    62  		}
    63  		reflectValue = reflectValueSrc.Elem()
    64  		for _, fieldName := range emptyFields {
    65  			if field := reflectValue.FieldByName(fieldName); field.IsValid() {
    66  				value := field.Interface()
    67  				mapResult[fieldName] = value
    68  			}
    69  		}
    70  
    71  	} else if reflect.TypeOf(mapEntity).Kind() == reflect.Struct {
    72  		reflectValue = reflect.ValueOf(mapEntity)
    73  		for _, fieldName := range emptyFields {
    74  			if field := reflectValue.FieldByName(fieldName); field.IsValid() {
    75  				value := field.Interface()
    76  				mapResult[fieldName] = value
    77  			}
    78  		}
    79  
    80  	}
    81  	return mapResult
    82  }
    83  
    84  // 判断变量是否为空
    85  func Empty(val interface{}) bool {
    86  	if val == nil {
    87  		return true
    88  	}
    89  
    90  	v := reflect.ValueOf(val)
    91  	switch v.Kind() {
    92  	case reflect.String, reflect.Array:
    93  		return v.Len() == 0
    94  	case reflect.Map, reflect.Slice:
    95  		return v.Len() == 0 || v.IsNil()
    96  	case reflect.Bool:
    97  		return !v.Bool()
    98  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    99  		return v.Int() == 0
   100  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   101  		return v.Uint() == 0
   102  	case reflect.Float32, reflect.Float64:
   103  		return v.Float() == 0
   104  	case reflect.Interface, reflect.Ptr:
   105  		return v.IsNil()
   106  	}
   107  
   108  	return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())
   109  }