mirror of
https://github.com/zhufuyi/sponge.git
synced 2025-12-24 10:40:55 +08:00
uniform json name style
This commit is contained in:
@@ -124,11 +124,12 @@ type tmplData struct {
|
||||
}
|
||||
|
||||
type tmplField struct {
|
||||
Name string
|
||||
ColName string
|
||||
GoType string
|
||||
Tag string
|
||||
Comment string
|
||||
Name string
|
||||
ColName string
|
||||
GoType string
|
||||
Tag string
|
||||
Comment string
|
||||
JSONName string
|
||||
}
|
||||
|
||||
// ConditionZero type of condition 0
|
||||
@@ -280,10 +281,14 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
|
||||
if columnPrefix != "" && strings.HasPrefix(goFieldName, columnPrefix) {
|
||||
goFieldName = goFieldName[len(columnPrefix):]
|
||||
}
|
||||
|
||||
jsonName := colName
|
||||
if opt.JSONNamedType != 0 {
|
||||
jsonName = xstrings.FirstRuneToLower(xstrings.ToCamelCase(colName)) // name type use camel case
|
||||
}
|
||||
field := tmplField{
|
||||
Name: toCamel(goFieldName),
|
||||
ColName: colName,
|
||||
Name: toCamel(goFieldName),
|
||||
ColName: colName,
|
||||
JSONName: jsonName,
|
||||
}
|
||||
|
||||
tags := make([]string, 0, 4)
|
||||
@@ -335,10 +340,7 @@ func makeCode(stmt *ast.CreateTableStmt, opt options) (*codeText, error) {
|
||||
tags = append(tags, "gorm", gormTag.String())
|
||||
|
||||
if opt.JSONTag {
|
||||
if opt.JSONNamedType != 0 {
|
||||
colName = xstrings.FirstRuneToLower(xstrings.ToCamelCase(colName)) // use hump type json names
|
||||
}
|
||||
tags = append(tags, "json", colName)
|
||||
tags = append(tags, "json", jsonName)
|
||||
}
|
||||
|
||||
field.Tag = makeTagStr(tags)
|
||||
|
||||
@@ -52,7 +52,7 @@ import (
|
||||
// todo fill in the binding rules https://github.com/go-playground/validator
|
||||
type Create{{.TableName}}Request struct {
|
||||
{{- range .Fields}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.ColName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
@@ -62,7 +62,7 @@ type Create{{.TableName}}Request struct {
|
||||
// Update{{.TableName}}ByIDRequest update params
|
||||
type Update{{.TableName}}ByIDRequest struct {
|
||||
{{- range .Fields}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.ColName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}" binding:""` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
@@ -72,7 +72,7 @@ type Update{{.TableName}}ByIDRequest struct {
|
||||
// Get{{.TableName}}ByIDRespond respond detail
|
||||
type Get{{.TableName}}ByIDRespond struct {
|
||||
{{- range .Fields}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.ColName}}"` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{.Name}} {{.GoType}} ` + "`" + `json:"{{.JSONName}}"` + "`" + `{{if .Comment}} // {{.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}`
|
||||
|
||||
@@ -244,7 +244,15 @@ service {{.TName}}Service {
|
||||
}
|
||||
}
|
||||
|
||||
// todo fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
|
||||
// Some notes on defining fields under message:
|
||||
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
|
||||
// (2) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
|
||||
// you must add annotations for snake case names, such as string foo_bar = 1 [json_name = "foo_bar"],
|
||||
// to ensure that the front end and back end JSON naming is consistent.
|
||||
// (3) If the declared route path includes a variable, such as /api/v1/userExample/{id},
|
||||
// the request parameter of the rpc method contains the route variable field and this field
|
||||
// must be annotated, such as int64 id = 1 [(tagger.tags) = "uri:\"id\"" ]
|
||||
|
||||
|
||||
// protoMessageCreateCode
|
||||
|
||||
@@ -305,21 +313,21 @@ message List{{.TableName}}Reply {
|
||||
protoMessageCreateTmpl *template.Template
|
||||
protoMessageCreateTmplRaw = `message Create{{.TableName}}Request {
|
||||
{{- range $i, $v := .Fields}}
|
||||
{{$v.GoType}} {{$v.ColName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}`
|
||||
|
||||
protoMessageUpdateTmpl *template.Template
|
||||
protoMessageUpdateTmplRaw = `message Update{{.TableName}}ByIDRequest {
|
||||
{{- range $i, $v := .Fields}}
|
||||
{{$v.GoType}} {{$v.ColName}} = {{$v.AddOneWithTag $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOneWithTag $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}`
|
||||
|
||||
protoMessageDetailTmpl *template.Template
|
||||
protoMessageDetailTmplRaw = `message {{.TableName}} {
|
||||
{{- range $i, $v := .Fields}}
|
||||
{{$v.GoType}} {{$v.ColName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{$v.GoType}} {{$v.JSONName}} = {{$v.AddOne $i}}; {{if $v.Comment}} // {{$v.Comment}}{{end}}
|
||||
{{- end}}
|
||||
}`
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ type Args struct {
|
||||
Package string // specify the package name (only valid for model types)
|
||||
GormType bool // whether to display the gorm type name (only valid for model type codes)
|
||||
JSONTag bool // does it include a json tag
|
||||
JSONNamedType int // json naming type, 0: consistent with the column name, other values indicate a hump
|
||||
JSONNamedType int // json field naming type, 0: snake case such as my_field_name, 1: camel sase, such as myFieldName
|
||||
IsEmbed bool // is gorm.Model embedded
|
||||
CodeType string // specify the different types of code to be generated, namely model (default), json, dao, handler, proto
|
||||
ForceTableName bool
|
||||
|
||||
Reference in New Issue
Block a user