Skip to content

Commit

Permalink
chore: release v0.6.8 (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever authored Sep 1, 2023
2 parents 0db9f4f + 67901ad commit cb56959
Show file tree
Hide file tree
Showing 31 changed files with 1,316 additions and 89 deletions.
11 changes: 6 additions & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ The description of the title will be attached in Release Notes,
so please describe it from user-oriented, what this PR does / why we need it.
Please check your PR title with the below requirements:
-->
- [ ] This PR title match the format: `<type>(optional scope): <description>`.
- [ ] This PR title match the format: \<type\>(optional scope): \<description\>
- [ ] The description of this PR title is user-oriented and clear enough for others to understand.
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. [User docs repo](https://github.com/cloudwego/cloudwego.github.io).
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)


#### (Optional) Translate the PR title into Chinese.


#### (Optional) More detail description for this PR(en: English/zh: Chinese).
#### (Optional) More detailed description for this PR(en: English/zh: Chinese).
<!--
Provide more detailed info for review. If it is a perf type PR, perf data is suggested to give.
Provide more detailed info for review(e.g., it's recommended to provide perf data if this is a perf type PR).
-->
en:
zh(optional):
Expand All @@ -44,4 +45,4 @@ Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
#### (Optional) The PR that updates user documentation:
<!--
If the current PR requires user awareness at the usage level, please submit a PR to update user docs. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)
-->
-->
16 changes: 14 additions & 2 deletions cmd/hz/generator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type HttpMethod struct {
RefPackage string // handler import dir
RefPackageAlias string // handler import alias
ModelPackage map[string]string
GenHandler bool // Whether to generate one handler, when an idl interface corresponds to multiple http method
// Annotations map[string]string
Models map[string]*model.Model
}
Expand Down Expand Up @@ -78,8 +79,10 @@ func (pkgGen *HttpPackageGenerator) genHandler(pkg *HttpPackage, handlerDir, han
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
}

if err := pkgGen.updateHandler(handler, handlerTplName, handler.FilePath, false); err != nil {
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
if m.GenHandler {
if err := pkgGen.updateHandler(handler, handlerTplName, handler.FilePath, false); err != nil {
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
}
}
}
} else { // generate handler service
Expand All @@ -105,6 +108,15 @@ func (pkgGen *HttpPackageGenerator) genHandler(pkg *HttpPackage, handlerDir, han
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
}

// Avoid generating duplicate handlers when IDL interface corresponds to multiple http methods
methods := handler.Methods
handler.Methods = []*HttpMethod{}
for _, m := range methods {
if m.GenHandler {
handler.Methods = append(handler.Methods, m)
}
}

if err := pkgGen.updateHandler(handler, handlerTplName, handler.FilePath, false); err != nil {
return fmt.Errorf("generate handler %s failed, err: %v", handler.FilePath, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package meta
import "runtime"

// Version hz version
const Version = "v0.6.6"
const Version = "v0.6.7"

const DefaultServiceName = "hertz_service"

Expand Down
52 changes: 44 additions & 8 deletions cmd/hz/protobuf/api/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/hz/protobuf/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extend google.protobuf.ServiceOptions {

// 50731~50760 used to extend service option by hz
optional string base_domain_compatible = 50731;
optional string service_path = 50732;
}

extend google.protobuf.MessageOptions {
Expand Down
38 changes: 33 additions & 5 deletions cmd/hz/protobuf/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package protobuf
import (
"fmt"
"path/filepath"
"sort"
"strings"

"github.com/cloudwego/hertz/cmd/hz/generator"
Expand Down Expand Up @@ -129,19 +130,35 @@ func astToService(ast *descriptorpb.FileDescriptorProto, resolver *Resolver, cmd
ms := s.GetMethod()
methods := make([]*generator.HttpMethod, 0, len(ms))
clientMethods := make([]*generator.ClientMethod, 0, len(ms))
servicePathAnno := checkFirstOption(api.E_ServicePath, s.GetOptions())
servicePath := ""
if val, ok := servicePathAnno.(string); ok {
servicePath = val
}
for _, m := range ms {
hmethod, vpath := checkFirstOptions(HttpMethodOptions, m.GetOptions())
if hmethod == "" {
rs := getAllOptions(HttpMethodOptions, m.GetOptions())
if len(rs) == 0 {
continue
}
path := vpath.(string)
httpOpts := httpOptions{}
for k, v := range rs {
httpOpts = append(httpOpts, httpOption{
method: k,
path: v.(string),
})
}
// turn the map into a slice and sort it to make sure getting the results in the same order every time
sort.Sort(httpOpts)

var handlerOutDir string
genPath := getCompatibleAnnotation(m.GetOptions(), api.E_HandlerPath, api.E_HandlerPathCompatible)
handlerOutDir, ok := genPath.(string)
if !ok || len(handlerOutDir) == 0 {
handlerOutDir = ""
}
if len(handlerOutDir) == 0 {
handlerOutDir = servicePath
}

// protoGoInfo can get generated "Go Info" for proto file.
// the type name may be different between "***.proto" and "***.pb.go"
Expand Down Expand Up @@ -181,10 +198,11 @@ func astToService(ast *descriptorpb.FileDescriptorProto, resolver *Resolver, cmd

method := &generator.HttpMethod{
Name: util.CamelString(m.GetName()),
HTTPMethod: hmethod,
Path: path,
HTTPMethod: httpOpts[0].method,
Path: httpOpts[0].path,
Serializer: serializer,
OutputDir: handlerOutDir,
GenHandler: true,
}

goOptMapAlias := make(map[string]string, 1)
Expand Down Expand Up @@ -223,6 +241,16 @@ func astToService(ast *descriptorpb.FileDescriptorProto, resolver *Resolver, cmd
method.ReturnTypePackage = respPackage

methods = append(methods, method)
for idx, anno := range httpOpts {
if idx == 0 {
continue
}
tmp := *method
tmp.HTTPMethod = anno.method
tmp.Path = anno.path
tmp.GenHandler = false
methods = append(methods, &tmp)
}

if cmdType == meta.CmdClient {
clientMethod := &generator.ClientMethod{}
Expand Down
32 changes: 32 additions & 0 deletions cmd/hz/protobuf/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ var (
SerializerOptions = map[*protoimpl.ExtensionInfo]string{api.E_Serializer: "serializer"}
)

type httpOption struct {
method string
path string
}

type httpOptions []httpOption

func (s httpOptions) Len() int {
return len(s)
}

func (s httpOptions) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s httpOptions) Less(i, j int) bool {
return s[i].method < s[j].method
}

func getAllOptions(extensions map[*protoimpl.ExtensionInfo]string, opts ...protoreflect.ProtoMessage) map[string]interface{} {
out := map[string]interface{}{}
for _, opt := range opts {
for e, t := range extensions {
if proto.HasExtension(opt, e) {
v := proto.GetExtension(opt, e)
out[t] = v
}
}
}
return out
}

func checkFirstOptions(extensions map[*protoimpl.ExtensionInfo]string, opts ...protoreflect.ProtoMessage) (string, interface{}) {
for _, opt := range opts {
for e, t := range extensions {
Expand Down
Loading

0 comments on commit cb56959

Please sign in to comment.