Skip to content

Commit

Permalink
feat(*): adjust some details, specially for xgorm
Browse files Browse the repository at this point in the history
  • Loading branch information
Aoi-hosizora committed Aug 26, 2020
1 parent b367b97 commit 765bad4
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 72 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Functions

+ **xgin**
+ **xfiber**
+ xfiber
+ **xgorm**
+ **xredis**
+ xneo4j
Expand All @@ -19,7 +19,6 @@

+ See [go.mod](./go.mod) and [go.sum](./go.sum)
+ `github.com/Aoi-hosizora/ahlib v1.3.9`
+ `github.com/Aoi-hosizora/ahlib-more v1.0.0`
+ `github.com/gin-gonic/gin v1.6.3`
+ `github.com/go-playground/validator/v10 v10.2.0`
+ `github.com/gofiber/fiber v1.12.6`
Expand Down
2 changes: 1 addition & 1 deletion xdto/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ErrorDto struct {

// Build a basic dto (only include time, type, detail, request).
func BuildBasicErrorDto(err interface{}, requests []string) *ErrorDto {
return BuildErrorDto(err, requests, -2, false)
return BuildErrorDto(err, requests, -1, false)
}

// Build a complete dto (also include runtime parameters).
Expand Down
4 changes: 2 additions & 2 deletions xfiber/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func DumpRequest(c *fiber.Ctx) []string {
return request
}

bytes := c.Fasthttp.Request.String()
params := strings.Split(bytes, "\r\n")
str := c.Fasthttp.Request.String()
params := strings.Split(str, "\r\n")
for _, param := range params {
if strings.HasPrefix(param, "Authorization:") { // Authorization header
request = append(request, "Authorization: *")
Expand Down
3 changes: 0 additions & 3 deletions xgin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@
+ `EnableRegexpBinding() error`
+ `EnableRFC3339DateBinding() error`
+ `EnableRFC3339DateTimeBinding() error`
+ `type HandlerFuncW func(c *gin.Context) (int, interface{})`
+ `JsonW(fn HandlerFuncW) gin.HandlerFunc`
+ `XmlW(fn HandlerFuncW) gin.HandlerFunc`
4 changes: 2 additions & 2 deletions xgin/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func DumpRequest(c *gin.Context) []string {
return request
}

bytes, _ := httputil.DumpRequest(c.Request, false)
params := strings.Split(string(bytes), "\r\n")
bs, _ := httputil.DumpRequest(c.Request, false)
params := strings.Split(string(bs), "\r\n")
for _, param := range params {
if strings.HasPrefix(param, "Authorization:") { // Authorization header
request = append(request, "Authorization: *")
Expand Down
21 changes: 0 additions & 21 deletions xgin/w.go

This file was deleted.

11 changes: 0 additions & 11 deletions xgin/xgin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,3 @@ func TestBuildErrorDto(t *testing.T) {
})
_ = app.Run(":1234")
}

func TestW(t *testing.T) {
app := gin.New()
app.GET("json", JsonW(func(c *gin.Context) (int, interface{}) {
return 200, &gin.H{"test": "hello world"}
}))
app.GET("xml", XmlW(func(c *gin.Context) (int, interface{}) {
return 200, &gin.H{"test": "hello world"}
}))
_ = app.Run(":1234")
}
1 change: 1 addition & 0 deletions xgorm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
+ `(h *Helper) Create(model interface{}, object interface{}) (xstatus.DbStatus, error)`
+ `(h *Helper) Update(model interface{}, where interface{}, object interface{}) (xstatus.DbStatus, error)`
+ `(h *Helper) Delete(model interface{}, where interface{}, object interface{}) (xstatus.DbStatus, error)`
+ `QueryErr(rdb *gorm.DB) (bool, error)`
+ `CreateDB(rdb *gorm.DB) (xstatus.DbStatus, error)`
+ `UpdateDB(rdb *gorm.DB) (xstatus.DbStatus, error)`
+ `DeleteDB(rdb *gorm.DB) (xstatus.DbStatus, error)`
24 changes: 18 additions & 6 deletions xgorm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,40 @@ func (h *Helper) Exist(model interface{}, where interface{}) (bool, error) {
return cnt > 0, nil
}

// Through db.Model(model).Create(object).
func (h *Helper) Create(model interface{}, object interface{}) (xstatus.DbStatus, error) {
rdb := h.db.Model(model).Create(object)
return CreateDB(rdb)
return CreateErr(rdb)
}

// Through db.Model(model).Where(where).Update(object).
func (h *Helper) Update(model interface{}, where interface{}, object interface{}) (xstatus.DbStatus, error) {
if where == nil {
where = object
}
rdb := h.db.Model(model).Where(where).Update(object)
return UpdateDB(rdb)
return UpdateErr(rdb)
}

// Through db.Model(model).Where(where).Delete(object).
func (h *Helper) Delete(model interface{}, where interface{}, object interface{}) (xstatus.DbStatus, error) {
if where == nil {
where = object
}
rdb := h.db.Model(model).Where(where).Delete(object)
return DeleteDB(rdb)
return DeleteErr(rdb)
}

func CreateDB(rdb *gorm.DB) (xstatus.DbStatus, error) {
func QueryErr(rdb *gorm.DB) (bool, error) {
if rdb.RecordNotFound() {
return false, nil
} else if rdb.Error != nil {
return false, rdb.Error
}
return true, nil
}

func CreateErr(rdb *gorm.DB) (xstatus.DbStatus, error) {
if IsMySqlDuplicateEntryError(rdb.Error) {
return xstatus.DbExisted, nil
} else if rdb.Error != nil || rdb.RowsAffected == 0 {
Expand All @@ -62,7 +74,7 @@ func CreateDB(rdb *gorm.DB) (xstatus.DbStatus, error) {
return xstatus.DbSuccess, nil
}

func UpdateDB(rdb *gorm.DB) (xstatus.DbStatus, error) {
func UpdateErr(rdb *gorm.DB) (xstatus.DbStatus, error) {
if IsMySqlDuplicateEntryError(rdb.Error) {
return xstatus.DbExisted, nil
} else if rdb.Error != nil {
Expand All @@ -74,7 +86,7 @@ func UpdateDB(rdb *gorm.DB) (xstatus.DbStatus, error) {
return xstatus.DbSuccess, nil
}

func DeleteDB(rdb *gorm.DB) (xstatus.DbStatus, error) {
func DeleteErr(rdb *gorm.DB) (xstatus.DbStatus, error) {
if rdb.Error != nil {
return xstatus.DbFailed, rdb.Error
} else if rdb.RowsAffected == 0 {
Expand Down
2 changes: 1 addition & 1 deletion xneo4j/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func GetDuration(data interface{}) neo4j.Duration {
// Setup cypher orderBy's parent, index starts from 1.
type OrderByPair map[string]int

// Apply cypher orderBy string through PropertyDict and OrderByPair (using parent pair).
// Apply cypher orderBy string through xproperty.PropertyDict and xneo4j.OrderByPair (using parent pair).
func ApplyCypherOrderBy(p xproperty.PropertyDict, pairs OrderByPair) func(source string, parents ...string) string {
return func(source string, parents ...string) string {
result := make([]string, 0)
Expand Down
2 changes: 0 additions & 2 deletions xstatus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
+ `type JwtStatus int8`
+ `JwtSuccess`
+ `JwtExpired`
+ `JwtNotIssued`
+ `JwtNotValid`
+ `JwtIssuer`
+ `JwtSubject`
+ `JwtAudience`
+ `JwtInvalid`
+ `JwtUserErr`
+ `JwtFailed`
Expand Down
35 changes: 16 additions & 19 deletions xstatus/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package xstatus
type JwtStatus int8

const (
JwtSuccess JwtStatus = iota // success
JwtExpired // exp, ExpiresAt
JwtNotIssued // iat, IssuedAt
JwtNotValid // nbf, NotBefore
JwtIssuer // iss, Issuer
JwtSubject // sub, Subject
JwtAudience // aud, Audience
JwtInvalid // generic
JwtBlank // blank
JwtNotFound // not found
JwtUserErr // user
JwtFailed // server error
JwtTagA // tag a
JwtTagB // tag b
JwtTagC // tag c
JwtSuccess JwtStatus = iota // success
JwtExpired // exp, ExpiresAt
JwtNotValid // nbf, NotBefore
JwtIssuer // iss, Issuer
JwtSubject // sub, Subject
JwtInvalid // generic
JwtBlank // blank
JwtNotFound // not found
JwtUserErr // user
JwtFailed // server error
JwtTagA // tag a
JwtTagB // tag b
JwtTagC // tag c

// JwtNotIssued // iat, IssuedAt
// JwtAudience // aud, Audience
)

func (j JwtStatus) String() string {
Expand All @@ -26,16 +27,12 @@ func (j JwtStatus) String() string {
return "jwt-success"
case JwtExpired:
return "jwt-expired"
case JwtNotIssued:
return "jwt-not-issued"
case JwtNotValid:
return "jwt-not-valid"
case JwtIssuer:
return "jwt-issuer"
case JwtSubject:
return "jwt-subject"
case JwtAudience:
return "jwt-audience"
case JwtInvalid:
return "jwt-invalid"
case JwtBlank:
Expand Down
3 changes: 1 addition & 2 deletions xstatus/xstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ func TestFsmStatus(t *testing.T) {
func TestJwtStatus(t *testing.T) {
xtesting.Equal(t, JwtSuccess.String(), "jwt-success")
xtesting.Equal(t, JwtExpired.String(), "jwt-expired")
xtesting.Equal(t, JwtNotIssued.String(), "jwt-not-issued")
xtesting.Equal(t, JwtNotValid.String(), "jwt-not-valid")
xtesting.Equal(t, JwtIssuer.String(), "jwt-issuer")
xtesting.Equal(t, JwtSubject.String(), "jwt-subject")
xtesting.Equal(t, JwtAudience.String(), "jwt-audience")
xtesting.Equal(t, JwtInvalid.String(), "jwt-invalid")
xtesting.Equal(t, JwtBlank.String(), "jwt-blank")
xtesting.Equal(t, JwtNotFound.String(), "jwt-not-found")
Expand All @@ -44,4 +42,5 @@ func TestJwtStatus(t *testing.T) {
xtesting.Equal(t, JwtTagA.String(), "jwt-tag-a")
xtesting.Equal(t, JwtTagB.String(), "jwt-tag-b")
xtesting.Equal(t, JwtTagC.String(), "jwt-tag-c")
xtesting.Equal(t, JwtStatus(20).String(), "jwt-?")
}

0 comments on commit 765bad4

Please sign in to comment.