Skip to content

Commit

Permalink
add support slice primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
cjp2600 committed Apr 19, 2019
1 parent 4364745 commit 41819b2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1,254 deletions.
73 changes: 41 additions & 32 deletions plugin/options/bom.pb.go

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

1 change: 1 addition & 0 deletions plugin/options/bom.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ message BomTag {
optional bool skip = 4;
optional bool mongoObjectId = 5;
optional bool update = 6;
optional bool additional = 7;
}

// To be used in (leiu of) the interceptor
Expand Down
45 changes: 37 additions & 8 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,19 @@ func (p *MongoPlugin) generateModelsStructures(message *descriptor.DescriptorPro
}

if bomField != nil && bomField.Tag.GetMongoObjectId() {
idName := ""
if bomField.Tag.GetIsID() {
idName = "`_id, omitempty`"

repeated := field.IsRepeated()
if repeated {
p.P(fieldName, ` `, `[]primitive.ObjectID`)
} else {
idName := ""
if bomField.Tag.GetIsID() {
idName = "`_id, omitempty`"
}
p.P(fieldName, ` `, `primitive.ObjectID`, idName)
}
p.P(fieldName, ` `, `primitive.ObjectID`, idName)
p.usePrimitive = true

} else if p.IsMap(field) {
m := p.GoMapType(nil, field)
//_, keyField, keyAliasField := m.GoType, m.KeyField, m.KeyAliasField
Expand Down Expand Up @@ -369,7 +376,22 @@ func (p *MongoPlugin) GenerateFieldConversion(field *descriptor.FieldDescriptorP

} else {
if bomField != nil && bomField.Tag.GetMongoObjectId() {
p.P(`resp.`, fieldName, ` = e.`, fieldName, `.Hex()`)

repeated := field.IsRepeated()
if repeated {

p.P(`if len(e.`, fieldName, `) > 0 {`)
p.P(`var sub`, fieldName, goTyp)
p.P(`for _, b := range `, `e.`, fieldName, `{`)
p.P(`sub`, fieldName, ` = append(sub`, fieldName, `, b.Hex())`)
p.P(`}`)
p.P(`resp.`, fieldName, ` = sub`, fieldName)
p.P(`}`)

} else {
p.P(`resp.`, fieldName, ` = e.`, fieldName, `.Hex()`)
}

} else {
p.P(`resp.`, fieldName, ` = e.`, fieldName)
}
Expand Down Expand Up @@ -444,9 +466,16 @@ func (p *MongoPlugin) ToMongoGenerateFieldConversion(field *descriptor.FieldDesc

} else if bomField != nil && bomField.Tag.GetMongoObjectId() {

p.P(`if len(e.`, fieldName, `) > 0 {`)
p.P(`resp.`, fieldName, ` = bom.ToObj(e.`, fieldName, `)`)
p.P(`}`)
repeated := field.IsRepeated()
if repeated {
p.P(`if len(e.`, fieldName, `) > 0 {`)
p.P(`resp.`, fieldName, ` = bom.ToObjects(e.`, fieldName, `)`)
p.P(`}`)
} else {
p.P(`if len(e.`, fieldName, `) > 0 {`)
p.P(`resp.`, fieldName, ` = bom.ToObj(e.`, fieldName, `)`)
p.P(`}`)
}

} else {
p.P(`resp.`, fieldName, ` = e.`, fieldName)
Expand Down
Loading

0 comments on commit 41819b2

Please sign in to comment.