Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only import lo and timestamppb if the timestamp field is a messa… #10

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions example/demo/example.pb.gorm.go

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

4 changes: 2 additions & 2 deletions plugin/gorm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func (m *{{ .Model.Name }}) ToProto() (theProto *{{.GoIdent.GoName}}, err error)
if len(m.{{ .GoName }}) > 0 {
theProto.{{ .GoName }} = []{{ .Enum.GoIdent.GoName }}{}
for _, val := range m.{{ .GoName }} {
theProto.{{ .GoName }} = append(theProto.{{ .GoName }}, EnumOne(EnumOne_value[val]))
theProto.{{ .GoName }} = append(theProto.{{ .GoName }}, {{ .Enum.GoIdent.GoName }}({{ .Enum.GoIdent.GoName }}_value[val]))
}
}
{{ else }}
if len(m.{{ .GoName }}) > 0 {
theProto.{{ .GoName }} = []{{ .Enum.GoIdent.GoName }}{}
for _, val := range m.{{ .GoName }} {
theProto.{{ .GoName }} = append(theProto.{{ .GoName }}, EnumOne(val))
theProto.{{ .GoName }} = append(theProto.{{ .GoName }}, {{ .Enum.GoIdent.GoName }}(val))
}
}
{{ end }}
Expand Down
9 changes: 4 additions & 5 deletions plugin/model_field.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"fmt"
gorm "github.com/catalystsquad/protoc-gen-go-gorm/options"
"google.golang.org/protobuf/compiler/protogen"
"strings"
Expand Down Expand Up @@ -53,9 +52,7 @@ func shouldGenerateBelongsToIdField(f *ModelField) bool {
}
// there are belongs to options, loop through the fields and make sure there isn't one already
fieldName := options.GetBelongsTo().Foreignkey
g.P(fmt.Sprintf("// fieldName: %s", fieldName))
for _, field := range f.Parent.Fields {
g.P(fmt.Sprintf("// field.GoName: %s fieldName: %s", field.GoName, fieldName))
if field.GoName == fieldName {
// field is already on the message, don't generate a duplicate
return false
Expand All @@ -81,8 +78,10 @@ func isStructPb(field *protogen.Field) bool {
func getModelFieldType(field *ModelField) string {
if field.IsTimestamp {
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "time"})
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/samber/lo"})
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "google.golang.org/protobuf/types/known/timestamppb"})
if field.IsMessage {
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/samber/lo"})
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "google.golang.org/protobuf/types/known/timestamppb"})
}
return "*time.Time"
} else if field.IsStructPb {
g.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/jackc/pgtype"})
Expand Down