Skip to content

Commit

Permalink
feat: add bytes support (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmayb authored Nov 22, 2023
1 parent 76a99c1 commit d3024d4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 74 deletions.
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

0 comments on commit d3024d4

Please sign in to comment.