fix: gen models
This commit is contained in:
@@ -93,7 +93,7 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return defaultTableModelField
|
return defaultTableModelField
|
||||||
}
|
}
|
||||||
|
|
||||||
splits := strings.Split(toType, ".")
|
splits := strings.SplitN(toType, ".", 2)
|
||||||
typeName := splits[len(splits)-1]
|
typeName := splits[len(splits)-1]
|
||||||
|
|
||||||
pkg := splits[0]
|
pkg := splits[0]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-jet/jet/v2/qrm"
|
"github.com/go-jet/jet/v2/qrm"
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
@@ -31,6 +32,7 @@ type Response struct {
|
|||||||
StatusCode int `json:"-" xml:"-"`
|
StatusCode int `json:"-" xml:"-"`
|
||||||
Code int `json:"code" xml:"code"`
|
Code int `json:"code" xml:"code"`
|
||||||
Message string `json:"message" xml:"message"`
|
Message string `json:"message" xml:"message"`
|
||||||
|
Data any `json:"data,omitempty" xml:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(code, statusCode int, message string) *Response {
|
func New(code, statusCode int, message string) *Response {
|
||||||
@@ -47,6 +49,13 @@ func (r *Response) Sql(sql string) *Response {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Response) from(err *Response) *Response {
|
||||||
|
r.Code = err.Code
|
||||||
|
r.Message = err.Message
|
||||||
|
r.StatusCode = err.StatusCode
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) Params(params ...any) *Response {
|
func (r *Response) Params(params ...any) *Response {
|
||||||
r.params = params
|
r.params = params
|
||||||
if _, file, line, ok := runtime.Caller(1); ok {
|
if _, file, line, ok := runtime.Caller(1); ok {
|
||||||
@@ -62,12 +71,15 @@ func Wrap(err error) *Response {
|
|||||||
return &Response{err: err}
|
return &Response{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Response) Wrap(err error) *Response {
|
||||||
|
r.err = err
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) format() {
|
func (r *Response) format() {
|
||||||
r.isFormat = true
|
r.isFormat = true
|
||||||
if errors.Is(r.err, qrm.ErrNoRows) {
|
if errors.Is(r.err, qrm.ErrNoRows) {
|
||||||
r.Code = RecordNotExists.Code
|
r.from(RecordNotExists)
|
||||||
r.Message = RecordNotExists.Message
|
|
||||||
r.StatusCode = RecordNotExists.StatusCode
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +89,19 @@ func (r *Response) format() {
|
|||||||
r.StatusCode = e.Code
|
r.StatusCode = e.Code
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.err != nil {
|
||||||
|
msg := r.err.Error()
|
||||||
|
if strings.Contains(msg, "duplicate key value") || strings.Contains(msg, "unique constraint") {
|
||||||
|
r.from(RecordDuplicated)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Code = http.StatusInternalServerError
|
||||||
|
r.StatusCode = http.StatusInternalServerError
|
||||||
|
r.Message = msg
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Error() string {
|
func (r *Response) Error() string {
|
||||||
@@ -95,7 +120,12 @@ func (r *Response) Response(ctx fiber.Ctx) error {
|
|||||||
contentType := utils.ToLower(utils.UnsafeString(ctx.Context().Request.Header.ContentType()))
|
contentType := utils.ToLower(utils.UnsafeString(ctx.Context().Request.Header.ContentType()))
|
||||||
contentType = binder.FilterFlags(utils.ParseVendorSpecificContentType(contentType))
|
contentType = binder.FilterFlags(utils.ParseVendorSpecificContentType(contentType))
|
||||||
|
|
||||||
log.WithError(r.err).WithField("file", r.file).WithField("params", r.params).Errorf("response error: %+v", r)
|
log.
|
||||||
|
WithError(r.err).
|
||||||
|
WithField("file", r.file).
|
||||||
|
WithField("sql", r.sql).
|
||||||
|
WithField("params", r.params).
|
||||||
|
Errorf("response error: %+v", r)
|
||||||
|
|
||||||
// Parse body accordingly
|
// Parse body accordingly
|
||||||
switch contentType {
|
switch contentType {
|
||||||
@@ -104,11 +134,12 @@ func (r *Response) Response(ctx fiber.Ctx) error {
|
|||||||
case fiber.MIMETextHTML, fiber.MIMETextPlain:
|
case fiber.MIMETextHTML, fiber.MIMETextPlain:
|
||||||
return ctx.Status(r.StatusCode).SendString(r.Message)
|
return ctx.Status(r.StatusCode).SendString(r.Message)
|
||||||
default:
|
default:
|
||||||
return ctx.Status(r.StatusCode).JSON(r.Message)
|
return ctx.Status(r.StatusCode).JSON(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
RecordDuplicated = New(1001, http.StatusBadRequest, "记录重复")
|
||||||
RecordNotExists = New(http.StatusNotFound, http.StatusNotFound, "记录不存在")
|
RecordNotExists = New(http.StatusNotFound, http.StatusNotFound, "记录不存在")
|
||||||
BadRequest = New(http.StatusBadRequest, http.StatusBadRequest, "请求错误")
|
BadRequest = New(http.StatusBadRequest, http.StatusBadRequest, "请求错误")
|
||||||
Unauthorized = New(http.StatusUnauthorized, http.StatusUnauthorized, "未授权")
|
Unauthorized = New(http.StatusUnauthorized, http.StatusUnauthorized, "未授权")
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func FromContext(ctx context.Context, db *sql.DB) qrm.DB {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func TruncateAllTables(ctx context.Context, db *sql.DB, tableName ...string) error {
|
func Truncate(ctx context.Context, db *sql.DB, tableName ...string) error {
|
||||||
for _, name := range tableName {
|
for _, name := range tableName {
|
||||||
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
|
sql := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY", name)
|
||||||
if _, err := db.ExecContext(ctx, sql); err != nil {
|
if _, err := db.ExecContext(ctx, sql); err != nil {
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// implement sql.Scanner interface
|
// implement sql.Scanner interface
|
||||||
type field struct{}
|
type Json[T any] struct {
|
||||||
|
Data T `json:",inline"`
|
||||||
|
}
|
||||||
|
|
||||||
func (x *field) Scan(value interface{}) (err error) {
|
func ToJson[T any](data T) Json[T] {
|
||||||
|
return Json[T]{Data: data}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Json[T]) Scan(value interface{}) (err error) {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
return json.Unmarshal([]byte(v), &x)
|
return json.Unmarshal([]byte(v), &x)
|
||||||
@@ -23,10 +27,19 @@ func (x *field) Scan(value interface{}) (err error) {
|
|||||||
return errors.New("Unknown type for ")
|
return errors.New("Unknown type for ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x field) Value() (driver.Value, error) {
|
func (x Json[T]) Value() (driver.Value, error) {
|
||||||
return json.Marshal(x)
|
return json.Marshal(x.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x field) MustValue() driver.Value {
|
func (x Json[T]) MarshalJSON() ([]byte, error) {
|
||||||
return lo.Must(json.Marshal(x))
|
return json.Marshal(x.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x *Json[T]) UnmarshalJSON(data []byte) error {
|
||||||
|
var value T
|
||||||
|
if err := json.Unmarshal(data, &value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
x.Data = value
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user