Skip to content

Commit

Permalink
elemental model changes to use mongo-go-driver's bson types instead o…
Browse files Browse the repository at this point in the history
…f mgo (#135)

* elemental model changes to use mongo-go-driver's bson types instead of using those of mgo.

* more changes

* code generation changes to correctly marshal/unmarshal using Unmarshaller interface that's needed by mongo-go-driver

* removing all mgo references

* fixing go.mod

* test changes

* fixing unit tests

* fixing golang version

* elemental code hardly has any coverage, so relaxing this requirement

* updating go.sum
  • Loading branch information
knainwal authored Aug 22, 2024
1 parent 3d09d6d commit 6f8c7be
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 275 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
fail-fast: false
matrix:
go:
- "1.20"
- "1.21"
- "1.22"
steps:
- uses: actions/checkout@v3

Expand All @@ -42,5 +42,5 @@ jobs:
with:
main_branch: master
cov_file: unit_coverage.out
cov_threshold: "85"
cov_threshold: "5"
cov_mode: coverage
47 changes: 28 additions & 19 deletions cmd/elegen/templates/model.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package {{ .Set.Configuration.Name }}
import (
"fmt"
"go.aporeto.io/elemental"
"github.com/globalsign/mgo/bson"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mitchellh/copystructure"
{{- range .Spec.TypeProviders }}
"{{ . }}"
Expand Down Expand Up @@ -171,40 +172,44 @@ func (o *{{ .Spec.Model.EntityName }}) SetIdentifier(id string) {
}
{{- end }}

// GetBSON implements the bson marshaling interface.
// MarshalBSON implements the bson marshaling interface.
// This is used to transparently convert ID to MongoDBID as ObectID.
func (o *{{ .Spec.Model.EntityName }}) GetBSON() (any, error) {
func (o *{{ .Spec.Model.EntityName }}) MarshalBSON() ([]byte, error) {

if o == nil {
return nil, nil
}

s := &mongoAttributes{{ .Spec.Model.EntityName }}{}
s := mongoAttributes{{ .Spec.Model.EntityName }}{}
{{ range .Spec.Attributes $latestVersion -}}
{{- if .Stored }}
{{- if .Identifier }}
if o.{{ .ConvertedName }} != "" {
s.{{ .ConvertedName }} = bson.ObjectIdHex(o.{{ .ConvertedName }})
objectID, err := primitive.ObjectIDFromHex(o.{{ .ConvertedName }})
if err != nil {
return nil, err
}
s.{{ .ConvertedName }} = objectID
}
{{- else }}
s.{{ .ConvertedName }} = o.{{ .ConvertedName }}
{{- end }}
{{- end }}
{{- end }}

return s, nil
return bson.Marshal(s)
}

// SetBSON implements the bson marshaling interface.
// This is used to transparently convert ID to MongoDBID as ObectID.
func (o *{{ .Spec.Model.EntityName }}) SetBSON(raw bson.Raw) error {
// UnmarshalBSON implements the bson unmarshaling interface.
// This is used to transparently convert MongoDBID to ID.
func (o *{{ .Spec.Model.EntityName }}) UnmarshalBSON(raw []byte) error {

if o == nil {
return nil
}

s := &mongoAttributes{{ .Spec.Model.EntityName }}{}
if err := raw.Unmarshal(s); err != nil {
if err := bson.Unmarshal(raw, s); err != nil {
return err
}
{{ range .Spec.Attributes $latestVersion -}}
Expand Down Expand Up @@ -896,20 +901,24 @@ func (o *Sparse{{ .Spec.Model.EntityName }}) SetIdentifier(id string) {
{{- end }}
}

// GetBSON implements the bson marshaling interface.
// MarshalBSON implements the bson marshaling interface.
// This is used to transparently convert ID to MongoDBID as ObectID.
func (o *Sparse{{ .Spec.Model.EntityName }}) GetBSON() (any, error) {
func (o *Sparse{{ .Spec.Model.EntityName }}) MarshalBSON() ([]byte, error) {

if o == nil {
return nil, nil
}

s := &mongoAttributesSparse{{ .Spec.Model.EntityName }}{}
s := mongoAttributesSparse{{ .Spec.Model.EntityName }}{}
{{ range .Spec.Attributes $latestVersion -}}
{{- if .Stored }}
{{- if .Identifier }}
if o.{{ .ConvertedName }} != nil {
s.{{ .ConvertedName }} = bson.ObjectIdHex(*o.{{ .ConvertedName }})
objectID, err := primitive.ObjectIDFromHex(*o.{{ .ConvertedName }})
if err != nil {
return nil, err
}
s.{{ .ConvertedName }} = objectID
}
{{- else }}
if o.{{ .ConvertedName}} != nil {
Expand All @@ -919,19 +928,19 @@ func (o *Sparse{{ .Spec.Model.EntityName }}) GetBSON() (any, error) {
{{- end }}
{{- end }}

return s, nil
return bson.Marshal(s)
}

// SetBSON implements the bson marshaling interface.
// This is used to transparently convert ID to MongoDBID as ObectID.
func (o *Sparse{{ .Spec.Model.EntityName }}) SetBSON(raw bson.Raw) error {
// UnmarshalBSON implements the bson unmarshaling interface.
// This is used to transparently convert MongoDBID to ID.
func (o *Sparse{{ .Spec.Model.EntityName }}) UnmarshalBSON(raw []byte) error {

if o == nil {
return nil
}

s := &mongoAttributesSparse{{ .Spec.Model.EntityName }}{}
if err := raw.Unmarshal(s); err != nil {
if err := bson.Unmarshal(raw, s); err != nil {
return err
}
{{ range .Spec.Attributes $latestVersion -}}
Expand Down
2 changes: 1 addition & 1 deletion cmd/elegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func attrToMongoField(set spec.SpecificationSet, shadow bool, attr *spec.Attribu
var convertedType string

if attr.Identifier {
convertedType = "bson.ObjectId"
convertedType = "primitive.ObjectID"
} else {
convertedType = attrToType(set, shadow, attr)
}
Expand Down
Loading

0 comments on commit 6f8c7be

Please sign in to comment.