diff --git a/internal/app/storage.go b/internal/app/storage.go index 3b2ba4d..fc43feb 100644 --- a/internal/app/storage.go +++ b/internal/app/storage.go @@ -18,39 +18,23 @@ func stubNotFoundError2(expect stuber.Query, result *stuber.Result) error { template += string(expectString) if result.Similar() == nil { - // fixme - //nolint:goerr113,perfsprint - return fmt.Errorf(template) + return fmt.Errorf("%s", template) //nolint:err113 } - if len(result.Similar().Input.Equals) > 0 { - closestMatchString, err := json.MarshalIndent(result.Similar().Input.Equals, "", "\t") - if err != nil { - return err - } - - template += fmt.Sprintf("\n\nClosest Match \n\n%s:%s", "equals", closestMatchString) - } + addClosestMatch := func(key string, match map[string]interface{}) { + if len(match) > 0 { + matchString, err := json.MarshalIndent(match, "", "\t") + if err != nil { + return + } - if len(result.Similar().Input.Contains) > 0 { - closestMatchString, err := json.MarshalIndent(result.Similar().Input.Contains, "", "\t") - if err != nil { - return err + template += fmt.Sprintf("\n\nClosest Match \n\n%s:%s", key, matchString) } - - template += fmt.Sprintf("\n\nClosest Match \n\n%s:%s", "contains", closestMatchString) } - if len(result.Similar().Input.Matches) > 0 { - closestMatchString, err := json.MarshalIndent(result.Similar().Input.Matches, "", "\t") - if err != nil { - return err - } - - template += fmt.Sprintf("\n\nClosest Match \n\n%s:%s", "matches", closestMatchString) - } + addClosestMatch("equals", result.Similar().Input.Equals) + addClosestMatch("contains", result.Similar().Input.Contains) + addClosestMatch("matches", result.Similar().Input.Matches) - // fixme - //nolint:goerr113,perfsprint - return fmt.Errorf(template) + return fmt.Errorf("%s", template) //nolint:err113 } diff --git a/internal/domain/rest/api.gen.go b/internal/domain/rest/api.gen.go index f575ac2..3da719f 100644 --- a/internal/domain/rest/api.gen.go +++ b/internal/domain/rest/api.gen.go @@ -529,3 +529,4 @@ func HandlerWithOptions(si ServerInterface, options GorillaServerOptions) http.H return r } + diff --git a/pkg/grpccontext/interceptor.go b/pkg/grpccontext/interceptor.go index b52e184..730949f 100644 --- a/pkg/grpccontext/interceptor.go +++ b/pkg/grpccontext/interceptor.go @@ -41,13 +41,12 @@ func (w serverStreamWrapper) SendHeader(md metadata.MD) error { return w.ss.Send func (w serverStreamWrapper) SetHeader(md metadata.MD) error { return w.ss.SetHeader(md) } func (w serverStreamWrapper) SetTrailer(md metadata.MD) { w.ss.SetTrailer(md) } +// StreamInterceptor is a gRPC interceptor that adds a logger to the context. +// The logger can be used to log messages related to the gRPC stream. +// +// It takes a logger as a parameter and returns a grpc.StreamServerInterceptor. +// The returned interceptor is used to intercept the gRPC stream requests. func StreamInterceptor(logger *zerolog.Logger) grpc.StreamServerInterceptor { - // StreamInterceptor is a gRPC interceptor that adds a logger to the context. - // The logger can be used to log messages related to the gRPC stream. - // - // It takes a logger as a parameter and returns a grpc.StreamServerInterceptor. - // The returned interceptor is used to intercept the gRPC stream requests. - // // The interceptor function is called for each gRPC stream request. // It takes the server, the stream, the server info, and the handler. // It returns an error. diff --git a/protoc-gen-gripmock/generator.go b/protoc-gen-gripmock/generator.go index 465b82e..66df471 100644 --- a/protoc-gen-gripmock/generator.go +++ b/protoc-gen-gripmock/generator.go @@ -3,7 +3,7 @@ package main // import "github.com/bavix/gripmock/protoc-gen-gripmock" import ( "bytes" - "embed" + _ "embed" "fmt" "io" "log" @@ -20,6 +20,10 @@ import ( "google.golang.org/protobuf/types/pluginpb" ) +const ( + SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) +) + // This package contains the implementation of protoc-gen-gripmock. // It uses the protoc tool to generate a gRPC mock server from a .proto file. // @@ -32,7 +36,11 @@ import ( func main() { // Protoc passes pluginpb.CodeGeneratorRequest in via stdin // marshalled with Protobuf - input, _ := io.ReadAll(os.Stdin) + input, err := io.ReadAll(os.Stdin) + if err != nil { + log.Fatalf("error reading stdin: %v", err) + } + var request pluginpb.CodeGeneratorRequest if err := proto.Unmarshal(input, &request); err != nil { log.Fatalf("error unmarshalling [%s]: %v", string(input), err) @@ -45,7 +53,7 @@ func main() { log.Fatalf("error initializing plugin: %v", err) } - plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) + plugin.SupportedFeatures = SupportedFeatures // Create a slice of FileDescriptorProto objects for each input file. protos := make([]*descriptorpb.FileDescriptorProto, len(plugin.Files)) @@ -131,28 +139,11 @@ type Options struct { Writer io.Writer `json:"writer"` } -// ServerTemplate is the template used to generate the gRPC server code. -// It is populated during the init function. -var ServerTemplate string - -// serverTmpl is the embed.FS used to read the server template file. +// It is used to embed the "server.tmpl" file into the binary and read its +// contents at runtime. // //go:embed server.tmpl -var serverTmpl embed.FS - -// Init initializes the ServerTemplate with the contents of the server.tmpl file. -// -// It reads the server.tmpl file from the serverTmpl embed.FS and assigns its contents -// to the ServerTemplate variable. If there is an error reading the file, it logs -// the error and stops the program. -func init() { - data, err := serverTmpl.ReadFile("server.tmpl") - if err != nil { - log.Fatalf("error reading server.tmpl: %s", err) - } - - ServerTemplate = string(data) -} +var serverTmpl string // generateServer generates the gRPC server code based on the given protobuf // descriptors and writes it to the provided io.Writer. @@ -183,7 +174,7 @@ func generateServer(protos []*descriptorpb.FileDescriptorProto, opt *Options) er // Create a new template and parse the server template tmpl := template.New("server.tmpl") - _, err := tmpl.Parse(ServerTemplate) + _, err := tmpl.Parse(serverTmpl) if err != nil { return fmt.Errorf("template parse %v", err) } @@ -293,7 +284,9 @@ func getGoPackage(proto *descriptorpb.FileDescriptorProto) (alias string, goPack return } - // If the alias is a keyword, append a random number to it. + // If the alias is a Go keyword, append a random number to it. + // Go keywords are used for reserved identifiers and cannot be used as package names. + // This is to prevent naming conflicts with the generated code. if isKeyword(alias) { alias = fmt.Sprintf("%s_pb%d", alias, aliasNum) }