diff --git a/plugin/plugin.go b/plugin/plugin.go index 9829339..c00ce01 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -55,7 +55,6 @@ func (p *MongoPlugin) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) p.localName = generator.FileName(file) p.usePrimitive = false - for _, msg := range file.GetMessageType() { if bomMessage, ok := p.getMessageOptions(msg); ok { if bomMessage.GetModel() { @@ -65,6 +64,7 @@ func (p *MongoPlugin) Generate(file *generator.FileDescriptor) { p.GenerateToPB(msg) p.GenerateToObject(msg) p.GenerateBomConnect(msg) + p.GenerateInsertMethod(msg) } } } @@ -102,6 +102,47 @@ func (p *MongoPlugin) getFieldOptions(field *descriptor.FieldDescriptorProto) *b return opts } +//GenerateInsertMethod +func (p *MongoPlugin) GenerateInsertMethod(message *descriptor.DescriptorProto) { + //typeName := p.GenerateName(message.GetName()) + mName := p.GenerateName(message.GetName()) + p.usePrimitive = true + useId := false + + p.P(`func (e *`, mName, `) Insert(bom *bom.Bom) (*`, mName, `, error) {`) + + for _, field := range message.GetField() { + fieldName := field.GetName() + fieldName = generator.CamelCase(fieldName) + + bomField := p.getFieldOptions(field) + if bomField != nil && bomField.Tag.GetMongoObjectId() { + if bomField.GetTag().GetIsID() { + useId = true + p.P(`e.`, fieldName, ` = primitive.NewObjectID() // create object id`) + } + } + } + + if useId { + p.P(`res, err := e.WithBom(bom).InsertOne(e)`) + } else { + p.P(`_, err := e.WithBom(bom).InsertOne(e)`) + } + p.P(`if err != nil {`) + p.P(`return nil, err`) + p.P(`}`) + + if useId { + p.P(`if insertId, ok := res.InsertedID.(primitive.ObjectID); ok {`) + p.P(`e.Id = insertId`) + p.P(`}`) + } + + p.P(`return e, nil`) + p.P(`}`) +} + //GenerateBehaviorInterface func (p *MongoPlugin) GenerateBehaviorInterface(message *descriptor.DescriptorProto) { p.In() diff --git a/test.pb.bom.go b/test.pb.bom.go index 86923fa..bb65a52 100644 --- a/test.pb.bom.go +++ b/test.pb.bom.go @@ -90,7 +90,9 @@ func (e *Role) ToMongo() (*RoleMongo, error) { return &resp, err } } - resp.Id = bom.ToObj(e.Id) + if len(e.Id) > 0 { + resp.Id = bom.ToObj(e.Id) + } resp.Name = e.Name // create nested mongo var subRole []*PermissionMongo @@ -117,6 +119,17 @@ func (e *Role) ToMongo() (*RoleMongo, error) { func (e *RoleMongo) WithBom(b *bom.Bom) *bom.Bom { return b.WithColl("role") } +func (e *RoleMongo) Insert(bom *bom.Bom) (*RoleMongo, error) { + e.Id = primitive.NewObjectID() // create object id + res, err := e.WithBom(bom).InsertOne(e) + if err != nil { + return nil, err + } + if insertId, ok := res.InsertedID.(primitive.ObjectID); ok { + e.Id = insertId + } + return e, nil +} // create MongoDB Model from protobuf (PermissionMongo) type PermissionMongo struct { @@ -180,7 +193,9 @@ func (e *Permission) ToMongo() (*PermissionMongo, error) { return &resp, err } } - resp.Id = bom.ToObj(e.Id) + if len(e.Id) > 0 { + resp.Id = bom.ToObj(e.Id) + } resp.Service = e.Service resp.Create = e.Create resp.Read = e.Read @@ -197,6 +212,17 @@ func (e *Permission) ToMongo() (*PermissionMongo, error) { func (e *PermissionMongo) WithBom(b *bom.Bom) *bom.Bom { return b.WithColl("permission") } +func (e *PermissionMongo) Insert(bom *bom.Bom) (*PermissionMongo, error) { + e.Id = primitive.NewObjectID() // create object id + res, err := e.WithBom(bom).InsertOne(e) + if err != nil { + return nil, err + } + if insertId, ok := res.InsertedID.(primitive.ObjectID); ok { + e.Id = insertId + } + return e, nil +} // create MongoDB Model from protobuf (UserMongo) type UserMongo struct { @@ -284,7 +310,9 @@ func (e *User) ToMongo() (*UserMongo, error) { return &resp, err } } - resp.Id = bom.ToObj(e.Id) + if len(e.Id) > 0 { + resp.Id = bom.ToObj(e.Id) + } resp.Active = e.Active resp.FirstName = e.FirstName resp.LastName = e.LastName @@ -320,6 +348,17 @@ func (e *User) ToMongo() (*UserMongo, error) { func (e *UserMongo) WithBom(b *bom.Bom) *bom.Bom { return b.WithColl("user") } +func (e *UserMongo) Insert(bom *bom.Bom) (*UserMongo, error) { + e.Id = primitive.NewObjectID() // create object id + res, err := e.WithBom(bom).InsertOne(e) + if err != nil { + return nil, err + } + if insertId, ok := res.InsertedID.(primitive.ObjectID); ok { + e.Id = insertId + } + return e, nil +} // create MongoDB Model from protobuf (TokenMongo) type TokenMongo struct { @@ -388,6 +427,13 @@ func (e *Token) ToMongo() (*TokenMongo, error) { func (e *TokenMongo) WithBom(b *bom.Bom) *bom.Bom { return b.WithColl("token") } +func (e *TokenMongo) Insert(bom *bom.Bom) (*TokenMongo, error) { + _, err := e.WithBom(bom).InsertOne(e) + if err != nil { + return nil, err + } + return e, nil +} // create MongoDB Model from protobuf (ProviderUsersMongo) type ProviderUsersMongo struct { @@ -443,8 +489,12 @@ func (e *ProviderUsers) ToMongo() (*ProviderUsersMongo, error) { return &resp, err } } - resp.ProviderId = bom.ToObj(e.ProviderId) - resp.UserId = bom.ToObj(e.UserId) + if len(e.ProviderId) > 0 { + resp.ProviderId = bom.ToObj(e.ProviderId) + } + if len(e.UserId) > 0 { + resp.UserId = bom.ToObj(e.UserId) + } if posthook, ok := interface{}(e).(ProviderUsersMongoWithAfterToMongo); ok { if err := posthook.AfterToMongo(&resp); err != nil { return &resp, err @@ -456,3 +506,10 @@ func (e *ProviderUsers) ToMongo() (*ProviderUsersMongo, error) { func (e *ProviderUsersMongo) WithBom(b *bom.Bom) *bom.Bom { return b.WithColl("providerusers") } +func (e *ProviderUsersMongo) Insert(bom *bom.Bom) (*ProviderUsersMongo, error) { + _, err := e.WithBom(bom).InsertOne(e) + if err != nil { + return nil, err + } + return e, nil +}