Skip to content

Commit

Permalink
Merge pull request #12 from hongminhcbg/feature/support-json-type
Browse files Browse the repository at this point in the history
feature/support-json-type
  • Loading branch information
hongminhcbg authored Feb 5, 2023
2 parents 616fba2 + 78ccb9e commit a671600
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ vendor/
# Go workspace file
go.work
main
gocrud
collections.yaml
*_model.go
*_migrate.sql
*_store.go
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ III. Usage
- [ ] Support datetime type
- [ ] Support Email type
- [ ] Support JSON type
- [OK] Intergrate https://github.com/go-playground/validator
- [ ] Intergrate https://github.com/go-playground/validator
6 changes: 5 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func generate(ctx *cli.Context) error {
fieldList := make([]fields.IField, 0, len(c.Fields))
for i := 0; i < len(c.Fields); i++ {
sqlHint := utils.ParseSqlHint(c.Fields[i].SqlHint)
fmt.Println("parse sql hint", c.Fields[i].SqlHint, " output is ", sqlHint)
fmt.Println("parse sql hint", " fieldName:", c.Fields[i].Name, " hint:", c.Fields[i].SqlHint, " output is ", sqlHint)
if strings.EqualFold(c.Fields[i].Type, "text") {
fieldList = append(fieldList, fields.NewFieldString(c.Fields[i].Name, c.Fields[i].Comment, sqlHint, c.Fields[i].Validate))
continue
Expand Down Expand Up @@ -74,6 +74,10 @@ func generate(ctx *cli.Context) error {
if strings.EqualFold(c.Fields[i].Type, "bool") {
fieldList = append(fieldList, fields.NewBoolField(c.Fields[i].Name, c.Fields[i].Comment, sqlHint, c.Fields[i].Validate))
}

if strings.EqualFold(c.Fields[i].Type, "json") {
fieldList = append(fieldList, fields.NewJsonFileld(c.Fields[i].Name, c.Fields[i].Comment, c.Fields[i].Validate, sqlHint))
}
}

stru := str.NewStruct(c.Name, fieldList)
Expand Down
23 changes: 16 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package main

import (
"fmt"
"os"

"github.com/urfave/cli/v3"
)

const rawInit = `
# data_type
# text => free text
# bool => true, false
# json => https://github.com/go-gorm/datatypes/blob/master/json.go
# int8 => uint8
# int16 => int16
# int32 => int32
# int64 => int64
name: users
type: mysql_gorm
fields:
- name: fullname
type: text
validate: 'required' #https://github.com/go-playground/validator
comment: 'fullname of user'
sql_hint: 'type=BIGINT(20),key=candidate,is_not_null=true, xxx'
- name: fullname # required, snake case
type: text # required, in list ['text', 'bool', 'json', 'int8', 'int16', 'int32', 'int64']
validate: 'required' # optional, https://github.com/go-playground/validator
comment: 'fullname of user' # optional
sql_hint: 'type=BIGINT(20),key=candidate,is_not_null=true' # optional, hint for create table, support flags [], if empty create default
- name: age
type: int16
validate: 'gte=1,lte=200' #https://github.com/go-playground/validator
Expand All @@ -32,9 +39,11 @@ fields:
- name: account_balance
type: int64
comment: 'total money'
- name: raw
type: json
comment: 'raw json fo user'
`

func initCmd(ctx *cli.Context) error {
fmt.Println("this init func")
return os.WriteFile("collections.yaml", []byte(rawInit), 0644)
}
2 changes: 0 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

"github.com/urfave/cli/v3"
Expand All @@ -11,7 +10,6 @@ var app = new(cli.App)
var fileInput string

func main() {
fmt.Println(os.Args)
app.Commands = []*cli.Command{
{
Name: "init",
Expand Down
34 changes: 0 additions & 34 deletions collections_tmpl.yaml

This file was deleted.

70 changes: 70 additions & 0 deletions fields/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package fields

import (
"fmt"
"strings"

"github.com/hongminhcbg/gocrud/models"
"github.com/hongminhcbg/gocrud/utils"
)

type _json struct {
snakeCase string
camelCase string
comment string
sqlHint *models.SqlHint
validate string
}

func NewJsonFileld(snake, comment, validate string, sqlHint *models.SqlHint) IField {
if sqlHint == nil {
sqlHint = new(models.SqlHint)
}

return &_json{
snakeCase: snake,
camelCase: utils.SnakeToCamel(snake),
validate: validate,
sqlHint: sqlHint,
}
}

func (j *_json) Name() string {
return j.camelCase
}

func (j *_json) DataType() string {
return "datatypes.JSON"
}

func (j *_json) Annotation() string {
if j.validate == "" {
return fmt.Sprintf("`gorm:\"column:%s\" json:\"%s,omitempty\"`", j.snakeCase, j.snakeCase)
}

return fmt.Sprintf("`gorm:\"column:%s\" json:\"%s,omitempty\" validate:\"%s\"`", j.snakeCase, j.snakeCase, j.validate)
}

func (j *_json) Comment() string { return j.comment }

func (j *_json) NameSnake() string { return j.snakeCase }

func (j *_json) GenSql() string {
ans := new(strings.Builder)
ans.WriteString(fmt.Sprintf("`%s` ", j.snakeCase))
if j.sqlHint.DataType != "" {
ans.WriteString(fmt.Sprintf("%s ", j.sqlHint.DataType))
} else {
ans.WriteString("JSON ")
}

if j.sqlHint.IsNotNull {
ans.WriteString("NOT NULL ")
}

if j.sqlHint.DefaultVal != "" {
ans.WriteString(fmt.Sprintf("DEFAILT '%s' ", j.sqlHint.DefaultVal))
}

return ans.String()
}
1 change: 1 addition & 0 deletions str/model_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
import (
"github.com/go-playground/validator/v10"
"github.com/go-gorm/datatypes"
)
var %sValidate *validator.Validate
Expand Down
36 changes: 36 additions & 0 deletions tmpl/collections_tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# data_type
# text => free text
# bool => true, false
# json => https://github.com/go-gorm/datatypes/blob/master/json.go
# int8 => uint8
# int16 => int16
# int32 => int32
# int64 => int64
name: users
type: mysql_gorm
fields:
- name: fullname # required, snake case
type: text # required, in list ['text', 'bool', 'json', 'int8', 'int16', 'int32', 'int64']
validate: 'required' # optional, https://github.com/go-playground/validator
comment: 'fullname of user' # optional
sql_hint: 'type=BIGINT(20),key=candidate,is_not_null=true' # optional, hint for create table, support flags [], if empty create default
- name: age
type: int16
validate: 'gte=1,lte=200' #https://github.com/go-playground/validator
comment: 'age'
sql_hint: 'type=INT(10),key=candidate,is_not_null=true,default=18'
- name: email
type: text
validate: 'required,email' #https://github.com/go-playground/validator
comment: 'email'
sql_hint: 'type=VARCHAR(64),key=unique,is_not_null=true'
- name: sex
type: bool
comment: 'true:male,false:female'
- name: account_balance
type: int64
comment: 'total money'
- name: raw
type: json
comment: 'raw json fo user'

4 changes: 4 additions & 0 deletions utils/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func CamelToSnake(camel string) (snake string) {

func ParseSqlHint(in string) *models.SqlHint {
ans := new(models.SqlHint)
if in == "" {
return ans
}

args := strings.Split(in, ",")
for _, kv := range args {
keyVal := strings.Split(kv, "=")
Expand Down

0 comments on commit a671600

Please sign in to comment.