From 0bf41c5a509da10f3beaf791a896f3cf958927c0 Mon Sep 17 00:00:00 2001 From: "yuxuan.wang1" Date: Fri, 20 Dec 2024 14:15:42 +0800 Subject: [PATCH] feat: expose Package for pb Service Descriptor --- conv/j2p/conv_test.go | 5 +++-- proto/descriptor.go | 5 +++++ proto/idl.go | 8 +++++--- proto/idl_test.go | 10 ++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/conv/j2p/conv_test.go b/conv/j2p/conv_test.go index bf38e826..99f895e4 100644 --- a/conv/j2p/conv_test.go +++ b/conv/j2p/conv_test.go @@ -14,13 +14,14 @@ import ( "time" "github.com/bytedance/sonic/ast" + "github.com/stretchr/testify/require" + goprotowire "google.golang.org/protobuf/encoding/protowire" + "github.com/cloudwego/dynamicgo/conv" "github.com/cloudwego/dynamicgo/internal/util_test" "github.com/cloudwego/dynamicgo/proto" "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/base" "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" - "github.com/stretchr/testify/require" - goprotowire "google.golang.org/protobuf/encoding/protowire" ) var ( diff --git a/proto/descriptor.go b/proto/descriptor.go index 6d6e2104..9b4acd6e 100644 --- a/proto/descriptor.go +++ b/proto/descriptor.go @@ -171,6 +171,7 @@ type ServiceDescriptor struct { serviceName string methods map[string]*MethodDescriptor isCombinedServices bool + packageName string } func (s *ServiceDescriptor) Name() string { @@ -188,3 +189,7 @@ func (s *ServiceDescriptor) LookupMethodByName(name string) *MethodDescriptor { func (s *ServiceDescriptor) IsCombinedServices() bool { return s.isCombinedServices } + +func (s *ServiceDescriptor) PackageName() string { + return s.packageName +} diff --git a/proto/idl.go b/proto/idl.go index 6ba7f1b7..ff626f05 100644 --- a/proto/idl.go +++ b/proto/idl.go @@ -6,10 +6,11 @@ import ( "math" "unsafe" - "github.com/cloudwego/dynamicgo/internal/util" - "github.com/cloudwego/dynamicgo/meta" "github.com/jhump/protoreflect/desc" "github.com/jhump/protoreflect/desc/protoparse" + + "github.com/cloudwego/dynamicgo/internal/util" + "github.com/cloudwego/dynamicgo/meta" ) type compilingInstance struct { @@ -118,7 +119,8 @@ func parse(ctx context.Context, fileDesc *desc.FileDescriptor, mode meta.ParseSe } sDsc := &ServiceDescriptor{ - methods: map[string]*MethodDescriptor{}, + methods: map[string]*MethodDescriptor{}, + packageName: fileDesc.GetPackage(), } structsCache := compilingCache{} diff --git a/proto/idl_test.go b/proto/idl_test.go index 63cf0792..668c0c9b 100644 --- a/proto/idl_test.go +++ b/proto/idl_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" + "github.com/cloudwego/dynamicgo/meta" ) @@ -108,3 +110,11 @@ func TestIsCombinedServices(t *testing.T) { } fmt.Printf("%#v\n", svc) } + +func TestParsePackageName(t *testing.T) { + opts := Options{} + importDirs := []string{"../testdata/idl/"} + svc, err := opts.NewDescriptorFromPath(context.Background(), "basic_example.proto", importDirs...) + require.Nil(t, err, err) + require.Equal(t, "pb3", svc.PackageName(), svc.PackageName()) +}