Skip to content

Instantly share code, notes, and snippets.

@dalmarcogd
Last active July 30, 2021 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalmarcogd/238f08b0dbb46b4d75aaf4ba2481b58b to your computer and use it in GitHub Desktop.
Save dalmarcogd/238f08b0dbb46b4d75aaf4ba2481b58b to your computer and use it in GitHub Desktop.
CSU Emulator with scenaries requests
import (
"context"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
"github.com/hashlab/issuing-processor/internal/pbcsu"
"github.com/hashlab/issuing-processor/pkg/pbcommon"
)
type requestResponse struct {
req interface{}
resp interface{}
}
var mapRequestResponse = map[string]requestResponse{
"": {req: nil, resp: nil},
}
func matcher(req interface{}) (string, interface{}, error) {
for scenario, reqResp := range mapRequestResponse {
if cmp.Equal(reqResp.req, req) {
return scenario, reqResp.resp, nil
}
}
return "", nil, errors.New("unmatched request")
}
type csuServer struct {
t *testing.T
}
func NewCsuServer() pbcsu.CsuServer {
return &csuServer{}
}
func (c *csuServer) GetAccount(_ context.Context, request *pbcsu.GetAccountRequest) (*pbcommon.RAccount, error) {
scenario, resp, err := matcher(request)
if err != nil {
zap.L().Error("get-accounts", zap.Error(err))
}
zap.L().Info("get-accounts", zap.String("scenario", scenario))
if r, ok := resp.(*pbcommon.RAccount); ok {
return r, nil
}
return nil, nil
}
package main
import (
"context"
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/protobuf/encoding/protojson"
// Automatic load environment variables from .env.
_ "github.com/joho/godotenv/autoload"
"github.com/hashlab/issuing-processor/internal/pbcsu"
)
func main() {
initLogger()
server()
}
func server() {
ctx := context.Background()
gmux := grpcruntime.NewServeMux(
grpcruntime.WithMarshalerOption(grpcruntime.MIMEWildcard, &grpcruntime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
EmitUnpopulated: true,
UseProtoNames: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}),
grpcruntime.WithErrorHandler(grpcruntime.DefaultHTTPErrorHandler),
)
_ = pbcsu.RegisterCsuHandlerServer(ctx, gmux, NewCsuServer())
}
func initLogger() {
config := zap.NewProductionConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
log, err := config.Build()
if err != nil {
panic(err)
}
_ = zap.ReplaceGlobals(log)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment