Skip to content

Commit

Permalink
feat: 添加 Problems.Exists
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 25, 2024
1 parent 043c3ba commit 30a2352
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmd/web/enum/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import(
{{range .Enums}}
//---------------------{{.Name}}------------------------
//--------------------- {{.Name}} ------------------------
var {{.Type2StringMap}} = map[{{.Name}}] string {
{{- range .Values}}
Expand Down Expand Up @@ -119,7 +119,7 @@ var(
)
{{end}}
//---------------------end {{.Name}}--------------------
//--------------------- end {{.Name}} --------------------
{{end}}
`
5 changes: 2 additions & 3 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"slices"
"strings"

"github.com/issue9/localeutil"
"github.com/issue9/mux/v8/header"
"github.com/issue9/sliceutil"
"golang.org/x/text/encoding"
Expand Down Expand Up @@ -177,7 +176,7 @@ func (e *Codec) contentEncoding(name string, r io.Reader) (io.ReadCloser, error)
if c, f := sliceutil.At(e.compressions, func(item *compression, _ int) bool { return item.compressor.Name() == name }); f {
return c.compressor.NewDecoder(r)
}
return nil, localeutil.Error("not found compress for %s", name)
return nil, NewLocaleError("not found compress for %s", name)
}

// 根据客户端的 Accept-Encoding 报头选择是适合的压缩方法
Expand Down Expand Up @@ -282,7 +281,7 @@ func (e *Codec) contentType(h string) (UnmarshalFunc, encoding.Encoding, error)

item := e.searchFunc(func(s string) bool { return s == mimetype })
if item == nil {
return nil, nil, localeutil.Error("not found serialization function for %s", mimetype)
return nil, nil, NewLocaleError("not found serialization function for %s", mimetype)
}

if charset == "" || charset == header.UTF8 {
Expand Down
5 changes: 2 additions & 3 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"

"github.com/issue9/assert/v4"
"github.com/issue9/localeutil"
"github.com/issue9/mux/v8/header"

"github.com/issue9/web/compressor"
Expand Down Expand Up @@ -182,14 +181,14 @@ func TestCodec_contentType(t *testing.T) {

mt := NewCodec()
a.NotNil(mt)
mt.AddMimetype("application/octet-stream", marshalJSON, unmarshalJSON, "")
mt.AddMimetype(header.OctetStream, marshalJSON, unmarshalJSON, "")

f, e, err := mt.contentType(";;;")
a.Error(err).Nil(f).Nil(e)

// 不存在的 mimetype
f, e, err = mt.contentType("not-exists; charset=utf-8")
a.Equal(err, localeutil.Error("not found serialization function for %s", "not-exists")).Nil(f).Nil(e)
a.Equal(err, NewLocaleError("not found serialization function for %s", "not-exists")).Nil(f).Nil(e)

// charset=utf-8
f, e, err = mt.contentType(qheader.BuildContentType(header.OctetStream, header.UTF8))
Expand Down
2 changes: 1 addition & 1 deletion internal/qheader/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ func BenchmarkBuildContentType(b *testing.B) {
a := assert.New(b, false)

for range b.N {
a.Equal(BuildContentType("application/json", header.UTF8), "application/json; charset=utf-8")
a.Equal(BuildContentType(header.JSON, header.UTF8), "application/json; charset=utf-8")
}
}
2 changes: 1 addition & 1 deletion internal/qheader/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func ClientIP(r *http.Request) string {
ip = r.RemoteAddr
}
if ip == "" {
ip = r.Header.Get("X-Real-IP")
ip = r.Header.Get(header.XRealIP)
}

return strings.TrimSpace(ip)
Expand Down
7 changes: 4 additions & 3 deletions mimetype/jsonp/jsonp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/issue9/assert/v4"
"github.com/issue9/mux/v8/header"

"github.com/issue9/web"
"github.com/issue9/web/server"
Expand All @@ -31,14 +32,14 @@ func TestJSONP(t *testing.T) {
defer servertest.Run(a, s)()
defer s.Close(0)

servertest.Get(a, "http://localhost:8080/jsonp").Header("accept", Mimetype).Do(nil).
servertest.Get(a, "http://localhost:8080/jsonp").Header(header.Accept, Mimetype).Do(nil).
Status(http.StatusBadRequest).
BodyEmpty()

servertest.Get(a, "http://localhost:8080/jsonp?callback=cb").Header("accept", Mimetype).Do(nil).
servertest.Get(a, "http://localhost:8080/jsonp?callback=cb").Header(header.Accept, Mimetype).Do(nil).
StringBody(`cb("jsonp")`)

servertest.Get(a, "http://localhost:8080/jsonp?cb=cb").Header("accept", Mimetype).Do(nil).
servertest.Get(a, "http://localhost:8080/jsonp?cb=cb").Header(header.Accept, Mimetype).Do(nil).
Status(http.StatusBadRequest).
BodyEmpty()
}
5 changes: 3 additions & 2 deletions problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (ps *Problems) Add(s int, p ...*LocaleProblem) *Problems {
}

for _, pp := range p {
if ps.exists(pp.ID) {
if ps.Exists(pp.ID) {
panic(fmt.Sprintf("存在相同值的 id 参数 %s", pp.ID))
}

Expand All @@ -223,7 +223,8 @@ func (ps *Problems) Add(s int, p ...*LocaleProblem) *Problems {
return ps
}

func (ps *Problems) exists(id string) bool {
// Exists 查看指定 id 是否已经存在
func (ps *Problems) Exists(id string) bool {
return slices.IndexFunc(ps.problems, func(p *LocaleProblem) bool { return p.ID == id }) > -1
}

Expand Down
10 changes: 5 additions & 5 deletions problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ func TestProblems_Add(t *testing.T) {
a.NotNil(ps)
l := len(ps.problems)

a.False(ps.exists("40010")).
False(ps.exists("40011")).
True(ps.exists(ProblemNotFound))
a.False(ps.Exists("40010")).
False(ps.Exists("40011")).
True(ps.Exists(ProblemNotFound))

ps.Add(400, []*LocaleProblem{
{ID: "40010", Title: Phrase("title"), Detail: Phrase("detail")},
{ID: "40011", Title: Phrase("title"), Detail: Phrase("detail")},
}...)
a.True(ps.exists("40010")).
True(ps.exists("40011")).
a.True(ps.Exists("40010")).
True(ps.Exists("40011")).
Equal(l+2, len(ps.problems))

a.PanicString(func() {
Expand Down
8 changes: 1 addition & 7 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package web

import (
"io"
"time"

"github.com/issue9/cache"
"github.com/issue9/config"
Expand Down Expand Up @@ -58,18 +57,13 @@ type (

// UnmarshalFunc 反序列化函数原型
//
// 参数 [io.Reader] 必定不会为空。
// NOTE: 参数 [io.Reader] 必定不会为空。
UnmarshalFunc = func(io.Reader, any) error
)

// NewCache 声明带有统一前缀的缓存接口
func NewCache(prefix string, c Cache) Cache { return cache.Prefix(c, prefix) }

// GetCacheItemOrInit 获取缓存 c 的项如果不存在则尝试用 init 的返回值进行初始化
func GetCacheItemOrInit[T any](c Cache, key string, v *T, ttl time.Duration, init func() (T, error)) error {
return cache.GetOrInit(c, key, v, ttl, init)
}

// Phrase 生成本地化的语言片段
func Phrase(key string, v ...any) LocaleStringer { return localeutil.Phrase(key, v...) }

Expand Down

0 comments on commit 30a2352

Please sign in to comment.