Skip to content

Commit 5734b2d

Browse files
committed
mongodb json tag
1 parent 20bd7c0 commit 5734b2d

File tree

11 files changed

+33
-30
lines changed

11 files changed

+33
-30
lines changed

.github/RELEASE.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
## Change log
22

3-
- Add model, dao, handler, service, web service, and grpc service code generated from mongodb sql.
4-
- Add test generation code command script.
5-
- Update document.
3+
- Fix `⓶ create grpc service based on sql`, selecting multiple tables to generate code reports **unsupported db driver error**.
4+
- The mongodb json tag supports both shake case and camel case naming.

README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,15 @@ sponge is mainly based on `SQL` and `Protobuf` two ways to generate code, each w
4545

4646
<br>
4747

48-
#### Generate Code for Egg Model
48+
#### Generate Service Code for Egg Model
4949

50-
The sponge code generation process strips away the business logic and non-business logic of the two major parts of the code. Sponge's code generation function as a hen, the generated service code is the egg, take the generated web service backend code as an example:
51-
52-
- `Eggshell` is web service framework code (automatically generated without manual writing).
53-
- `Yolk` is the core of business logic. For example, defining mysql tables, defining api interfaces, and writing specific logic codes all belong to yolk (code that needs to be written manually).
54-
- `Albumen` is a bridge connecting the core code of business logic and the web framework code. For example, the registration route code, handler code, parameter verification code, error code, and swagger document generated according to the proto file belong to the albumen (code that automatically generated without manual writing).
55-
56-
Here is egg model profiling diagram for `⓷Create web service based on protobuf`:
50+
The sponge code generation process strips away the business logic and non-business logic of the two major parts of the code. Sponge's code generation function as a hen, the generated service code is the egg, take the generated web service backend code as an example, egg model profiling diagram:
5751

5852
<p align="center">
5953
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_web-http-pb-anatomy.png">
6054
</p>
6155

