Skip to content

Commit

Permalink
mini refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jul 9, 2024
1 parent c5e9b1b commit 876a5e2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 59 deletions.
40 changes: 12 additions & 28 deletions internal/app/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/domain/rest/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions pkg/grpccontext/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 18 additions & 25 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main // import "github.com/bavix/gripmock/protoc-gen-gripmock"

import (
"bytes"
"embed"
_ "embed"
"fmt"
"io"
"log"
Expand All @@ -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.
//
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 876a5e2

Please sign in to comment.