diff --git a/httpdata/.golangci.yaml b/httpdata/.golangci.yaml new file mode 100644 index 0000000..d884b2f --- /dev/null +++ b/httpdata/.golangci.yaml @@ -0,0 +1,141 @@ +## 基于 golangci-lint@v1.52.2 +run: + timeout: 1m + build-tags: [ ] + skip-dirs: [ ] + skip-files: [ ] +linters: + disable-all: true + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - cyclop + - dupl + - durationcheck + - errname + - errorlint + - exhaustive + - exportloopref + - funlen + - gocheckcompilerdirectives + - gochecknoinits + - goconst + - gocritic + - gocyclo + - goimports + - gomnd + - goprintffuncname + - gosec + - lll + - loggercheck + - makezero + - nakedret + - nestif + - nilnil + - noctx + - nolintlint + - prealloc + - predeclared + - promlinter + - reassign + - revive + - rowserrcheck + - stylecheck + - tenv + - testableexamples + - testpackage + - tparallel + - unconvert + - unparam + - usestdlibvars + - wastedassign + - whitespace +linters-settings: + errcheck: + check-type-assertions: true + exclude-functions: [ ] + govet: + enable-all: true + disable: [ ] + cyclop: + max-complexity: 10 + package-average: 0.0 + dupl: + threshold: 150 + exhaustive: + check: + - switch + - map + funlen: + lines: 100 + statements: 60 + gocritic: + disabled-checks: + - commentFormatting + settings: + captLocal: + paramsOnly: false + underef: + skipRecvDeref: false + gocyclo: + min-complexity: 20 + gomnd: + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + - prometheus.ExponentialBuckets + - prometheus.ExponentialBucketsRange + - prometheus.LinearBuckets + lll: + line-length: 240 + nakedret: + max-func-lines: 10 + nestif: + min-complexity: 5 + predeclared: + ignore: "" + q: false + reassign: + patterns: + - ".*" + rowserrcheck: + packages: + - github.com/jmoiron/sqlx + tenv: + all: true + usestdlibvars: + time-month: true + time-layout: true + crypto-hash: true + default-rpc-path: true + os-dev-null: true + sql-isolation-level: true + tls-signature-scheme: true + constant-kind: true + syslog-priority: true +issues: + max-same-issues: 10 + exclude-rules: + - source: "//noinspection" + linters: [ gocritic ] + - path: "_test\\.go" + linters: + - bodyclose + - dupl + - funlen + - goconst + - gosec + - noctx diff --git a/httpdata/code.go b/httpdata/code.go new file mode 100644 index 0000000..b158588 --- /dev/null +++ b/httpdata/code.go @@ -0,0 +1,14 @@ +package httpdata + +type Code string + +const ( + CodeOK Code = "ok" + CodeError Code = "error" + CodeBadRequest Code = "bad_request" + CodeInternal Code = "internal" + CodeInternalUpstream Code = "internal.upstream" + CodeUnexpect Code = "unexpect" + CodeUnexpectPanic Code = "unexpect.panic" + CodeUnknown Code = "unknown" +) diff --git a/httpdata/data.go b/httpdata/data.go new file mode 100644 index 0000000..03c1358 --- /dev/null +++ b/httpdata/data.go @@ -0,0 +1,22 @@ +package httpdata + +type Response struct { + Data any `json:"data,omitempty"` + Code Code `json:"code"` + Message string `json:"message,omitempty"` +} + +type PageData struct { + List []any `json:"list"` + PageIndex int `json:"pageIndex"` // >=1 + PageSize int `json:"pageSize"` // >=1 + Total int `json:"total"` // maybe 0 +} + +func NewOkResponse(data any) Response { + return Response{ + Code: CodeOK, + Message: "", + Data: data, + } +} diff --git a/httpdata/error.go b/httpdata/error.go new file mode 100644 index 0000000..927fd6c --- /dev/null +++ b/httpdata/error.go @@ -0,0 +1,42 @@ +package httpdata + +import ( + "errors" +) + +type UniverseError struct { + Code Code + Message string +} + +func NewUniverseError(code Code, message string) error { + return UniverseError{ + Code: code, + Message: message, + } +} + +func NewBadRequestError(message string) error { + return UniverseError{ + Code: CodeBadRequest, + Message: message, + } +} + +func (err UniverseError) Error() string { + if err.Message == "" { + return string(err.Code) + } + return string(err.Code) + ": " + err.Message +} + +func IsUniverseError(err error) bool { + return errors.As(err, &UniverseError{}) +} + +func ToUniverseError(err error, code Code, message string) error { + if err == nil || IsUniverseError(err) { + return err + } + return NewUniverseError(code, message) +} diff --git a/httpdata/go.mod b/httpdata/go.mod new file mode 100644 index 0000000..5372031 --- /dev/null +++ b/httpdata/go.mod @@ -0,0 +1,3 @@ +module git.blauwelle.com/go/crate/httpdata + +go 1.21.1