62-
In addition to the egg model of web service backend code, there are egg models of grpc service code and grpc gateway service code described in [sponge documentation](https://go-sponge.com/learn-about-sponge?id=%f0%9f%8f%b7project-code-egg-model).
56+
In addition to the egg model of web service backend code, there are egg models of grpc service code and grpc gateway service code, [click here to view](https://go-sponge.com/learn-about-sponge?id=%f0%9f%8f%b7egg-model-for-generate-service-code).
6357

6458
<br>
6559

assets/readme-cn.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,15 @@ sponge主要基于`SQL`和`Protobuf`两种方式生成代码,每种方式拥
2929

3030
<br>
3131

32-
#### 生成代码的鸡蛋模型
32+
#### 生成服务代码的鸡蛋模型
3333

34-
sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码,把sponge的生成代码功能看作是母鸡,生成的服务代码就是鸡蛋,以生成的一个web服务后端代码为例:
35-
36-
- `蛋壳`是web服务框架代码(自动生成,不需要人工编写)。
37-
- `蛋黄`是业务逻辑的核心,例如定义mysql表、在proto定义api接口、编写具体逻辑代码都属于蛋黄部分(需要人工编写的代码)。
38-
- `蛋白`是业务逻辑核心代码与web框架代码连接的桥梁,例如根据proto文件生成的注册路由代码、handler代码、dao代码、参数校验代码、错误码、swagger文档等都属于蛋白部分(自动生成,不需要人工编写)。
39-
40-
下面是`⓷基于protobuf创建的web服务`代码的鸡蛋模型剖析图:
34+
sponge生成代码过程中剥离了业务逻辑与非业务逻辑两大部分代码,把sponge的生成代码功能看作是母鸡,生成的服务代码就是鸡蛋,以生成的一个web服务后端代码为例,鸡蛋模型剖析图:
4135

4236
<p align="center">
4337
<img width="1200px" src="https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/web-http-pb-anatomy.png">
4438
</p>
4539

46-
除了web服务后端代码鸡蛋模型,还有grpc服务代码、grpc网关服务代码的鸡蛋模型,在[sponge文档](https://go-sponge.com/zh-cn/learn-about-sponge?id=%f0%9f%8f%b7%e9%a1%b9%e7%9b%ae%e4%bb%a3%e7%a0%81%e9%b8%a1%e8%9b%8b%e6%a8%a1%e5%9e%8b)中有介绍
40+
除了web服务后端代码鸡蛋模型,还有grpc服务代码和grpc网关服务代码的鸡蛋模型,点击[这里查看](https://go-sponge.com/zh-cn/learn-about-sponge?id=%f0%9f%8f%b7%e7%94%9f%e6%88%90%e6%9c%8d%e5%8a%a1%e4%bb%a3%e7%a0%81%e7%9a%84%e9%b8%a1%e8%9b%8b%e6%a8%a1%e5%9e%8b)
4741

4842
<br>
4943

cmd/sponge/commands/patch/gen-db-init.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ type dbInitGenerator struct {
9797
}
9898

9999
func (g *dbInitGenerator) generateCode() (string, error) {
100-
fmt.Println(*g)
101100
subTplName := "init-" + g.dbDriver
102101
r := generate.Replacers[generate.TplNameSponge]
103102
if r == nil {

cmd/sponge/server/handler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ func handleGenerateCode(c *gin.Context, outPath string, arg string) {
146146
}
147147

148148
out = os.TempDir() + gofile.GetPathDelimiter() + out
149-
//fmt.Println("outTmp =", out)
150149
args = append(args, fmt.Sprintf("--out=%s", out))
151150

152151
ctx, _ := context.WithTimeout(context.Background(), time.Second*10) // nolint

internal/config/serverNameExample_cc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ type Nacos struct {
2525
NamespaceID string `yaml:"namespaceID" json:"namespaceID"`
2626
Port int `yaml:"port" json:"port"`
2727
Scheme string `yaml:"scheme" json:"scheme"`
28-
}
28+
}

pkg/ggorm/test_sqlite.db

0 Bytes
Binary file not shown.

pkg/mgo/mongo_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@ import (
66

77
"github.com/stretchr/testify/assert"
88
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
910
mgoOptions "go.mongodb.org/mongo-driver/mongo/options"
1011
)
1112

1213
func TestInit(t *testing.T) {
1314
dsns := []string{
1415
"mongodb://root:123456@192.168.3.37:27017/account",
1516
"mongodb://root:123456@192.168.3.37:27017/account?connectTimeoutMS=2000",
17+
// error
18+
"mongodb-dsn",
19+
"mongodb://root:123456@192.168.3.37",
1620
}
1721
for _, dsn := range dsns {
1822
timeout := time.Second * 2
1923
opts := &mgoOptions.ClientOptions{Timeout: &timeout}
2024
db, err := Init(dsn, opts)
2125
if err != nil {
2226
t.Log(err)
23-
return
27+
continue
2428
}
2529
time.Sleep(time.Millisecond * 100)
2630
defer Close(db)
2731
}
32+
33+
defer func() { recover() }()
34+
db := &mongo.Database{}
35+
_ = Close(db)
2836
}
2937

3038
func TestInit2(t *testing.T) {

pkg/sql2code/parser/mongodb.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ var mgoTypeToGo = map[bsontype.Type]string{
6464
bson.TypeJavaScript: goTypeString,
6565
}
6666

67-
var jsonTagFormat int32 // 0: camel case, 1: snake case
67+
var jsonTagFormat int32 = 1 // 0: snake case, 1: camel case
6868

6969
// SetJSONTagSnakeCase set json tag format to snake case
7070
func SetJSONTagSnakeCase() {
71-
atomic.AddInt32(&jsonTagFormat, 1)
71+
atomic.AddInt32(&jsonTagFormat, -jsonTagFormat)
7272
}
7373

7474
// SetJSONTagCamelCase set json tag format to camel case
7575
func SetJSONTagCamelCase() {
76-
atomic.AddInt32(&jsonTagFormat, -jsonTagFormat)
76+
atomic.AddInt32(&jsonTagFormat, 1)
7777
}
7878

7979
// MgoField mongo field
@@ -172,9 +172,9 @@ func parseObject(name string, elements []bson.RawElement) (goTypeStr string, goO
172172

173173
var jsonTag string
174174
if jsonTagFormat == 0 {
175-
jsonTag = toLowerFirst(xstrings.ToCamelCase(k))
176-
} else {
177175
jsonTag = xstrings.ToSnakeCase(k)
176+
} else {
177+
jsonTag = toLowerFirst(xstrings.ToCamelCase(k))
178178
}
179179

180180
goObjStr += fmt.Sprintf(" %s %s `bson:\"%s\" json:\"%s\"`\n", xstrings.ToCamelCase(k), t, k, jsonTag)

pkg/sql2code/parser/parser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
320320
if opt.ForceTableName || data.RawTableName != inflection.Plural(data.RawTableName) {
321321
data.NameFunc = true
322322
}
323+
switch opt.DBDriver {
324+
case DBDriverMongodb:
325+
if opt.JSONNamedType != 0 {
326+
SetJSONTagCamelCase()
327+
} else {
328+
SetJSONTagSnakeCase()
329+
}
330+
}
323331

324332
data.TableName = toCamel(data.TableName)
325333
data.TName = firstLetterToLow(data.TableName)

pkg/sql2code/parser/parser_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func Test_getMongodbTableFields(t *testing.T) {
345345
},
346346
}
347347

348+
SetJSONTagCamelCase()
348349
goStructs := MgoFieldToGoStruct("foobar", fields)
349350
t.Log(goStructs)
350351

@@ -363,6 +364,7 @@ func Test_getMongodbTableFields(t *testing.T) {
363364
_ = codes
364365
//printCode(codes)
365366

367+
SetJSONTagSnakeCase()
366368
sql, fieldsMap = ConvertToSQLByMgoFields("foobar", fields)
367369
t.Log(sql)
368370
opts = []Option{

0 commit comments

Comments
 (0)