diff --git a/.gitignore b/.gitignore index 3ca2320..ad8eb4f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ .idea .build + +vendor/**/ \ No newline at end of file diff --git a/langs/go/go.go b/langs/go/go.go index 90ffd16..589cd79 100644 --- a/langs/go/go.go +++ b/langs/go/go.go @@ -300,6 +300,19 @@ 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"))) @@ -307,6 +320,15 @@ func (m *GoModeler) writeModel(model *firemodel.SchemaModel, sourceCoder firemod 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)) diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go index 0d72062..b9470bd 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go +++ b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_model.firemodel.go @@ -159,6 +159,18 @@ 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") @@ -166,3 +178,10 @@ func (m *TestModelWrapper) Set(ctx context.Context) error { _, 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 +} diff --git a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go index 35c32b9..ae48cdb 100644 --- a/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go +++ b/testfixtures/firemodel/TestFiremodelFromSchema/go/test_timestamps.firemodel.go @@ -110,6 +110,18 @@ 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") @@ -117,3 +129,10 @@ func (m *TestTimestampsWrapper) Set(ctx context.Context) error { _, 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 +}