Skip to content

Commit

Permalink
Merge pull request #217 from AnPiakhota-Infoblox/fix-gen-code-issues
Browse files Browse the repository at this point in the history
Fix nil pointer dereference in gen code
  • Loading branch information
AnPiakhota-Infoblox authored Sep 17, 2021
2 parents 200aa50 + 6bb646c commit 51b6c18
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions example/feature_demo/demo_types.pb.gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (m *TestTypes) ToORM(ctx context.Context) (TestTypesORM, error) {
to.Uuid = go_uuid.Nil
}
if m.CreatedAt != nil {
*to.CreatedAt = m.CreatedAt.AsTime()
t := m.CreatedAt.AsTime()
to.CreatedAt = &t
}
to.TypeWithIdId = m.TypeWithIdId
if m.JsonField != nil {
Expand Down Expand Up @@ -240,7 +241,8 @@ func (m *TypeWithID) ToORM(ctx context.Context) (TypeWithIDORM, error) {
}
}
if m.DeletedAt != nil {
*to.DeletedAt = m.DeletedAt.AsTime()
t := m.DeletedAt.AsTime()
to.DeletedAt = &t
}
if posthook, ok := interface{}(m).(TypeWithIDWithAfterToORM); ok {
err = posthook.AfterToORM(ctx, &to)
Expand Down
15 changes: 10 additions & 5 deletions example/user/user.pb.gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ func (m *User) ToORM(ctx context.Context) (UserORM, error) {
to.Id = v.(string)
}
if m.CreatedAt != nil {
*to.CreatedAt = m.CreatedAt.AsTime()
t := m.CreatedAt.AsTime()
to.CreatedAt = &t
}
if m.UpdatedAt != nil {
*to.UpdatedAt = m.UpdatedAt.AsTime()
t := m.UpdatedAt.AsTime()
to.UpdatedAt = &t
}
if m.Birthday != nil {
*to.Birthday = m.Birthday.AsTime()
t := m.Birthday.AsTime()
to.Birthday = &t
}
to.Num = m.Num
if m.CreditCard != nil {
Expand Down Expand Up @@ -651,10 +654,12 @@ func (m *CreditCard) ToORM(ctx context.Context) (CreditCardORM, error) {
to.Id = v
}
if m.CreatedAt != nil {
*to.CreatedAt = m.CreatedAt.AsTime()
t := m.CreatedAt.AsTime()
to.CreatedAt = &t
}
if m.UpdatedAt != nil {
*to.UpdatedAt = m.UpdatedAt.AsTime()
t := m.UpdatedAt.AsTime()
to.UpdatedAt = &t
}
to.Number = m.Number
if m.UserId != nil {
Expand Down
3 changes: 2 additions & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,8 @@ func (b *ORMBuilder) generateFieldConversion(message *protogen.Message, field *p
} else if fieldType == protoTypeTimestamp { // Singular WKT Timestamp ---
if toORM {
g.P(`if m.`, fieldName, ` != nil {`)
g.P(`*to.`, fieldName, ` = m.`, fieldName, `.AsTime()`)
g.P(`t := m.`, fieldName, `.AsTime()`)
g.P(`to.`, fieldName, ` = &t`)
g.P(`}`)
} else {
g.P(`if m.`, fieldName, ` != nil {`)
Expand Down

0 comments on commit 51b6c18

Please sign in to comment.