Skip to content

Commit 181fe0c

Browse files
authored
Merge pull request #212 from swordqiu/hotfix/qj-update-ignore-trivial-changes
fix: update ignoring trivial changes
2 parents 3b18bc6 + 17315f3 commit 181fe0c

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed

reflect.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,3 +758,116 @@ func ConvertValueToString(val interface{}) string {
758758
}
759759
return jsonutils.Marshal(val).String()
760760
}
761+
762+
func getFloatValue(val interface{}) (float64, error) {
763+
switch v := val.(type) {
764+
case float32:
765+
return float64(v), nil
766+
case float64:
767+
return v, nil
768+
case int:
769+
return float64(v), nil
770+
case int8:
771+
return float64(v), nil
772+
case int16:
773+
return float64(v), nil
774+
case int32:
775+
return float64(v), nil
776+
case int64:
777+
return float64(v), nil
778+
case uint:
779+
return float64(v), nil
780+
case uint8:
781+
return float64(v), nil
782+
case uint16:
783+
return float64(v), nil
784+
case uint32:
785+
return float64(v), nil
786+
case uint64:
787+
return float64(v), nil
788+
case *float32:
789+
return float64(*v), nil
790+
case *float64:
791+
return *v, nil
792+
case *int:
793+
return float64(*v), nil
794+
case *int8:
795+
return float64(*v), nil
796+
case *int16:
797+
return float64(*v), nil
798+
case *int32:
799+
return float64(*v), nil
800+
case *int64:
801+
return float64(*v), nil
802+
case *uint:
803+
return float64(*v), nil
804+
case *uint8:
805+
return float64(*v), nil
806+
case *uint16:
807+
return float64(*v), nil
808+
case *uint32:
809+
return float64(*v), nil
810+
case *uint64:
811+
return float64(*v), nil
812+
case string:
813+
return strconv.ParseFloat(v, 64)
814+
case *string:
815+
return strconv.ParseFloat(*v, 64)
816+
}
817+
return 0, errors.ErrInvalidFormat
818+
}
819+
820+
func getTimeValue(val interface{}) (time.Time, error) {
821+
switch v := val.(type) {
822+
case time.Time:
823+
return v, nil
824+
case *time.Time:
825+
return *v, nil
826+
case string:
827+
return timeutils.ParseTimeStr(v)
828+
case *string:
829+
return timeutils.ParseTimeStr(*v)
830+
}
831+
return time.Time{}, errors.ErrInvalidFormat
832+
}
833+
834+
const MIN_FLOAT_EQUAL_DIFF = float64(0.000001)
835+
836+
func floatEqual(of, nf float64) bool {
837+
if of > nf {
838+
return of < MIN_FLOAT_EQUAL_DIFF+nf
839+
} else if of < nf {
840+
return of+MIN_FLOAT_EQUAL_DIFF > nf
841+
} else {
842+
return true
843+
}
844+
}
845+
846+
const MIN_MICRO_SECOND_EQUAL_DIFF = 1000000
847+
848+
func timeEqual(of, nf time.Time) bool {
849+
ofUnix := of.UnixMicro()
850+
nfUnix := nf.UnixMicro()
851+
if ofUnix == nfUnix {
852+
return true
853+
}
854+
if ofUnix > nfUnix {
855+
return ofUnix < MIN_MICRO_SECOND_EQUAL_DIFF+nfUnix
856+
} else {
857+
return ofUnix+MIN_MICRO_SECOND_EQUAL_DIFF > nfUnix
858+
}
859+
}
860+
861+
func EqualsGrossValue(of, nf interface{}) bool {
862+
ofFloat, ofErr := getFloatValue(of)
863+
nfFloat, nfErr := getFloatValue(nf)
864+
if ofErr == nil && nfErr == nil {
865+
return floatEqual(ofFloat, nfFloat)
866+
}
867+
ofTime, ofErr := getTimeValue(of)
868+
nfTime, nfErr := getTimeValue(nf)
869+
if ofErr == nil && nfErr == nil {
870+
return timeEqual(ofTime, nfTime)
871+
}
872+
return false
873+
}

reflect_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,123 @@ func TestConvertValueToTriState(t *testing.T) {
401401
}
402402
}
403403
}
404+
405+
func TestEqualsGrossValue(t *testing.T) {
406+
cases := []struct {
407+
of interface{}
408+
nf interface{}
409+
want bool
410+
}{
411+
{
412+
of: 1.0,
413+
nf: 1.0,
414+
want: true,
415+
},
416+
{
417+
of: 1.0,
418+
nf: 1.00001,
419+
want: false,
420+
},
421+
{
422+
of: 1.0,
423+
nf: 0.99999,
424+
want: false,
425+
},
426+
{
427+
of: 1.0,
428+
nf: 1.000002,
429+
want: false,
430+
},
431+
{
432+
of: 1.0,
433+
nf: 0.999998,
434+
want: false,
435+
},
436+
{
437+
of: 1.0,
438+
nf: 1.000001,
439+
want: false,
440+
},
441+
{
442+
of: 1.0,
443+
nf: 1.0000011,
444+
want: false,
445+
},
446+
{
447+
of: 1.0,
448+
nf: 1.0000009,
449+
want: true,
450+
},
451+
{
452+
of: 1.0,
453+
nf: 0.999999,
454+
want: false,
455+
},
456+
{
457+
of: 1.0,
458+
nf: 0.9999989,
459+
want: false,
460+
},
461+
{
462+
of: 1.0,
463+
nf: 0.9999991,
464+
want: true,
465+
},
466+
{
467+
of: 1.0,
468+
nf: 1.0000001,
469+
want: true,
470+
},
471+
{
472+
of: 1.0,
473+
nf: 0.9999999,
474+
want: true,
475+
},
476+
{
477+
of: "1.0",
478+
nf: "0.9999999",
479+
want: true,
480+
},
481+
{
482+
of: 1,
483+
nf: "0.9999999",
484+
want: true,
485+
},
486+
{
487+
of: "2025-01-01T00:00:00.000000Z",
488+
nf: "2025-01-01T00:00:00.000001Z",
489+
want: true,
490+
},
491+
{
492+
of: "2025-01-01T00:00:00.000000Z",
493+
nf: "2025-01-01T00:00:00.001001Z",
494+
want: true,
495+
},
496+
{
497+
of: "2025-01-01T00:00:00.000000Z",
498+
nf: "2025-01-01T00:00:01.001000Z",
499+
want: false,
500+
},
501+
{
502+
of: "2025-01-01T00:00:00.999999Z",
503+
nf: "2025-01-01T00:00:01.000000Z",
504+
want: true,
505+
},
506+
{
507+
of: "2025-01-01T00:00:00.999999Z",
508+
nf: "2025-01-01T00:00:01.999000Z",
509+
want: true,
510+
},
511+
{
512+
of: "2025-01-01T00:00:00.999999Z",
513+
nf: "2025-01-01T00:00:01.999999Z",
514+
want: false,
515+
},
516+
}
517+
for _, c := range cases {
518+
got := EqualsGrossValue(c.of, c.nf)
519+
if got != c.want {
520+
t.Errorf("want %v got %v for %v and %v", c.want, got, c.of, c.nf)
521+
}
522+
}
523+
}

update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ func (us *SUpdateSession) SaveUpdateSql(dt interface{}) (*SUpdateSQLResult, erro
174174
if ofJsonStr == nfJsonStr {
175175
continue
176176
}
177+
if EqualsGrossValue(of, nf) {
178+
continue
179+
}
177180
}
178181
if c.IsZero(nf) && c.IsText() {
179182
nf = nil

0 commit comments

Comments
 (0)