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

feat: add bytes support #254

Merged
merged 1 commit into from
Nov 22, 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
156 changes: 83 additions & 73 deletions example/feature_demo/demo_types.pb.go

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

7 changes: 7 additions & 0 deletions example/feature_demo/demo_types.pb.gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ type TypeWithIDORM struct {
Id uint32
IntPointId *uint32
Ip string `gorm:"column:ip_addr"`
Metadata []byte `gorm:"type:bytea"`
MultiAccountTypes []*JoinTable `gorm:"foreignKey:TypeWithIDID"`
Point *IntPointORM `gorm:"foreignKey:IntPointId;references:Id"`
SecretInt int32 `gorm:"-"`
Expand Down Expand Up @@ -265,6 +266,7 @@ func (m *TypeWithID) ToORM(ctx context.Context) (TypeWithIDORM, error) {
t := m.DeletedAt.AsTime()
to.DeletedAt = &t
}
to.Metadata = m.Metadata
if posthook, ok := interface{}(m).(TypeWithIDWithAfterToORM); ok {
err = posthook.AfterToORM(ctx, &to)
}
Expand Down Expand Up @@ -334,6 +336,7 @@ func (m *TypeWithIDORM) ToPB(ctx context.Context) (TypeWithID, error) {
if m.DeletedAt != nil {
to.DeletedAt = timestamppb.New(*m.DeletedAt)
}
to.Metadata = m.Metadata
if posthook, ok := interface{}(m).(TypeWithIDWithAfterToPB); ok {
err = posthook.AfterToPB(ctx, &to)
}
Expand Down Expand Up @@ -1995,6 +1998,10 @@ func DefaultApplyFieldMaskTypeWithID(ctx context.Context, patchee *TypeWithID, p
patchee.DeletedAt = patcher.DeletedAt
continue
}
if f == prefix+"Metadata" {
patchee.Metadata = patcher.Metadata
continue
}
}
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions example/feature_demo/demo_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ message TypeWithID {
// Limited support for DB type 'time', implemented via strings (string -> DB && DB -> string)
gorm.types.TimeOnly time_only = 14;
google.protobuf.Timestamp deleted_at = 15;
bytes metadata = 16;
}

// MultiaccountTypeWithID demonstrates the generated multi-account support
Expand Down
4 changes: 3 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ func (b *ORMBuilder) parseBasicFields(msg *protogen.Message, g *protogen.Generat
tag := gormOptions.Tag
fieldName := camelCase(string(fd.Name()))
fieldType := fd.Kind().String()

var typePackage string

if b.dbEngine == ENGINE_POSTGRES && b.IsAbleToMakePQArray(fieldType) && field.Desc.IsList() {
Expand Down Expand Up @@ -994,6 +993,9 @@ func (b *ORMBuilder) parseBasicFields(msg *protogen.Message, g *protogen.Generat
}

switch fieldType {
case "bytes":
fieldType = "[]byte"
gormOptions.Tag = tagWithType(tag, "bytea")
case "float":
fieldType = "float32"
case "double":
Expand Down
Loading