-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from hongminhcbg/feature/support-json-type
feature/support-json-type
- Loading branch information
Showing
10 changed files
with
135 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ vendor/ | |
# Go workspace file | ||
go.work | ||
main | ||
gocrud | ||
collections.yaml | ||
*_model.go | ||
*_migrate.sql | ||
*_store.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters