增加比较两个map某一个字段值是否相等的能力 #4
							
								
								
									
										93
									
								
								tool/define/diff.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								tool/define/diff.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,93 @@ | |||||||
|  | // Package define ... | ||||||
|  | // | ||||||
|  | // Description : define ... | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 2024-03-08 11:04 | ||||||
|  | package define | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"git.zhangdeman.cn/zhangdeman/wrapper" | ||||||
|  | 	"reflect" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // DiffOption 做数据对比时的选项 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 11:12 2024/3/8 | ||||||
|  | type DiffOption struct { | ||||||
|  | 	StrictMode          bool                      `json:"strict_mode"`         // 采用严格模式: 1 != "1" , 采用非严格模式 1 == "1" | ||||||
|  | 	IgnoreEmptyString   bool                      `json:"ignore_empty_string"` // 忽略空字符串, 若输入值为空字符串, 则不做比较, 认为两个值相同 | ||||||
|  | 	IgnoreZeroNumber    bool                      `json:"ignore_zero_number"`  // 忽略置为0的数字, 若输入的数据为数字类型, 则不做比较, 认为两个值相同 | ||||||
|  | 	IgnoreNil           bool                      `json:"ignore_nil"`          // 忽略 nil 值, 若输入值为NIL , 则不做比较, 认为两个值相同 | ||||||
|  | 	CustomDiffFuncTable map[string]CustomDiffFunc `json:"-"`                   // 外部自定义的字段是否相同的比较函数, 会优先使用外部输入的比较函数 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // CustomDiffFunc 自定义字段对比方法 | ||||||
|  | // | ||||||
|  | // 输入分别如下: | ||||||
|  | // | ||||||
|  | // field : 要对比的字段 | ||||||
|  | // | ||||||
|  | // inputVal : 输入的原始数据 | ||||||
|  | // | ||||||
|  | // storageVal : 当前存储的数据 | ||||||
|  | // | ||||||
|  | // option : 对比时的额外选项 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 11:06 2024/3/8 | ||||||
|  | type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult | ||||||
|  |  | ||||||
|  | // DiffResult 对比结果 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 11:10 2024/3/8 | ||||||
|  | type DiffResult struct { | ||||||
|  | 	OldVal     interface{} `json:"old_val"`     // 当前field在storageVal中的值 | ||||||
|  | 	NewVal     interface{} `json:"new_val"`     // 当前field在inputVal中的值 | ||||||
|  | 	IsSame     bool        `json:"is_same"`     // 两个值是否相同 | ||||||
|  | 	DiffReason string      `json:"diff_reason"` // 两个值不同的原因 | ||||||
|  | 	Err        error       `json:"err"`         // 对比过程中是否出现异常 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	DiffReasonTypeNotMatch  = "TYPE_NOT_MATCH"  // 类型不匹配 | ||||||
|  | 	DiffReasonValueNotMatch = "VALUE_NOT_MATCH" // 数据值不匹配 | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	// 当前仅支持基础类型的比较,不支持slice/map/struct等复杂类型的比较 | ||||||
|  | 	supportValueTypeTable = map[reflect.Kind]interface{}{ | ||||||
|  | 		reflect.Bool:    true, | ||||||
|  | 		reflect.Int:     true, | ||||||
|  | 		reflect.Int8:    true, | ||||||
|  | 		reflect.Int16:   true, | ||||||
|  | 		reflect.Int32:   true, | ||||||
|  | 		reflect.Int64:   true, | ||||||
|  | 		reflect.Uint:    true, | ||||||
|  | 		reflect.Uint8:   true, | ||||||
|  | 		reflect.Uint16:  true, | ||||||
|  | 		reflect.Uint32:  true, | ||||||
|  | 		reflect.Uint64:  true, | ||||||
|  | 		reflect.Float32: true, | ||||||
|  | 		reflect.Float64: true, | ||||||
|  | 		// reflect.Ptr:     true, | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // IsSupportValueType ... | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 11:23 2024/3/8 | ||||||
|  | func IsSupportValueType(kind reflect.Kind) bool { | ||||||
|  | 	if _, exist := supportValueTypeTable[kind]; exist { | ||||||
|  | 		return true | ||||||
|  | 	} | ||||||
|  | 	return false | ||||||
|  | } | ||||||
							
								
								
									
										15
									
								
								tool/diff.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								tool/diff.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | |||||||
|  | // Package tool ... | ||||||
|  | // | ||||||
|  | // Description : 对比两个数据是否相同 + 构建不同数据的前后值 | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 2024-03-08 11:03 | ||||||
|  | package tool | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	Diff = &diff{} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type diff struct { | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user