Skip to content

Commit

Permalink
MINOR: struct_equal_generator: add support for strfmt.DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer committed Aug 24, 2023
1 parent 647b829 commit 3a7f2e3
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 131 deletions.
68 changes: 36 additions & 32 deletions cmd/struct_equal_generator/generate.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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] {
Expand All @@ -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 }}
Expand All @@ -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}}}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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] {
Expand All @@ -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}}
Expand Down
114 changes: 61 additions & 53 deletions cmd/struct_equal_generator/test.tmpl
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -13,17 +13,17 @@ 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 {
t.Errorf(err.Error())
}
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}}
Expand All @@ -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 {
Expand All @@ -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 -}}
}
}
}
Expand All @@ -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}}
Expand All @@ -77,49 +77,56 @@ 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})
}

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 {
Expand All @@ -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}}
Expand Down
4 changes: 2 additions & 2 deletions models/general_file_compare.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions models/general_file_compare_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3a7f2e3

Please sign in to comment.