From 78ccb9ed88c2ecdcf897f8ead220dcaef1d55464 Mon Sep 17 00:00:00 2001 From: Hong Minh Date: Sun, 5 Feb 2023 15:40:45 +0700 Subject: [PATCH] feature/support-json-type --- .gitignore | 2 ++ README.md | 2 +- cmd/generate.go | 6 +++- cmd/init.go | 23 +++++++++---- cmd/main.go | 2 -- collections_tmpl.yaml | 34 ------------------ fields/json.go | 70 ++++++++++++++++++++++++++++++++++++++ str/model_gen.go | 1 + tmpl/collections_tmpl.yaml | 36 ++++++++++++++++++++ utils/x.go | 4 +++ 10 files changed, 135 insertions(+), 45 deletions(-) delete mode 100644 collections_tmpl.yaml create mode 100644 fields/json.go create mode 100644 tmpl/collections_tmpl.yaml diff --git a/.gitignore b/.gitignore index ea82d5b..8b20ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,8 @@ vendor/ # Go workspace file go.work main +gocrud collections.yaml *_model.go *_migrate.sql +*_store.go diff --git a/README.md b/README.md index d2c2966..a311bed 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/generate.go b/cmd/generate.go index 3da99f3..24f2389 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -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 @@ -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) diff --git a/cmd/init.go b/cmd/init.go index f0bcfae..51e707f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 @@ -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) } diff --git a/cmd/main.go b/cmd/main.go index cab5f5b..96aa7c2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "github.com/urfave/cli/v3" @@ -11,7 +10,6 @@ var app = new(cli.App) var fileInput string func main() { - fmt.Println(os.Args) app.Commands = []*cli.Command{ { Name: "init", diff --git a/collections_tmpl.yaml b/collections_tmpl.yaml deleted file mode 100644 index fe3abb2..0000000 --- a/collections_tmpl.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# data type -# text => free text -# bool => true, false -# url => url format -# json => raw byte -# 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: 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' - diff --git a/fields/json.go b/fields/json.go new file mode 100644 index 0000000..b26ea82 --- /dev/null +++ b/fields/json.go @@ -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() +} diff --git a/str/model_gen.go b/str/model_gen.go index 246829f..83ba19b 100644 --- a/str/model_gen.go +++ b/str/model_gen.go @@ -11,6 +11,7 @@ package main import ( "github.com/go-playground/validator/v10" + "github.com/go-gorm/datatypes" ) var %sValidate *validator.Validate diff --git a/tmpl/collections_tmpl.yaml b/tmpl/collections_tmpl.yaml new file mode 100644 index 0000000..28d3306 --- /dev/null +++ b/tmpl/collections_tmpl.yaml @@ -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' + diff --git a/utils/x.go b/utils/x.go index 479c98a..d41e53e 100644 --- a/utils/x.go +++ b/utils/x.go @@ -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, "=")