Skip to content

Commit

Permalink
chore: release v0.9.5 (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangmingLuo authored Jan 3, 2025
2 parents ac920f3 + 2a22913 commit dcda12a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 13 deletions.
4 changes: 4 additions & 0 deletions cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func Init() *cli.App {
handlerByMethod := cli.BoolFlag{Name: "handler_by_method", Usage: "Generate a separate handler file for each method.", Destination: &globalArgs.HandlerByMethod}
trimGoPackage := cli.StringFlag{Name: "trim_gopackage", Aliases: []string{"trim_pkg"}, Usage: "Trim the prefix of go_package for protobuf.", Destination: &globalArgs.TrimGoPackage}

// client flag
enableClientOptionalFlag := cli.BoolFlag{Name: "enable_optional", Usage: "Optional field do not transfer for thrift if not set.(Only works for query tag)", Destination: &globalArgs.EnableClientOptional}

// app
app := cli.NewApp()
app.Name = "hz"
Expand Down Expand Up @@ -327,6 +330,7 @@ func Init() *cli.App {
&trimGoPackage,

&jsonEnumStrFlag,
&enableClientOptionalFlag,
&queryEnumIntFlag,
&unsetOmitemptyFlag,
&protoCamelJSONTag,
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Argument struct {
EnableExtends bool
SortRouter bool

// client flag
EnableClientOptional bool

CustomizeLayout string
CustomizeLayoutData string
CustomizePackage string
Expand Down
6 changes: 6 additions & 0 deletions cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,13 @@ func (r *request) addHeaders(params map[string]string) *request {
func (r *request) setQueryParam(param string, value interface{}) *request {
if value == nil {
return r
}
v := reflect.ValueOf(value)
if v.Kind() == reflect.Pointer && v.IsNil() {
return r
}
switch v.Kind() {
case reflect.Slice, reflect.Array:
for index := 0; index < v.Len(); index++ {
Expand Down
20 changes: 16 additions & 4 deletions cmd/hz/thrift/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument)
if err != nil {
return nil, err
}
err = parseAnnotationToClient(clientMethod, m.Arguments[0].GetType(), rt)
err = parseAnnotationToClient(clientMethod, m.Arguments[0].GetType(), rt, args.EnableClientOptional)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func newHTTPMethod(s *parser.Service, m *parser.Function, method *generator.Http
return &newMethod, nil
}

func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Type, symbol ResolvedSymbol) error {
func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Type, symbol ResolvedSymbol, enableOptional bool) error {
if p == nil {
return fmt.Errorf("get type failed for parse annotatoon to client")
}
Expand All @@ -270,13 +270,21 @@ func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Typ
for _, field := range st.Fields() {
hasAnnotation := false
isStringFieldType := false
isOptional := false
if field.GetType().String() == "string" {
isStringFieldType = true
}
if field.GetRequiredness() == parser.FieldType_Optional {
isOptional = true
}
if anno := getAnnotation(field.Annotations, AnnotationQuery); len(anno) > 0 {
hasAnnotation = true
query := checkSnakeName(anno[0])
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", query, field.GoName().String())
if isOptional && enableOptional {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: func() interface{} {\n\t\t\t\tif req.IsSet%s() {\n\t\t\t\t\treturn req.Get%s()\n\t\t\t\t} else {\n\t\t\t\t\treturn nil\n\t\t\t\t}}(),\n", query, field.GoName().String(), field.GoName().String())
} else {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", query, field.GoName().String())
}
}

if anno := getAnnotation(field.Annotations, AnnotationPath); len(anno) > 0 {
Expand Down Expand Up @@ -326,7 +334,11 @@ func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Typ
// cookie do nothing
}
if !hasAnnotation && strings.EqualFold(clientMethod.HTTPMethod, "get") {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", checkSnakeName(field.GetName()), field.GoName().String())
if isOptional && enableOptional {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: func() interface{} {\n\t\t\t\tif req.IsSet%s() {\n\t\t\t\t\treturn req.Get%s()\n\t\t\t\t} else {\n\t\t\t\t\treturn nil\n\t\t\t\t}}(),\n", checkSnakeName(field.GetName()), field.GoName().String(), field.GoName().String())
} else {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", checkSnakeName(field.GetName()), field.GoName().String())
}
}
}
clientMethod.BodyParamsCode = meta.SetBodyParam
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ func (ctx *RequestContext) Body() ([]byte, error) {
return ctx.Request.BodyE()
}

// ClientIP tries to parse the headers in [X-Real-Ip, X-Forwarded-For].
// ClientIP attempts to parse the headers in the order of [X-Forwarded-For, X-Real-IP].
// It calls RemoteIP() under the hood. If it cannot satisfy the requirements,
// use engine.SetClientIPFunc to inject your own implementation.
func (ctx *RequestContext) ClientIP() string {
Expand Down
6 changes: 4 additions & 2 deletions pkg/common/test/mock/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ func (m *Conn) AddCloseCallback(callback netpoll.CloseCallback) error {
}

type StreamConn struct {
Data []byte
HasReleased bool
Data []byte
}

func NewStreamConn() *StreamConn {
Expand Down Expand Up @@ -354,7 +355,8 @@ func (m *StreamConn) Skip(n int) error {
}

func (m *StreamConn) Release() error {
panic("implement me")
m.HasReleased = true
return nil
}

func (m *StreamConn) Len() int {
Expand Down
6 changes: 3 additions & 3 deletions pkg/common/test/mock/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func TestStreamConn(t *testing.T) {
assert.DeepEqual(t, cap(conn.Data), conn.Len())
err = conn.Skip(conn.Len() + 1)
assert.DeepEqual(t, "not enough data", err.Error())
err = conn.Release()
assert.DeepEqual(t, nil, err)
assert.DeepEqual(t, true, conn.HasReleased)
})

t.Run("TestNotImplement", func(t *testing.T) {
conn := NewStreamConn()
assert.Panic(t, func() {
conn.Release()
})
assert.Panic(t, func() {
conn.ReadByte()
})
Expand Down
1 change: 0 additions & 1 deletion pkg/protocol/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type MockDoer struct {
}

func (m *MockDoer) Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error {

// this is the real logic in (c *HostClient) doNonNilReqResp method
if len(req.Header.Host()) == 0 {
req.Header.SetHostBytes(req.URI().Host())
Expand Down
16 changes: 15 additions & 1 deletion pkg/protocol/http1/ext/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (rs *bodyStream) skipRest() error {
if err != nil {
return err
}
// After Skip, the buffer needs to be released to prevent OOM if there are too much data on conn.
err = rs.reader.Release()
if err != nil {
return err
}

}
}
// max value of pSize is 8193, it's safe.
Expand Down Expand Up @@ -300,7 +306,15 @@ func (rs *bodyStream) skipRest() error {
if skip > needSkipLen {
skip = needSkipLen
}
rs.reader.Skip(skip)
err := rs.reader.Skip(skip)
if err != nil {
return err
}
// After Skip, the buffer needs to be released to prevent OOM if there are too much data on conn.
err = rs.reader.Release()
if err != nil {
return err
}
needSkipLen -= skip
if needSkipLen == 0 {
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/protocol/http1/req/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ func TestStreamNotEnoughData(t *testing.T) {
err = ext.ReleaseBodyStream(req.BodyStream())
assert.Nil(t, err)
assert.DeepEqual(t, 0, len(conn.Data))
assert.DeepEqual(t, true, conn.HasReleased)
}

func TestRequestBodyStreamWithTrailer(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ package hertz
// Name and Version info of this framework, used for statistics and debug
const (
Name = "Hertz"
Version = "v0.9.4"
Version = "v0.9.5"
)

0 comments on commit dcda12a

Please sign in to comment.