Skip to content

Commit

Permalink
Add TX (#56)
Browse files Browse the repository at this point in the history
* Add TX

* Update fixtures

* Remove mergeall flag

* Govendor deps

* Govendor

* Add more to the vendor

* Add more

* More deps

* More

* Remove vendor
  • Loading branch information
omzouai-visor authored Jan 23, 2019
1 parent 2427e0c commit a15118b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

.idea
.build

vendor/**/
22 changes: 22 additions & 0 deletions langs/go/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,35 @@ func (m *GoModeler) writeModel(model *firemodel.SchemaModel, sourceCoder firemod
g.If(jen.Err().Op("!=").Nil()).Block(jen.Return(jen.Nil(), jen.Err()))
g.Return(jen.Id("wrapper"), jen.Nil())
})

f.Func().Params(jen.Id("c").Id("*"+clientName)).Id(getCommandByPathName+"Tx").Params(jen.Id("ctx").Qual("context", "Context"), jen.Id("tx").Op("*").Qual("cloud.google.com/go/firestore", "Transaction"), jen.Id("path").String()).Params(
jen.Id("*"+wrapperName),
jen.Error()).
BlockFunc(func(g *jen.Group) {
g.Id("reference").Op(":=").Id("c").Dot("client").Dot("Client").Dot("Doc").Call(jen.Id("path"))
g.Id("snapshot").Op(",").Err().Op(":=").Id("tx").Dot("Get").Call(jen.Id("reference"))
g.If(jen.Err().Op("!=").Nil()).Block(jen.Return(jen.Nil(), jen.Err()))
g.Id("wrapper").Op(",").Err().Op(":=").Id(fromSnapshotName).Call(jen.Id("snapshot"))
g.If(jen.Err().Op("!=").Nil()).Block(jen.Return(jen.Nil(), jen.Err()))
g.Return(jen.Id("wrapper"), jen.Nil())
})

f.Func().Params(jen.Id("m").Id("*" + wrapperName)).Id("Set").Params(jen.Id("ctx").Qual("context", "Context")).Params(jen.Id("error")).BlockFunc(func(g *jen.Group) {
g.If(jen.Id("m.ref").Op("==").Nil()).BlockFunc(func(g *jen.Group) {
g.Return(jen.Qual("errors", "New").Call(jen.Lit("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")))
})
g.Id("_").Op(",").Err().Op(":=").Id("m").Dot("ref").Dot("Set").Call(jen.Id("ctx"), jen.Id("m").Dot("Data"))
g.Return(jen.Err())
})

f.Func().Params(jen.Id("m").Id("*"+wrapperName)).Id("SetTx").Params(jen.Id("ctx").Qual("context", "Context"), jen.Id("tx").Op("*").Qual("cloud.google.com/go/firestore", "Transaction")).Params(jen.Id("error")).BlockFunc(func(g *jen.Group) {
g.If(jen.Id("m.ref").Op("==").Nil()).BlockFunc(func(g *jen.Group) {
g.Return(jen.Qual("errors", "New").Call(jen.Lit("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")))
})
g.Err().Op(":=").Id("tx").Dot("Set").Call(jen.Id("m").Dot("ref"), jen.Id("m").Dot("Data"))
g.Return(jen.Err())
})

}

w, err := sourceCoder.NewFile(fmt.Sprint(strcase.ToSnake(model.Name), fileExtension))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,29 @@ func (c *clientTestModel) GetByPath(ctx context.Context, path string) (*TestMode
}
return wrapper, nil
}
func (c *clientTestModel) GetByPathTx(ctx context.Context, tx *firestore.Transaction, path string) (*TestModelWrapper, error) {
reference := c.client.Client.Doc(path)
snapshot, err := tx.Get(reference)
if err != nil {
return nil, err
}
wrapper, err := TestModelFromSnapshot(snapshot)
if err != nil {
return nil, err
}
return wrapper, nil
}
func (m *TestModelWrapper) Set(ctx context.Context) error {
if m.ref == nil {
return errors.New("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")
}
_, err := m.ref.Set(ctx, m.Data)
return err
}
func (m *TestModelWrapper) SetTx(ctx context.Context, tx *firestore.Transaction) error {
if m.ref == nil {
return errors.New("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")
}
err := tx.Set(m.ref, m.Data)
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,29 @@ func (c *clientTestTimestamps) GetByPath(ctx context.Context, path string) (*Tes
}
return wrapper, nil
}
func (c *clientTestTimestamps) GetByPathTx(ctx context.Context, tx *firestore.Transaction, path string) (*TestTimestampsWrapper, error) {
reference := c.client.Client.Doc(path)
snapshot, err := tx.Get(reference)
if err != nil {
return nil, err
}
wrapper, err := TestTimestampsFromSnapshot(snapshot)
if err != nil {
return nil, err
}
return wrapper, nil
}
func (m *TestTimestampsWrapper) Set(ctx context.Context) error {
if m.ref == nil {
return errors.New("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")
}
_, err := m.ref.Set(ctx, m.Data)
return err
}
func (m *TestTimestampsWrapper) SetTx(ctx context.Context, tx *firestore.Transaction) error {
if m.ref == nil {
return errors.New("Cannot call set on a firemodel object that has no reference. Call `create` on the orm with this object instead")
}
err := tx.Set(m.ref, m.Data)
return err
}

0 comments on commit a15118b

Please sign in to comment.