diff --git a/cmd/struct_equal_generator/generate.tmpl b/cmd/struct_equal_generator/generate.tmpl index 52994502..927627c8 100644 --- a/cmd/struct_equal_generator/generate.tmpl +++ b/cmd/struct_equal_generator/generate.tmpl @@ -22,22 +22,22 @@ {{end -}} func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { {{- if or .NeedsOptions .NeedsOptionsIndex }} - opt := getOptions(opts...) + opt := getOptions(opts...) {{end}} {{- if eq .Mode "struct" }} - {{- range .Fields}} + {{- range .Fields}} {{- if and (HasPrefix .Name "Index") (HasPrefix .Type "*int64") }} if !opt.SkipIndex && !equalPointers(s.{{.Name}}, t.{{.Name}}){ - return false + return false } {{- else if or (eq .Type "string" "bool") (HasPrefix .Type "int") }} if s.{{.Name}} != t.{{.Name}} { return false } {{- else if or (HasPrefix .Type "[]int") (HasPrefix .Type "[]string")}} - if !equalComparableSlice(s.{{.Name}}, t.{{.Name}}, opt) { - return false - } + if !equalComparableSlice(s.{{.Name}}, t.{{.Name}}, opt) { + return false + } {{- else if or (HasPrefix .Type "*string") (HasPrefix .Type "*int") (HasPrefix .Type "*bool")}} if !equalPointers(s.{{.Name}}, t.{{.Name}}) { return false @@ -60,11 +60,11 @@ func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { return false } {{- else if (HasPrefix .Type "Date")}} - if !s.{{.Name}}.Equal(t.{{.Name}}) { - return false - } + if !s.{{.Name}}.Equal(t.{{.Name}}) { + return false + } {{- else if or (HasPrefix .Type "*") (not .IsBasicType)}} - {{- if eq .TypeInFile "time.Time"}} {{/* time.Time has Equal, but nothing else */}} + {{- if or (eq .TypeInFile "time.Time") (eq .TypeInFile "strfmt.DateTime") }} {{/* time.Time has Equal, but nothing else */}} if !s.{{.Name}}.Equal({{if HasPrefix .Type "*"}}*{{end}}t.{{.Name}}) { return false } @@ -82,7 +82,7 @@ func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { return true {{end}} {{- if eq .Mode "array" }} - {{- if and .IsComparable .IsBasicType }} + {{- if and .IsComparable .IsBasicType }} return equalComparableSlice(s,t,opt) {{- else }} if !opt.NilSameAsEmpty { @@ -93,10 +93,10 @@ func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { return false } } - if len(s) != len(t) { + if len(s) != len(t) { return false } - for i, v := range s { + for i, v := range s { {{- if .IsBasicType}} {{- if HasPrefix .Type "*"}} if *v != *t[i] { @@ -117,10 +117,9 @@ func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { {{- end }} {{- end -}} {{- if eq .Mode "ident" }} - return s == t + return s == t {{- end -}} } - // Diff checks if two structs of type {{.Name}} are equal // {{- if .NeedsOptions }} @@ -144,22 +143,22 @@ func (s {{.Name}}) Equal(t {{.Name}}, opts ...Options) bool { {{end -}} func (s {{.Name}}) Diff(t {{.Name}}, opts ...Options) map[string][]interface{} { {{- if or .NeedsOptions .NeedsOptionsIndex }} - opt := getOptions(opts...) + opt := getOptions(opts...) {{end}} - diff := make(map[string][]interface{}) - {{- range .Fields}} + diff := make(map[string][]interface{}) + {{- range .Fields}} {{- if and (HasPrefix .Name "Index") (HasPrefix .Type "*int64") }} if !opt.SkipIndex && !equalPointers(s.{{.Name}}, t.{{.Name}}){ - diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} + diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } {{- else if or (eq .Type "string" "bool") (HasPrefix .Type "int") }} if s.{{.Name}} != t.{{.Name}} { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } {{- else if or (HasPrefix .Type "[]int") (HasPrefix .Type "[]string")}} - if !equalComparableSlice(s.{{.Name}}, t.{{.Name}}, opt) { - diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} - } + if !equalComparableSlice(s.{{.Name}}, t.{{.Name}}, opt) { + diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} + } {{- else if or (HasPrefix .Type "*string") (HasPrefix .Type "*int") (HasPrefix .Type "*bool")}} if !equalPointers(s.{{.Name}}, t.{{.Name}}) { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} @@ -172,7 +171,7 @@ func (s {{.Name}}) Diff(t {{.Name}}, opts ...Options) map[string][]interface{} { if len(s.{{.Name}}) != len(t.{{.Name}}) { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } else { - diff2 := make(map[string][]interface{}) + diff2 := make(map[string][]interface{}) for i := range s.{{.Name}} { diffSub := s.{{.Name}}[i].Diff({{if HasPrefix .Type "[]*"}}*{{end}}t.{{.Name}}[i], opt) if len(diffSub) > 0 { @@ -188,20 +187,25 @@ func (s {{.Name}}) Diff(t {{.Name}}, opts ...Options) map[string][]interface{} { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } {{- else if (HasPrefix .Type "Date")}} - if !s.{{.Name}}.Equal(t.{{.Name}}) { - diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} - } - {{- else if or (HasPrefix .Type "*") (not .IsBasicType)}} - if !s.{{.Name}}.Equal({{if HasPrefix .Type "*"}}*{{end}}t.{{.Name}}, opt) { + if !s.{{.Name}}.Equal(t.{{.Name}}) { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } + {{- else if or (HasPrefix .Type "*") (not .IsBasicType)}} + {{- if or (eq .TypeInFile "time.Time") (eq .TypeInFile "strfmt.DateTime") }} {{/* time.Time has Equal, but nothing else */}} + if !s.{{.Name}}.Equal({{if HasPrefix .Type "*"}}*{{end}}t.{{.Name}}) { + diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} + } + {{- else}} + if !s.{{.Name}}.Equal({{if HasPrefix .Type "*"}}*{{end}}t.{{.Name}}, opt) { + diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} + } + {{- end}} {{- else}} if !s.{{.Name}}.Equal(&t.{{.Name}}, opt) { diff["{{.Name}}"] = []interface{}{s.{{.Name}}, t.{{.Name}}} } {{end}} {{end}} - {{- if eq .Mode "array" }} if !opt.NilSameAsEmpty { if s == nil && t != nil { @@ -213,11 +217,11 @@ func (s {{.Name}}) Diff(t {{.Name}}, opts ...Options) map[string][]interface{} { return diff } } - if len(s) != len(t) { + if len(s) != len(t) { diff["{{.Name}}"] = []interface{}{s, t} return diff } - for i, v := range s { + for i, v := range s { {{- if .IsBasicType}} {{- if HasPrefix .Type "*"}} if *v != *t[i] { @@ -236,7 +240,7 @@ func (s {{.Name}}) Diff(t {{.Name}}, opts ...Options) map[string][]interface{} { } {{- end -}} {{- if eq .Mode "ident" }} - if s != t { + if s != t { diff["{{.Name}}"] = []interface{}{s, t} } {{- end}} diff --git a/cmd/struct_equal_generator/test.tmpl b/cmd/struct_equal_generator/test.tmpl index 3d311dce..402ded57 100644 --- a/cmd/struct_equal_generator/test.tmpl +++ b/cmd/struct_equal_generator/test.tmpl @@ -1,7 +1,7 @@ {{ range .TestType}} func Test{{$.Name}}{{.}}(t *testing.T) { - faker := gofakeit.NewCrypto() + faker := gofakeit.NewCrypto() gofakeit.SetGlobalFaker(faker) samples := []struct { a, b {{$.Name}} @@ -13,7 +13,7 @@ func Test{{$.Name}}{{.}}(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - buf := new(bytes.Buffer) + buf := new(bytes.Buffer) enc := gob.NewEncoder(buf) err = enc.Encode(&sample) if err != nil { @@ -21,9 +21,9 @@ func Test{{$.Name}}{{.}}(t *testing.T) { } dec := gob.NewDecoder(buf) err = dec.Decode(&result) - if err != nil { - t.Errorf(err.Error()) - } + if err != nil { + t.Errorf(err.Error()) + } samples = append(samples, struct { a, b {{$.Name}} @@ -32,12 +32,12 @@ func Test{{$.Name}}{{.}}(t *testing.T) { for _, sample := range samples { result := sample.a.{{.}}(sample.b) - {{- if eq . "Equal" }} + {{- if eq . "Equal" }} if !result { - {{end -}} - {{- if eq . "Diff" }} + {{end -}} + {{- if eq . "Diff" }} if len(result) != 0 { - {{end -}} + {{end -}} json := jsoniter.ConfigCompatibleWithStandardLibrary a, err := json.Marshal(&sample.a) if err != nil { @@ -47,12 +47,12 @@ func Test{{$.Name}}{{.}}(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - {{- if eq . "Equal" }} - t.Errorf("Expected {{$.Name}} to be equal, but it is not %s %s", a, b) - {{end -}} - {{- if eq . "Diff" }} - t.Errorf("Expected {{$.Name}} to be equal, but it is not %s %s, %v", a, b, result) - {{end -}} + {{- if eq . "Equal" }} + t.Errorf("Expected {{$.Name}} to be equal, but it is not %s %s", a, b) + {{end -}} + {{- if eq . "Diff" }} + t.Errorf("Expected {{$.Name}} to be equal, but it is not %s %s, %v", a, b, result) + {{end -}} } } } @@ -61,7 +61,7 @@ func Test{{$.Name}}{{.}}(t *testing.T) { /* {{end -}} func Test{{$.Name}}{{.}}False(t *testing.T) { - faker := gofakeit.NewCrypto() + faker := gofakeit.NewCrypto() gofakeit.SetGlobalFaker(faker) samples := []struct { a, b {{$.Name}} @@ -77,28 +77,35 @@ func Test{{$.Name}}{{.}}False(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - {{- range $.Fields}} - {{- if (eq .Type "bool") }} - result.{{.Name}} = !sample.{{.Name}} - {{- end}} - {{- if (eq .Type "*bool") }} - result.{{.Name}} = Ptr(!*sample.{{.Name}}) - {{- end}} - {{- if (HasPrefix .Type "int") }} - result.{{.Name}} = sample.{{.Name}}+1 - {{- end}} - {{- if (HasPrefix .Type "*int") }} - result.{{.Name}} = Ptr(*sample.{{.Name}}+1) - {{- end}} - {{- if or (HasPrefix .Type "DateTime") (HasPrefix .Type "Date") }} + {{- range $.Fields}} + {{- if (eq .Type "bool") }} + result.{{.Name}} = !sample.{{.Name}} + {{- end}} + {{- if (eq .Type "*bool") }} + result.{{.Name}} = Ptr(!*sample.{{.Name}}) + {{- end}} + {{- if (HasPrefix .Type "int") }} + result.{{.Name}} = sample.{{.Name}}+1 + {{- end}} + {{- if (HasPrefix .Type "*int") }} + result.{{.Name}} = Ptr(*sample.{{.Name}}+1) + {{- end}} + {{- if or (HasPrefix .Type "DateTime") (HasPrefix .Type "Date") }} {{- if ne .TypeInFile "" }} - result.{{.Name}} = {{.TypeInFile}}(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) + result.{{.Name}} = {{.TypeInFile}}(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) {{- else}} result.{{.Name}} = time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)) - {{- end}} - {{- end}} + {{- end}} + {{- end}} + {{- if or (HasPrefix .Type "*DateTime") (HasPrefix .Type "*Date") }} + {{- if ne .TypeInFile "" }} + result.{{.Name}} = Ptr({{.TypeInFile}}(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)))) + {{- else}} + result.{{.Name}} = Ptr(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) + {{- end}} + {{- end}} {{- /*//{{.Name}} => {{.Type}}*/}} - {{- end}} + {{- end}} samples = append(samples, struct { a, b {{$.Name}} }{sample, result}) @@ -106,20 +113,20 @@ func Test{{$.Name}}{{.}}False(t *testing.T) { for _, sample := range samples { result := sample.a.{{.}}(sample.b) - {{- if eq . "Equal" }} + {{- if eq . "Equal" }} if result { - {{end -}} - {{- if eq . "Diff" }} - {{- if eq $.FieldCount 0 }} - if len(result) == 0 { - {{- else }} - {{- if $.HasIndex }} - if len(result) != {{$.FieldCount}} -1 { - {{- else }} - if len(result) != {{$.FieldCount}} { - {{end -}} - {{end -}} - {{end -}} + {{end -}} + {{- if eq . "Diff" }} + {{- if eq $.FieldCount 0 }} + if len(result) == 0 { + {{- else }} + {{- if $.HasIndex }} + if len(result) != {{$.FieldCount}} -1 { + {{- else }} + if len(result) != {{$.FieldCount}} { + {{end -}} + {{end -}} + {{end -}} json := jsoniter.ConfigCompatibleWithStandardLibrary a, err := json.Marshal(&sample.a) if err != nil { @@ -129,15 +136,16 @@ func Test{{$.Name}}{{.}}False(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - {{- if eq . "Equal" }} - t.Errorf("Expected {{$.Name}} to be different, but it is not %s %s", a, b) - {{end -}} - {{- if eq . "Diff" }} - t.Errorf("Expected {{$.Name}} to be different in {{$.FieldCount}} cases, but it is not (%d) %s %s", len(result), a, b) - {{end -}} + {{- if eq . "Equal" }} + t.Errorf("Expected {{$.Name}} to be different, but it is not %s %s", a, b) + {{end -}} + {{- if eq . "Diff" }} + t.Errorf("Expected {{$.Name}} to be different in {{$.FieldCount}} cases, but it is not (%d) %s %s", len(result), a, b) + {{end -}} } } } + {{if eq . "Diffs" -}} */ {{end}} diff --git a/models/general_file_compare.go b/models/general_file_compare.go index 9de304eb..8aee150c 100644 --- a/models/general_file_compare.go +++ b/models/general_file_compare.go @@ -36,7 +36,7 @@ func (s GeneralFile) Equal(t GeneralFile, opts ...Options) bool { return false } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { return false } @@ -67,7 +67,7 @@ func (s GeneralFile) Diff(t GeneralFile, opts ...Options) map[string][]interface diff["ID"] = []interface{}{s.ID, t.ID} } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { diff["Size"] = []interface{}{s.Size, t.Size} } diff --git a/models/general_file_compare_test.go b/models/general_file_compare_test.go index 3642bea6..ab7aef7a 100644 --- a/models/general_file_compare_test.go +++ b/models/general_file_compare_test.go @@ -91,7 +91,7 @@ func TestGeneralFileEqualFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.Size = sample.Size + 1 + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b GeneralFile }{sample, result}) @@ -178,7 +178,7 @@ func TestGeneralFileDiffFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.Size = sample.Size + 1 + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b GeneralFile }{sample, result}) diff --git a/models/map_compare.go b/models/map_compare.go index 39b9eba1..a33891be 100644 --- a/models/map_compare.go +++ b/models/map_compare.go @@ -36,7 +36,7 @@ func (s Map) Equal(t Map, opts ...Options) bool { return false } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { return false } @@ -67,7 +67,7 @@ func (s Map) Diff(t Map, opts ...Options) map[string][]interface{} { diff["ID"] = []interface{}{s.ID, t.ID} } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { diff["Size"] = []interface{}{s.Size, t.Size} } diff --git a/models/map_compare_test.go b/models/map_compare_test.go index 626b91c0..2b63fa12 100644 --- a/models/map_compare_test.go +++ b/models/map_compare_test.go @@ -91,7 +91,7 @@ func TestMapEqualFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.Size = sample.Size + 1 + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b Map }{sample, result}) @@ -178,7 +178,7 @@ func TestMapDiffFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.Size = sample.Size + 1 + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b Map }{sample, result}) diff --git a/models/ssl_certificate_compare.go b/models/ssl_certificate_compare.go index b58ab5a5..92eadb66 100644 --- a/models/ssl_certificate_compare.go +++ b/models/ssl_certificate_compare.go @@ -19,25 +19,16 @@ package models // Equal checks if two structs of type SslCertificate are equal // -// By default empty maps and slices are equal to nil: -// // var a, b SslCertificate // equal := a.Equal(b) // -// For more advanced use case you can configure these options (default values are shown): -// -// var a, b SslCertificate -// equal := a.Equal(b,Options{ -// NilSameAsEmpty: true, -// }) +// opts ...Options are ignored in this method func (s SslCertificate) Equal(t SslCertificate, opts ...Options) bool { - opt := getOptions(opts...) - if s.Description != t.Description { return false } - if !equalComparableSlice(s.Domains, t.Domains, opt) { + if s.Domains != t.Domains { return false } @@ -45,23 +36,23 @@ func (s SslCertificate) Equal(t SslCertificate, opts ...Options) bool { return false } - if !equalComparableSlice(s.IPAddresses, t.IPAddresses, opt) { + if s.IPAddresses != t.IPAddresses { return false } - if !equalComparableSlice(s.Issuers, t.Issuers, opt) { + if s.Issuers != t.Issuers { return false } - if !s.NotAfter.Equal(t.NotAfter) { + if !s.NotAfter.Equal(*t.NotAfter) { return false } - if !s.NotBefore.Equal(t.NotBefore) { + if !s.NotBefore.Equal(*t.NotBefore) { return false } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { return false } @@ -74,26 +65,17 @@ func (s SslCertificate) Equal(t SslCertificate, opts ...Options) bool { // Diff checks if two structs of type SslCertificate are equal // -// By default empty arrays, maps and slices are equal to nil: -// // var a, b SslCertificate // diff := a.Diff(b) // -// For more advanced use case you can configure the options (default values are shown): -// -// var a, b SslCertificate -// equal := a.Diff(b,Options{ -// NilSameAsEmpty: true, -// }) +// opts ...Options are ignored in this method func (s SslCertificate) Diff(t SslCertificate, opts ...Options) map[string][]interface{} { - opt := getOptions(opts...) - diff := make(map[string][]interface{}) if s.Description != t.Description { diff["Description"] = []interface{}{s.Description, t.Description} } - if !equalComparableSlice(s.Domains, t.Domains, opt) { + if s.Domains != t.Domains { diff["Domains"] = []interface{}{s.Domains, t.Domains} } @@ -101,23 +83,23 @@ func (s SslCertificate) Diff(t SslCertificate, opts ...Options) map[string][]int diff["File"] = []interface{}{s.File, t.File} } - if !equalComparableSlice(s.IPAddresses, t.IPAddresses, opt) { + if s.IPAddresses != t.IPAddresses { diff["IPAddresses"] = []interface{}{s.IPAddresses, t.IPAddresses} } - if !equalComparableSlice(s.Issuers, t.Issuers, opt) { + if s.Issuers != t.Issuers { diff["Issuers"] = []interface{}{s.Issuers, t.Issuers} } - if !s.NotAfter.Equal(t.NotAfter) { + if !s.NotAfter.Equal(*t.NotAfter) { diff["NotAfter"] = []interface{}{s.NotAfter, t.NotAfter} } - if !s.NotBefore.Equal(t.NotBefore) { + if !s.NotBefore.Equal(*t.NotBefore) { diff["NotBefore"] = []interface{}{s.NotBefore, t.NotBefore} } - if s.Size != t.Size { + if !equalPointers(s.Size, t.Size) { diff["Size"] = []interface{}{s.Size, t.Size} } diff --git a/models/ssl_certificate_compare_test.go b/models/ssl_certificate_compare_test.go index 218f24c5..1ccc8b60 100644 --- a/models/ssl_certificate_compare_test.go +++ b/models/ssl_certificate_compare_test.go @@ -94,9 +94,9 @@ func TestSslCertificateEqualFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.NotAfter = strfmt.Date(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) - result.NotBefore = strfmt.Date(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) - result.Size = sample.Size + 1 + result.NotAfter = Ptr(strfmt.DateTime(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)))) + result.NotBefore = Ptr(strfmt.DateTime(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)))) + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b SslCertificate }{sample, result}) @@ -183,9 +183,9 @@ func TestSslCertificateDiffFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - result.NotAfter = strfmt.Date(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) - result.NotBefore = strfmt.Date(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28))) - result.Size = sample.Size + 1 + result.NotAfter = Ptr(strfmt.DateTime(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)))) + result.NotBefore = Ptr(strfmt.DateTime(time.Now().AddDate(rand.Intn(10), rand.Intn(12), rand.Intn(28)))) + result.Size = Ptr(*sample.Size + 1) samples = append(samples, struct { a, b SslCertificate }{sample, result})