gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/jsonmap/jsonmap.go (about)

     1  package jsonmap
     2  
     3  import (
     4  	"gitee.com/zhongguo168a/gocodes/datax/convertx"
     5  	"gitee.com/zhongguo168a/gocodes/datax/schemax/basickind"
     6  	"reflect"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  func InterfaceToJSONKey(ival interface{}) string {
    12  	val := ""
    13  	switch x := ival.(type) {
    14  	case string:
    15  		val = x
    16  	case int:
    17  		val = strconv.Itoa(x)
    18  	case int8:
    19  		val = strconv.Itoa(int(x))
    20  	case int16:
    21  		val = strconv.Itoa(int(x))
    22  	case int32:
    23  		val = strconv.Itoa(int(x))
    24  	case int64:
    25  		val = strconv.Itoa(int(x))
    26  	case uint:
    27  		val = strconv.Itoa(int(x))
    28  	case uint8:
    29  		val = strconv.Itoa(int(x))
    30  	case uint16:
    31  		val = strconv.Itoa(int(x))
    32  	case uint32:
    33  		val = strconv.Itoa(int(x))
    34  	case uint64:
    35  		val = strconv.Itoa(int(x))
    36  	case float32:
    37  		val = strconv.Itoa(int(x))
    38  	case float64:
    39  		val = strconv.Itoa(int(x))
    40  	}
    41  	return val
    42  }
    43  
    44  func InterfaceToJSONValue(val interface{}) (r interface{}) {
    45  	switch v := val.(type) {
    46  	case string:
    47  		r = v
    48  	case bool:
    49  		r = v
    50  	case int:
    51  		r = v
    52  	case int8:
    53  		r = int(v)
    54  	case int16:
    55  		r = int(v)
    56  	case int32:
    57  		r = int(v)
    58  	case int64:
    59  		r = int(v)
    60  	case uint:
    61  		r = v
    62  	case uint8:
    63  		r = uint(v)
    64  	case uint16:
    65  		r = uint(v)
    66  	case uint32:
    67  		r = uint(v)
    68  	case uint64:
    69  		r = uint(v)
    70  	case float32:
    71  		r = float64(v)
    72  	case float64:
    73  		r = float64(v)
    74  	default:
    75  		r = val
    76  	}
    77  	return
    78  }
    79  
    80  // 把目标值,转换成jsonmap定义的类型
    81  func ConvertJsonKeyToReflectKind(kind reflect.Kind, val interface{}) (r interface{}) {
    82  	switch kind {
    83  	case reflect.Bool:
    84  		r = convertx.AnyToBool(val)
    85  	case reflect.Int:
    86  		r = int(convertx.AnyToInt64(val))
    87  	case reflect.Int8:
    88  		r = int8(convertx.AnyToInt64(val))
    89  	case reflect.Int16:
    90  		r = int16(convertx.AnyToInt64(val))
    91  	case reflect.Int32:
    92  		r = int32(convertx.AnyToInt64(val))
    93  	case reflect.Int64:
    94  		r = int64(convertx.AnyToInt64(val))
    95  	case reflect.Uint:
    96  		r = uint(convertx.AnyToInt64(val))
    97  	case reflect.Uint8:
    98  		r = uint8(convertx.AnyToInt64(val))
    99  	case reflect.Uint16:
   100  		r = uint16(convertx.AnyToInt64(val))
   101  	case reflect.Uint32:
   102  		r = uint32(convertx.AnyToInt64(val))
   103  	case reflect.Uint64:
   104  		r = uint64(convertx.AnyToInt64(val))
   105  	case reflect.Float32:
   106  		r = float32(convertx.AnyToFloat64(val))
   107  	case reflect.Float64:
   108  		r = convertx.AnyToFloat64(val)
   109  	case reflect.String:
   110  		r = convertx.AnyToString(val)
   111  	}
   112  	return
   113  }
   114  
   115  // ConvertBasicToJsonValue 把目标值,转换成jsonmap定义的类型
   116  func ConvertBasicToJsonValue(kind basickind.Kind, val interface{}) (r interface{}) {
   117  	switch kind {
   118  	case basickind.Bool:
   119  		r = convertx.AnyToBool(val)
   120  	case basickind.Int8:
   121  		fallthrough
   122  	case basickind.Int16:
   123  		fallthrough
   124  	case basickind.Int32:
   125  		fallthrough
   126  	case basickind.Int64:
   127  		r = convertx.AnyToInt(val)
   128  	case basickind.Uint8:
   129  		fallthrough
   130  	case basickind.Uint16:
   131  		fallthrough
   132  	case basickind.Uint32:
   133  		fallthrough
   134  	case basickind.Uint64:
   135  		r = convertx.AnyToUint(val)
   136  	case basickind.Float32:
   137  		fallthrough
   138  	case basickind.Float64:
   139  		r = convertx.AnyToFloat64(val)
   140  	case basickind.String:
   141  		r = convertx.AnyToString(val)
   142  	}
   143  	return
   144  }
   145  
   146  func RemoveNil(m map[string]interface{}) {
   147  	removeNil(m)
   148  }
   149  
   150  func removeNil(m map[string]interface{}) {
   151  	for key, val := range m {
   152  		//fmt.Printf("remove name=%v, %T\n", key, val)
   153  		switch v := val.(type) {
   154  		case map[string]interface{}:
   155  			if v == nil {
   156  				delete(m, key)
   157  			} else {
   158  				removeNil(v)
   159  			}
   160  		case []interface{}:
   161  			if v == nil {
   162  				delete(m, key)
   163  			} else {
   164  				if len(v) == 0 {
   165  					delete(m, key)
   166  				} else {
   167  					for i := 0; i < len(v); i++ {
   168  						iitem := v[i]
   169  						item, ok := iitem.(map[string]interface{})
   170  						if ok {
   171  							removeNil(item)
   172  						}
   173  					}
   174  				}
   175  			}
   176  		case string:
   177  			if v == "" {
   178  				delete(m, key)
   179  			}
   180  		case interface{}:
   181  			if v == nil {
   182  				delete(m, key)
   183  			}
   184  		default:
   185  			if v == nil {
   186  				//fmt.Println("delete default", key)
   187  				delete(m, key)
   188  			}
   189  		}
   190  	}
   191  }
   192  
   193  func parseTag(val string) (r map[string]string) {
   194  	r = map[string]string{}
   195  	arr := strings.Split(val, " ")
   196  	for _, arrval := range arr {
   197  		if strings.HasPrefix("--", arrval) {
   198  			paramsarr := strings.Split(arrval, "=")
   199  			if len(paramsarr) == 1 {
   200  				r[paramsarr[0]] = "true"
   201  			} else {
   202  				r[paramsarr[0]] = r[paramsarr[1]]
   203  			}
   204  
   205  		}
   206  	}
   207  	return
   208  }