Skip to content

Commit 712e16e

Browse files
committed
Fix issues in migration and wrap logic
1 parent ab3787c commit 712e16e

3 files changed

Lines changed: 36 additions & 34 deletions

File tree

migration/migrate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func createMigrationFile(ctx *gofr.Context, migrationName string) error {
100100
return err
101101
}
102102

103-
defer file.Close()
103+
defer func() { _ = file.Close() }()
104104

105105
err = migrationTemplate.Execute(file, migrationName)
106106
if err != nil {
@@ -123,6 +123,8 @@ func createAllMigration(ctx *gofr.Context) error {
123123
return err
124124
}
125125

126+
defer func() { _ = f.Close() }()
127+
126128
d, err := os.ReadDir("./")
127129
if err != nil {
128130
return err
@@ -152,7 +154,7 @@ func getAllExistingMigrations(ctx *gofr.Context, existing map[string]string) (ma
152154
return nil, err
153155
}
154156

155-
defer file.Close()
157+
defer func() { _ = file.Close() }()
156158

157159
scanner := bufio.NewScanner(file)
158160
for scanner.Scan() {

wrap/grpc.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ type FileType struct {
184184
}
185185

186186
// BuildGRPCGoFrClient generates gRPC client wrapper code based on a proto definition.
187-
func BuildGRPCGoFrClient(ctx *gofr.Context) (any, error) {
187+
func BuildGRPCGoFrClient(ctx *gofr.Context) (interface{}, error) {
188188
gRPCClient := []FileType{
189189
{FileSuffix: clientFileSuffix, CodeGenerator: generateGoFrClient},
190190
{FileSuffix: clientHealthFile, CodeGenerator: generateGoFrClientHealth},
@@ -194,7 +194,7 @@ func BuildGRPCGoFrClient(ctx *gofr.Context) (any, error) {
194194
}
195195

196196
// BuildGRPCGoFrServer generates gRPC client and server code based on a proto definition.
197-
func BuildGRPCGoFrServer(ctx *gofr.Context) (any, error) {
197+
func BuildGRPCGoFrServer(ctx *gofr.Context) (interface{}, error) {
198198
gRPCServer := []FileType{
199199
{FileSuffix: serverWrapperFileSuffix, CodeGenerator: generateGoFrServerWrapper},
200200
{FileSuffix: serverHealthFile, CodeGenerator: generateGoFrServerHealthWrapper},
@@ -208,40 +208,40 @@ func BuildGRPCGoFrServer(ctx *gofr.Context) (any, error) {
208208
// generateWrapper executes the function for specified FileType to create GoFr integrated
209209
// gRPC server/client files with the required services in proto file and
210210
// specified suffix for every service specified in the proto file.
211-
func generateWrapper(ctx *gofr.Context, options ...FileType) (any, error) {
211+
func generateWrapper(ctx *gofr.Context, options ...FileType) (interface{}, error) {
212212
protoPath := ctx.Param("proto")
213213
if protoPath == "" {
214214
ctx.Error(ErrNoProtoFile)
215215
return nil, ErrNoProtoFile
216216
}
217217

218-
definition, err := parseProtoFile(ctx, protoPath)
219-
if err != nil {
220-
ctx.Errorf("Failed to parse proto file: %v", err)
221-
return nil, err
222-
}
223-
224-
imports := getImports(ctx, definition, protoPath)
225-
projectPath, packageName := getPackageAndProject(ctx, definition, protoPath)
226-
services := getServices(ctx, definition)
227-
requests := getRequests(ctx, services)
228-
229-
for _, service := range services {
230-
wrapperData := WrapperData{
231-
Package: packageName,
232-
Service: service.Name,
233-
Methods: service.Methods,
234-
Requests: uniqueRequestTypes(ctx, service.Methods),
235-
Source: path.Base(protoPath),
236-
Imports: imports,
218+
definition, err := parseProtoFile(ctx, protoPath)
219+
if err != nil {
220+
ctx.Errorf("Failed to parse proto file: %v", err)
221+
return nil, err
237222
}
238223

239-
if err := generateFiles(ctx, projectPath, service.Name, &wrapperData, requests, options...); err != nil {
240-
return nil, err
224+
imports := getImports(ctx, definition, protoPath)
225+
projectPath, packageName := getPackageAndProject(ctx, definition, protoPath)
226+
services := getServices(ctx, definition)
227+
requests := getRequests(ctx, services)
228+
229+
for _, service := range services {
230+
wrapperData := WrapperData{
231+
Package: packageName,
232+
Service: service.Name,
233+
Methods: service.Methods,
234+
Requests: uniqueRequestTypes(ctx, service.Methods),
235+
Source: path.Base(protoPath),
236+
Imports: imports,
237+
}
238+
239+
if err := generateFiles(ctx, projectPath, service.Name, &wrapperData, requests, options...); err != nil {
240+
return nil, err
241+
}
241242
}
242-
}
243243

244-
ctx.Info("Successfully generated all files for GoFr integrated gRPC servers/clients")
244+
ctx.Info("Successfully generated all files for GoFr integrated gRPC servers/clients")
245245

246246
return "Successfully generated all files for GoFr integrated gRPC servers/clients", nil
247247
}
@@ -464,8 +464,8 @@ func getImports(ctx *gofr.Context, definition *proto.Proto, protoPath string) []
464464
}
465465

466466
lastPiece := strings.LastIndex(packageName, ".")
467-
formattedImport := fmt.Sprintf("%s \"%s\"", packageName[lastPiece+1:], packageSource)
468-
imports = append(imports, fmt.Sprintf("%q", formattedImport))
467+
formattedImport := fmt.Sprintf("%s %q", packageName[lastPiece+1:], packageSource)
468+
imports = append(imports, formattedImport)
469469
}
470470
}
471471

wrap/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type {{ .Service }}ServerWithGofr interface {
4848
{{- if or .StreamsRequest .StreamsResponse }}
4949
{{ .Name }}(*gofr.Context, {{ $.Service }}_{{ .Name }}Server) error
5050
{{- else }}
51-
{{ .Name }}(*gofr.Context) (any, error)
51+
{{ .Name }}(*gofr.Context) (interface{}, error)
5252
{{- end }}
5353
{{- end }}
5454
}
@@ -291,7 +291,7 @@ func (h *{{ .Service }}ServerWrapper) mustEmbedUnimplemented{{ .Service }}Server
291291
292292
// Register{{ .Service }}ServerWithGofr registers the server
293293
func Register{{ .Service }}ServerWithGofr(app *gofr.App, srv {{ .Service }}ServerWithGofr) {
294-
registerServerWithGofr(app, srv, func(s grpc.ServiceRegistrar, srv any) {
294+
registerServerWithGofr(app, srv, func(s grpc.ServiceRegistrar, srv interface{}) {
295295
wrapper := &{{ .Service }}ServerWrapper{
296296
server: srv.({{ .Service }}ServerWithGofr),
297297
healthServer: getOrCreateHealthServer(),
@@ -414,7 +414,7 @@ func (s *{{ $.Service }}GoFrServer) {{ .Name }}(ctx *gofr.Context, stream {{ $.S
414414
return nil
415415
}
416416
{{- else }}
417-
func (s *{{ $.Service }}GoFrServer) {{ .Name }}(ctx *gofr.Context) (any, error) {
417+
func (s *{{ $.Service }}GoFrServer) {{ .Name }}(ctx *gofr.Context) (interface{}, error) {
418418
return &{{ .Response }}{}, nil
419419
}
420420
{{- end }}
@@ -565,7 +565,7 @@ func getOrCreateHealthServer() *healthServer {
565565
return globalHealthServer
566566
}
567567
568-
func registerServerWithGofr(app *gofr.App, srv any, registerFunc func(grpc.ServiceRegistrar, any)) {
568+
func registerServerWithGofr(app *gofr.App, srv interface{}, registerFunc func(grpc.ServiceRegistrar, interface{})) {
569569
var s grpc.ServiceRegistrar = app
570570
h := getOrCreateHealthServer()
571571

0 commit comments

Comments
 (0)