Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:(generic) jsonpb using dynamicgo support parse IDL from memory #1359

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/generic/pbidl_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,41 @@ func NewPbFileProviderWithDynamicGo(main string, ctx context.Context, options dp
return p, nil
}

// NewPbContentProviderWithDynamicGo creates PbFileProviderWithDynamicGo from memory.
// NOTICE: mainPath is used to store mainContent in includes, thus it MUST NOT conflict with original includes
func NewPbContentProviderWithDynamicGo(ctx context.Context, options dproto.Options, mainPath, mainContent string, includes map[string]string) (PbDescriptorProviderDynamicGo, error) {
p := &PbFileProviderWithDynamicGo{
svcs: make(chan *dproto.ServiceDescriptor, 1),
}

sd, err := options.NewDesccriptorFromContent(ctx, mainPath, mainContent, includes)
if err != nil {
return nil, err
}
p.svcs <- sd

return p, nil
}

func (p *PbFileProviderWithDynamicGo) UpdateIDL(ctx context.Context, options dproto.Options, mainPath, mainContent string, includes map[string]string) error {
sd, err := options.NewDesccriptorFromContent(ctx, mainPath, mainContent, includes)
if err != nil {
return err
}

select {
case <-p.svcs:
default:
}

select {
case p.svcs <- sd:
default:
}

return nil
}

func (p *PbFileProviderWithDynamicGo) Provide() <-chan *dproto.ServiceDescriptor {
return p.svcs
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/generic/pbidl_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package generic

import (
"context"
"os"
"testing"

dproto "github.com/cloudwego/dynamicgo/proto"
Expand Down Expand Up @@ -72,3 +73,27 @@ func TestPbFileProviderWithDynamicGo(t *testing.T) {
svcDsc := <-p.Provide()
test.Assert(t, svcDsc != nil)
}

func TestPbContentProviderWithDynamicGo(t *testing.T) {
mainbs, _ := os.ReadFile("./jsonpb_test/idl/echo_import.proto")
main := string(mainbs)
incbs, _ := os.ReadFile("./jsonpb_test/idl/echo.proto")
includes := map[string]string{
"echo.proto": string(incbs),
}
opts := dproto.Options{}
p, err := NewPbContentProviderWithDynamicGo(context.Background(), opts, "main.proto", main, includes)

test.Assert(t, err == nil)

svcDsc := <-p.Provide()
test.Assert(t, svcDsc != nil)
test.Assert(t, svcDsc.Name() == "EchoService")
test.Assert(t, svcDsc.LookupMethodByName("Echo") != nil)

p.(*PbFileProviderWithDynamicGo).UpdateIDL(context.Background(), opts, "main.proto", main, includes)
svcDsc1 := <-p.Provide()
test.Assert(t, svcDsc1 != nil)
test.Assert(t, svcDsc1.Name() == "EchoService")
test.Assert(t, svcDsc1.LookupMethodByName("Echo") != nil)
}
Loading