@@ -16,9 +16,9 @@ package check
1616
1717import (
1818 "context"
19- "fmt"
2019
2120 checkv1 "buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go/buf/plugin/check/v1"
21+ "buf.build/go/bufplugin/info"
2222 "buf.build/go/bufplugin/internal/gen/buf/plugin/check/v1/v1pluginrpc"
2323 "buf.build/go/bufplugin/internal/pkg/cache"
2424 "buf.build/go/bufplugin/internal/pkg/xslices"
@@ -31,7 +31,11 @@ const (
3131)
3232
3333// Client is a client for a custom lint or breaking change plugin.
34+ //
35+ // All calls with pluginrpc.Error with CodeUnimplemented if any procedure is not implemented.
3436type Client interface {
37+ info.Client
38+
3539 // Check invokes a check using the plugin..
3640 Check (ctx context.Context , request Request , options ... CheckCallOption ) (Response , error )
3741 // ListRules lists all available Rules from the plugin.
@@ -54,7 +58,7 @@ func NewClient(pluginrpcClient pluginrpc.Client, options ...ClientOption) Client
5458 for _ , option := range options {
5559 option .applyToClient (clientOptions )
5660 }
57- return newClient (pluginrpcClient , clientOptions .cacheRulesAndCategories )
61+ return newClient (pluginrpcClient , clientOptions .caching )
5862}
5963
6064// ClientOption is an option for a new Client.
@@ -64,12 +68,16 @@ type ClientOption interface {
6468 applyToClient (opts * clientOptions )
6569}
6670
67- // ClientWithCacheRulesAndCategories returns a new ClientOption that will result in the Rules from
68- // ListRules and the Categories from ListCategories being cached.
71+ // ClientWithCaching returns a new ClientOption that will result caching for items
72+ // expected to be static:
6973//
70- // The default is to not cache Rules or Categories.
71- func ClientWithCacheRulesAndCategories () ClientOption {
72- return clientWithCacheRulesAndCategoriesOption {}
74+ // - The Rules from ListRules.
75+ // - The Categories from ListCategories.
76+ // - PluginInfo from GetPluginInfo.
77+ //
78+ // The default is to not cache.
79+ func ClientWithCaching () ClientOption {
80+ return clientWithCachingOption {}
7381}
7482
7583// NewClientForSpec return a new Client that directly uses the given Spec.
@@ -80,19 +88,15 @@ func NewClientForSpec(spec *Spec, options ...ClientForSpecOption) (Client, error
8088 for _ , option := range options {
8189 option .applyToClientForSpec (clientForSpecOptions )
8290 }
83- checkServiceHandler , err := NewCheckServiceHandler (spec )
84- if err != nil {
85- return nil , err
86- }
87- checkServiceServer , err := NewCheckServiceServer (checkServiceHandler )
91+ server , err := NewServer (spec )
8892 if err != nil {
8993 return nil , err
9094 }
9195 return newClient (
9296 pluginrpc .NewClient (
93- pluginrpc .NewServerRunner (checkServiceServer ),
97+ pluginrpc .NewServerRunner (server ),
9498 ),
95- clientForSpecOptions .cacheRulesAndCategories ,
99+ clientForSpecOptions .caching ,
96100 ), nil
97101}
98102
@@ -113,9 +117,11 @@ type ListCategoriesCallOption func(*listCategoriesCallOptions)
113117// *** PRIVATE ***
114118
115119type client struct {
120+ info.Client
121+
116122 pluginrpcClient pluginrpc.Client
117123
118- cacheRulesAndCategories bool
124+ caching bool
119125
120126 // Singleton ordering: rules -> categories -> checkServiceClient
121127 rules * cache.Singleton [[]Rule ]
@@ -125,11 +131,16 @@ type client struct {
125131
126132func newClient (
127133 pluginrpcClient pluginrpc.Client ,
128- cacheRulesAndCategories bool ,
134+ caching bool ,
129135) * client {
136+ var infoClientOptions []info.ClientOption
137+ if caching {
138+ infoClientOptions = append (infoClientOptions , info .ClientWithCaching ())
139+ }
130140 client := & client {
131- pluginrpcClient : pluginrpcClient ,
132- cacheRulesAndCategories : cacheRulesAndCategories ,
141+ Client : info .NewClient (pluginrpcClient , infoClientOptions ... ),
142+ pluginrpcClient : pluginrpcClient ,
143+ caching : caching ,
133144 }
134145 client .rules = cache .NewSingleton (client .listRulesUncached )
135146 client .categories = cache .NewSingleton (client .listCategoriesUncached )
@@ -174,14 +185,14 @@ func (c *client) Check(ctx context.Context, request Request, _ ...CheckCallOptio
174185}
175186
176187func (c * client ) ListRules (ctx context.Context , _ ... ListRulesCallOption ) ([]Rule , error ) {
177- if ! c .cacheRulesAndCategories {
188+ if ! c .caching {
178189 return c .listRulesUncached (ctx )
179190 }
180191 return c .rules .Get (ctx )
181192}
182193
183194func (c * client ) ListCategories (ctx context.Context , _ ... ListCategoriesCallOption ) ([]Category , error ) {
184- if ! c .cacheRulesAndCategories {
195+ if ! c .caching {
185196 return c .listCategoriesUncached (ctx )
186197 }
187198 return c .categories .Get (ctx )
@@ -285,7 +296,7 @@ func (c *client) getCheckServiceClientUncached(ctx context.Context) (v1pluginrpc
285296 v1pluginrpc .CheckServiceListCategoriesPath ,
286297 } {
287298 if spec .ProcedureForPath (procedurePath ) == nil {
288- return nil , fmt . Errorf ( "plugin spec not implemented: RPC %q not found " , procedurePath )
299+ return nil , pluginrpc . NewErrorf ( pluginrpc . CodeUnimplemented , "procedure unimplemented: %q " , procedurePath )
289300 }
290301 }
291302 return v1pluginrpc .NewCheckServiceClient (c .pluginrpcClient )
@@ -294,29 +305,29 @@ func (c *client) getCheckServiceClientUncached(ctx context.Context) (v1pluginrpc
294305func (* client ) isClient () {}
295306
296307type clientOptions struct {
297- cacheRulesAndCategories bool
308+ caching bool
298309}
299310
300311func newClientOptions () * clientOptions {
301312 return & clientOptions {}
302313}
303314
304315type clientForSpecOptions struct {
305- cacheRulesAndCategories bool
316+ caching bool
306317}
307318
308319func newClientForSpecOptions () * clientForSpecOptions {
309320 return & clientForSpecOptions {}
310321}
311322
312- type clientWithCacheRulesAndCategoriesOption struct {}
323+ type clientWithCachingOption struct {}
313324
314- func (clientWithCacheRulesAndCategoriesOption ) applyToClient (clientOptions * clientOptions ) {
315- clientOptions .cacheRulesAndCategories = true
325+ func (clientWithCachingOption ) applyToClient (clientOptions * clientOptions ) {
326+ clientOptions .caching = true
316327}
317328
318- func (clientWithCacheRulesAndCategoriesOption ) applyToClientForSpec (clientForSpecOptions * clientForSpecOptions ) {
319- clientForSpecOptions .cacheRulesAndCategories = true
329+ func (clientWithCachingOption ) applyToClientForSpec (clientForSpecOptions * clientForSpecOptions ) {
330+ clientForSpecOptions .caching = true
320331}
321332
322333type checkCallOptions struct {}
0 commit comments