Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elemental model changes to use mongo-go-driver's bson types instead of mgo #135

Merged
merged 10 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading