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

feat: add base mongo (#186) #186

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions cmd/static/client_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ func clientFlags() []cli.Flag {
&cli.StringSliceFlag{Name: consts.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes. (Valid only if idl is protobuf)"},
&cli.StringSliceFlag{Name: consts.Pass, Usage: "pass param to hz or kitex"},
&cli.BoolFlag{Name: consts.Verbose, Usage: "Turn on verbose mode."},
&cli.BoolFlag{Name: consts.GenBase, Usage: "Generate base mongo code, default is false."},
chaoranz758 marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions cmd/static/doc_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func docFlags() []cli.Flag {
&cli.StringSliceFlag{Name: consts.ThriftGo, Aliases: []string{"t"}, Usage: "Specify arguments for the thriftgo. ({flag}={value})"},
&cli.StringSliceFlag{Name: consts.Protoc, Aliases: []string{"p"}, Usage: "Specify arguments for the protoc. ({flag}={value})"},
&cli.BoolFlag{Name: consts.Verbose, Usage: "Turn on verbose mode, default is false."},
&cli.BoolFlag{Name: consts.GenBase, Usage: "Generate base mongo code, default is false."},
}
}
2 changes: 2 additions & 0 deletions config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type DocArgument struct {
ProtoSearchPath []string
ProtocOptions []string // options to pass through to protoc
ThriftOptions []string // options to pass through to thriftgo for go flag
GenBase bool
}

func NewDocArgument() *DocArgument {
Expand All @@ -55,6 +56,7 @@ func (d *DocArgument) ParseCli(ctx *cli.Context) error {
d.ProtoSearchPath = ctx.StringSlice(consts.ProtoSearchPath)
d.ProtocOptions = ctx.StringSlice(consts.Protoc)
d.ThriftOptions = ctx.StringSlice(consts.ThriftGo)
d.GenBase = ctx.Bool(consts.GenBase)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const (
ProtoSearchPath = "proto_search_path"
ThriftGo = "thriftgo"
Protoc = "protoc"
GenBase = "gen_base"

ProjectPath = "project_path"
HertzRepoUrl = "hertz_repo_url"
Expand Down
37 changes: 37 additions & 0 deletions pkg/curd/doc/mongo/codegen/aggregate_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package codegen

import "github.com/cloudwego/cwgo/pkg/curd/code"

func aggregateBaseCodegen() []code.Statement {
stmt := `if pipeline == nil {
return fmt.Errorf("pipeline param is empty")
}

cursor, err := b.collection.Aggregate(ctx, pipeline)
if err != nil {
return err
}
defer cursor.Close(ctx)

return cursor.All(ctx, result)`

return []code.Statement{
code.RawStmt(stmt),
}
}
260 changes: 260 additions & 0 deletions pkg/curd/doc/mongo/codegen/base_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package codegen

import "github.com/cloudwego/cwgo/pkg/curd/code"

func GetMInsertOneParams() (res []code.Param) {
ctx := code.Param{
ZhuYZ21 marked this conversation as resolved.
Show resolved Hide resolved
Name: "ctx",
Type: code.IdentType("context.Context"),
}
insertOneData := code.Param{
Name: "insertOneData",
Type: code.InterfaceType{
Name: "interface{}",
},
}
res = append(res, ctx, insertOneData)

return
}

func GetMDeleteOneParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
deleteOneData := code.Param{
Name: "deleteOneData",
Type: code.InterfaceType{
Name: "interface{}",
},
}
res = append(res, ctx, deleteOneData)

return
}

func GetMFindOneParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
result := code.Param{
Name: "result",
Type: code.InterfaceType{
Name: "interface{}",
},
}
res = append(res, ctx, selector, result)

return
}

func GetMBulkInsertParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
batchData := code.Param{
Name: "batchData",
Type: code.IdentType("[]interface{}"),
}

res = append(res, ctx, batchData)

return
}

func GetMBulkUpdateParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
filter := code.Param{
Name: "filter",
Type: code.IdentType("[]interface{}"),
}
updater := code.Param{
Name: "updater",
Type: code.IdentType("[]interface{}"),
}

res = append(res, ctx, filter, updater)

return
}

func GetMAggregateParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
pipeline := code.Param{
Name: "pipeline",
Type: code.IdentType("[]bson.M"),
}
result := code.Param{
Name: "result",
Type: code.IdentType("interface{}"),
}

res = append(res, ctx, pipeline, result)

return
}

func GetMCountParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}

res = append(res, ctx, selector)

return
}

func GetMUpdateOneParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
updater := code.Param{
Name: "updater",
Type: code.IdentType("bson.M"),
}

res = append(res, ctx, selector, updater)

return
}

func GetMUpdateManyParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
updater := code.Param{
Name: "updater",
Type: code.IdentType("bson.M"),
}

res = append(res, ctx, selector, updater)

return
}

func GetMFindListParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
result := code.Param{
Name: "result",
Type: code.InterfaceType{
Name: "interface{}",
},
}
res = append(res, ctx, selector, result)

return
}

func GetMFindPageListParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
sort := code.Param{
Name: "sort",
Type: code.IdentType("string"),
}
skip := code.Param{
Name: "skip",
Type: code.IdentType("int"),
}
limit := code.Param{
Name: "limit",
Type: code.IdentType("int"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
result := code.Param{
Name: "result",
Type: code.InterfaceType{
Name: "interface{}",
},
}
res = append(res, ctx, selector, sort, skip, limit, result)
return
}

func GetMFindSortPageListParams() (res []code.Param) {
ctx := code.Param{
Name: "ctx",
Type: code.IdentType("context.Context"),
}
skip := code.Param{
Name: "skip",
Type: code.IdentType("int"),
}
limit := code.Param{
Name: "limit",
Type: code.IdentType("int"),
}
selector := code.Param{
Name: "selector",
Type: code.IdentType("bson.M"),
}
result := code.Param{
Name: "result",
Type: code.InterfaceType{
Name: "interface{}",
},
}
sort := code.Param{
Name: "sorts",
Type: code.IdentType("...string"),
}

res = append(res, ctx, selector, skip, limit, result, sort)
return
}
53 changes: 53 additions & 0 deletions pkg/curd/doc/mongo/codegen/bulk_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package codegen

import "github.com/cloudwego/cwgo/pkg/curd/code"

func bulkInsertBaseCodegen() []code.Statement {
stmt := `if len(batchData) == 0 {
return nil, fmt.Errorf("batch param is empty")
}

models := make([]mongo.WriteModel, len(batchData))
for i, doc := range batchData {
models[i] = mongo.NewInsertOneModel().SetDocument(doc)
}

return b.collection.BulkWrite(ctx, models)`

return []code.Statement{
code.RawStmt(stmt),
}
}

func bulkUpdateBaseCodegen() []code.Statement {
stmt := `if len(filter) != len(updater) {
return nil, fmt.Errorf("filter and updater must have the same length")
}

models := make([]mongo.WriteModel, len(filter))
for i := range filter {
models[i] = mongo.NewUpdateOneModel().SetFilter(filter[i]).SetUpdate(updater[i])
}

return b.collection.BulkWrite(ctx, models)`

return []code.Statement{
code.RawStmt(stmt),
}
}
Loading
Loading