github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_maps.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gconv
     8  
     9  import "github.com/wangyougui/gf/v2/internal/json"
    10  
    11  // SliceMap is alias of Maps.
    12  func SliceMap(any interface{}, option ...MapOption) []map[string]interface{} {
    13  	return Maps(any, option...)
    14  }
    15  
    16  // SliceMapDeep is alias of MapsDeep.
    17  // Deprecated: used SliceMap instead.
    18  func SliceMapDeep(any interface{}) []map[string]interface{} {
    19  	return MapsDeep(any)
    20  }
    21  
    22  // Maps converts `value` to []map[string]interface{}.
    23  // Note that it automatically checks and converts json string to []map if `value` is string/[]byte.
    24  func Maps(value interface{}, option ...MapOption) []map[string]interface{} {
    25  	if value == nil {
    26  		return nil
    27  	}
    28  	switch r := value.(type) {
    29  	case string:
    30  		list := make([]map[string]interface{}, 0)
    31  		if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
    32  			if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
    33  				return nil
    34  			}
    35  			return list
    36  		} else {
    37  			return nil
    38  		}
    39  
    40  	case []byte:
    41  		list := make([]map[string]interface{}, 0)
    42  		if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
    43  			if err := json.UnmarshalUseNumber(r, &list); err != nil {
    44  				return nil
    45  			}
    46  			return list
    47  		} else {
    48  			return nil
    49  		}
    50  
    51  	case []map[string]interface{}:
    52  		return r
    53  
    54  	default:
    55  		array := Interfaces(value)
    56  		if len(array) == 0 {
    57  			return nil
    58  		}
    59  		list := make([]map[string]interface{}, len(array))
    60  		for k, v := range array {
    61  			list[k] = Map(v, option...)
    62  		}
    63  		return list
    64  	}
    65  }
    66  
    67  // MapsDeep converts `value` to []map[string]interface{} recursively.
    68  //
    69  // TODO completely implement the recursive converting for all types.
    70  // Deprecated: used Maps instead.
    71  func MapsDeep(value interface{}, tags ...string) []map[string]interface{} {
    72  	if value == nil {
    73  		return nil
    74  	}
    75  	switch r := value.(type) {
    76  	case string:
    77  		list := make([]map[string]interface{}, 0)
    78  		if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
    79  			if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
    80  				return nil
    81  			}
    82  			return list
    83  		} else {
    84  			return nil
    85  		}
    86  
    87  	case []byte:
    88  		list := make([]map[string]interface{}, 0)
    89  		if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
    90  			if err := json.UnmarshalUseNumber(r, &list); err != nil {
    91  				return nil
    92  			}
    93  			return list
    94  		} else {
    95  			return nil
    96  		}
    97  
    98  	case []map[string]interface{}:
    99  		list := make([]map[string]interface{}, len(r))
   100  		for k, v := range r {
   101  			list[k] = MapDeep(v, tags...)
   102  		}
   103  		return list
   104  
   105  	default:
   106  		array := Interfaces(value)
   107  		if len(array) == 0 {
   108  			return nil
   109  		}
   110  		list := make([]map[string]interface{}, len(array))
   111  		for k, v := range array {
   112  			list[k] = MapDeep(v, tags...)
   113  		}
   114  		return list
   115  	}
   116  }