generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2024 CloudWeGo Authors | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package generator | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/protobuf/compiler/protogen" | ||
) | ||
|
||
func generateHessian2Ext(g *generator, gen *protogen.Plugin) error { | ||
for _, file := range gen.Files { | ||
err := g.generateAPIFile(gen, file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, svr := range file.Services { | ||
err := g.generateServiceExt(gen, svr, file) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// generateAPIFile implements dubbo extension interface every message. | ||
// ref: https://github.com/kitex-contrib/codec-dubbo/blob/dcc4d83669b139d5daa75823d2eda5584d92ebc7/pkg/iface/protocol.go#L22 | ||
func (g *generator) generateAPIFile(gen *protogen.Plugin, file *protogen.File) error { | ||
if len(file.Messages) == 0 { | ||
return nil | ||
} | ||
fileName := filePrefix + file.GeneratedFilenamePrefix + "_" + fileExt | ||
genFile := gen.NewGeneratedFile(fileName, "") | ||
request := &ExtFile{ | ||
Version: version, | ||
PkgName: file.GoPackageName, | ||
} | ||
if file.Proto != nil && file.Proto.Options != nil { | ||
if file.Proto.Options.GoPackage != nil { | ||
request.GoPackage = *file.Proto.Options.GoPackage | ||
} | ||
if file.Proto.Options.JavaPackage != nil { | ||
request.JavaPackage = *file.Proto.Options.JavaPackage | ||
} | ||
} | ||
messages := file.Messages | ||
for len(messages) != 0 { | ||
var nestedMessages []*protogen.Message | ||
for _, message := range messages { | ||
s := &StructLike{ | ||
Name: message.GoIdent.GoName, | ||
GoPackage: request.GoPackage, | ||
JavaPackage: request.JavaPackage, | ||
} | ||
for _, field := range message.Fields { | ||
filed := &Field{ | ||
Name: field.GoName, | ||
} | ||
s.Fields = append(s.Fields, filed) | ||
} | ||
request.StructLikes = append(request.StructLikes, s) | ||
nestedMessages = append(nestedMessages, message.Messages...) | ||
} | ||
messages = nestedMessages | ||
} | ||
|
||
for _, svr := range file.Services { | ||
s := &ExtService{ | ||
Name: svr.GoName, | ||
} | ||
request.Services = append(request.Services, s) | ||
} | ||
|
||
var buf strings.Builder | ||
err := g.tpl.ExecuteTemplate(&buf, "structlikes", request) | ||
if err != nil { | ||
return err | ||
} | ||
genFile.P(buf.String()) | ||
return nil | ||
} | ||
|
||
// generateServiceExt: implements dubbo extension interface for service method args and response | ||
// ref: https://github.com/cloudwego/kitex/blob/8526b3af30fcd321db268cae59a3545a9c6f237f/tool/internal_pkg/generator/generator.go#L392 | ||
func (g *generator) generateServiceExt(gen *protogen.Plugin, svr *protogen.Service, file *protogen.File) error { | ||
fileName := filePrefix + string(file.GoImportPath) + "/" + strings.ToLower(svr.GoName) + "/" + fileExt | ||
genFile := gen.NewGeneratedFile(fileName, "") | ||
rSvr := &ExtService{ | ||
Version: version, | ||
PkgName: strings.ToLower(svr.GoName), | ||
Name: svr.GoName, | ||
} | ||
for _, method := range svr.Methods { | ||
s := &Function{ | ||
Method: method.GoName, | ||
InputType: method.Input.GoIdent.GoName, | ||
OutputType: method.Output.GoIdent.GoName, | ||
} | ||
rSvr.Functions = append(rSvr.Functions, s) | ||
} | ||
var buf strings.Builder | ||
err := g.tpl.ExecuteTemplate(&buf, "service", rSvr) | ||
if err != nil { | ||
return err | ||
} | ||
genFile.P(buf.String()) | ||
return nil | ||
} |
11 changes: 11 additions & 0 deletions
11
tools/protoc-gen-kitex-dubbo/templates/hessian_extension.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.