Skip to content

Commit

Permalink
chore: update context max length validating
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 26, 2023
1 parent 1597f32 commit deb3d2a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/bll/writing_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (i *CreateCollectionInput) Validate() error {
return gear.ErrBadRequest.From(err)
}

if i.Context != "" {
if tk := util.Tiktokens(i.Context); tk > 2048 {
return gear.ErrBadRequest.WithMsgf("context is too long, max tokens is 2048, got %d", tk)
}
}

return nil
}

Expand Down Expand Up @@ -139,6 +145,12 @@ func (i *UpdateCollectionInput) Validate() error {
return gear.ErrBadRequest.From(err)
}

if i.Context != nil {
if tk := util.Tiktokens(*i.Context); tk > 2048 {
return gear.ErrBadRequest.WithMsgf("context is too long, max tokens is 2048, got %d", tk)
}
}

return nil
}

Expand Down
11 changes: 11 additions & 0 deletions src/bll/writing_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ func (i *CreateMessageInput) Validate() error {
return gear.ErrBadRequest.WithMsg("message is too large")
}

if i.Context != "" {
if tk := util.Tiktokens(i.Context); tk > 2048 {
return gear.ErrBadRequest.WithMsgf("context is too long, max tokens is 2048, got %d", tk)
}
}

return nil
}

Expand Down Expand Up @@ -229,6 +235,11 @@ func (i *UpdateMessageInput) Validate() error {
if i.Message != nil && i.Language == nil {
return gear.ErrBadRequest.WithMsg("language is required with message")
}
if i.Context != nil {
if tk := util.Tiktokens(*i.Context); tk > 2048 {
return gear.ErrBadRequest.WithMsgf("context is too long, max tokens is 2048, got %d", tk)
}
}
if i.NewlyAdd == nil {
i.NewlyAdd = util.Ptr(true)
}
Expand Down
5 changes: 5 additions & 0 deletions src/bll/writing_publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (i *CreatePublicationInput) Validate() error {
if err := util.Validator.Struct(i); err != nil {
return gear.ErrBadRequest.From(err)
}
if i.Context != nil {
if tk := util.Tiktokens(*i.Context); tk > 2048 {
return gear.ErrBadRequest.WithMsgf("context is too long, max tokens is 2048, got %d", tk)
}
}

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRemoveDuplicates(t *testing.T) {
assert.Equal(t, RemoveDuplicates([]string{"a", "b", "a"}), []string{"a", "b"})

id := NewID()
id2 := mustParseID(id.String())
id2 := MustParseID(id.String())
assert.Equal(t, RemoveDuplicates([]ID{id, id2}), []ID{id})
}

Expand Down
8 changes: 4 additions & 4 deletions src/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func RequestCBOR(ctx context.Context, cli *http.Client, method, api string, inpu
if e != nil {
str = string(data)
}
er := gear.Err.WithCode(resp.StatusCode).WithMsgf("RequestCBOR failed, url: %q, rid: %s, code: %d, error: %v",
api, rid, resp.StatusCode, err)
er := gear.Err.WithCode(resp.StatusCode).WithMsgf("RequestCBOR failed, url: %q, rid: %s, code: %d, error: %v, body: %s",
api, rid, resp.StatusCode, err, str)
er.Data = str
return er
}
Expand All @@ -199,8 +199,8 @@ func RequestCBOR(ctx context.Context, cli *http.Client, method, api string, inpu
if e != nil {
str = string(data)
}
er := gear.ErrInternalServerError.WithMsgf("RequestCBOR failed, url: %q, rid: %s, code: %d, error: %v",
api, rid, resp.StatusCode, err)
er := gear.ErrInternalServerError.WithMsgf("RequestCBOR failed, url: %q, rid: %s, code: %d, error: %v, body: %s",
api, rid, resp.StatusCode, err, str)
er.Data = str
return er
}
Expand Down
6 changes: 3 additions & 3 deletions src/util/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

var ZeroID ID
var JARVIS ID = mustParseID("0000000000000jarvis0") // system user
var ANON ID = mustParseID("000000000000000anon0") // anonymous user
var JARVIS ID = MustParseID("0000000000000jarvis0") // system user
var ANON ID = MustParseID("000000000000000anon0") // anonymous user
var MinID ID = ID(xid.ID([12]byte{0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255}))

func NewID() ID {
Expand All @@ -28,7 +28,7 @@ func ParseID(s string) (ID, error) {
return ID(id), nil
}

func mustParseID(s string) ID {
func MustParseID(s string) ID {
id, err := xid.FromString(s)
if err != nil {
panic(err)
Expand Down

0 comments on commit deb3d2a

Please sign in to comment.