Skip to content

Commit

Permalink
feat: add opt WithSQLNullType
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed Oct 16, 2024
1 parent d7de699 commit d3a17ac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ func (cfg *Config) WithImportPkgPath(paths ...string) {
cfg.importPkgPaths = append(cfg.importPkgPaths, paths...)
}

// WithSQLNullType

Check failure on line 118 in config.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] config.go#L118

exported: comment on exported method Config.WithSQLNullType should be of the form "WithSQLNullType ..." (revive)
Raw output
config.go:118:1: exported: comment on exported method Config.WithSQLNullType should be of the form "WithSQLNullType ..." (revive)
// WithSQLNullType
^
/**
* This function configures the types of fields to use their SQL nullable counterparts.
*
* @param {boolean} all - If true, all basic types of fields will be replaced with their `sql.NullXXX` types.
* If false, only fields that are allowed to be null will be replaced with `sql.NullXXX` types.
*
* Examples:
*
* When `all` is true:
* - `int64` will be replaced with `sql.NullInt64`
* - `string` will be replaced with `sql.NullString`
*
* When `all` is false:
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `sql.NullXXX` types.
*
* Note:
* Ensure that proper error handling is implemented when converting
* fields to their `sql.NullXXX` types to avoid runtime issues.
*/
func (cfg *Config) WithSQLNullType(all bool) {
cfg.WithOpts(WithSQLNullType(all))
}

// Revise format path and db
func (cfg *Config) Revise() (err error) {
if strings.TrimSpace(cfg.ModelPkgPath) == "" {
Expand Down
53 changes: 53 additions & 0 deletions field_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,59 @@ var (
return opt(f)
}
}
// WithSQLNullType

Check failure on line 28 in field_options.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] field_options.go#L28

exported: comment on exported var WithSQLNullType should be of the form "WithSQLNullType ..." (revive)
Raw output
field_options.go:28:2: exported: comment on exported var WithSQLNullType should be of the form "WithSQLNullType ..." (revive)
	// WithSQLNullType
	^
/**
* This function configures the types of fields to use their SQL nullable counterparts.
*
* @param {boolean} all - If true, all basic types of fields will be replaced with their `sql.NullXXX` types.
* If false, only fields that are allowed to be null will be replaced with `sql.NullXXX` types.
*
* Examples:
*
* When `all` is true:
* - `int64` will be replaced with `sql.NullInt64`
* - `string` will be replaced with `sql.NullString`
*
* When `all` is false:
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `sql.NullXXX` types.
*
* Note:
* Ensure that proper error handling is implemented when converting
* fields to their `sql.NullXXX` types to avoid runtime issues.
*/
WithSQLNullType = func(all bool) model.ModifyFieldOpt {
return func(f *model.Field) *model.Field {
ft := f.Type
nullable := false
if strings.HasPrefix(ft, "*") {
nullable = true
ft = ft[1:]
}
if !all && !nullable {
return f
}

switch ft {
case "string":
f.Type = "sql.NullString"
case "int", "int64", "uint", "uint32", "uint64":
f.Type = "sql.NullInt64"
case "uint16", "int32":
f.Type = "sql.NullInt32"
case "int8", "uint8", "int16":
f.Type = "sql.NullInt16"
case "byte":
f.Type = "sql.NullByte"
case "float64", "float32":
f.Type = "sql.NullFloat64"
case "bool":
f.Type = "sql.NullBool"
case "time.Time":
f.Type = "sql.NullTime"
}
return f
}
}

// FieldNew add new field (any type your want)
FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt {
Expand Down

0 comments on commit d3a17ac

Please sign in to comment.