1
1
package registry
2
2
3
3
import (
4
+ "fmt"
5
+ "log/slog"
4
6
"os"
5
7
"path"
6
8
"path/filepath"
7
9
"strings"
8
10
9
11
"github.com/dpup/protoc-gen-grpc-gateway-ts/data"
10
12
"github.com/pkg/errors"
11
- log "github.com/sirupsen/logrus" //nolint: depguard // not sure, will remove
12
13
"google.golang.org/protobuf/types/descriptorpb"
13
14
"google.golang.org/protobuf/types/pluginpb"
14
15
)
@@ -65,14 +66,14 @@ type Registry struct {
65
66
// NewRegistry initialise the registry and return the instance.
66
67
func NewRegistry (opts Options ) (* Registry , error ) {
67
68
tsImportRoots , tsImportRootAliases , err := getTSImportRootInformation (opts )
68
- log . Debugf ("found ts import roots %v" , tsImportRoots )
69
- log . Debugf ("found ts import root aliases %v" , tsImportRootAliases )
69
+ slog . Debug ("found ts import roots" , slog . Any ( "importRoots" , tsImportRoots ) )
70
+ slog . Debug ("found ts import root aliases" , slog . Any ( "importRootAliases" , tsImportRootAliases ) )
70
71
if err != nil {
71
72
return nil , errors .Wrap (err , "error getting common import root information" )
72
73
}
73
74
74
- log . Debugf ("found fetch module directory %s" , opts .FetchModuleDirectory )
75
- log . Debugf ("found fetch module name %s" , opts .FetchModuleFilename )
75
+ slog . Debug ("found fetch module directory" , slog . String ( "moduleDir" , opts .FetchModuleDirectory ) )
76
+ slog . Debug ("found fetch module name" , slog . String ( "moduleName" , opts .FetchModuleFilename ) )
76
77
77
78
return & Registry {
78
79
Options : opts ,
@@ -161,7 +162,7 @@ func (r *Registry) Analyse(req *pluginpb.CodeGeneratorRequest) (map[string]*data
161
162
}
162
163
163
164
files := req .GetProtoFile ()
164
- log . Debugf ("about to start anaylyse files, %d in total" , len (files ))
165
+ slog . Debug ("about to start anaylyse files" , slog . Int ( "count" , len (files ) ))
165
166
data := make (map [string ]* data.File )
166
167
// analyse all files in the request first
167
168
for _ , f := range files {
@@ -258,9 +259,9 @@ func (r *Registry) getSourceFileForImport(source, target, root, alias string) (s
258
259
}
259
260
260
261
ret = strings .ReplaceAll (absTarget , absRoot , alias )
261
- log . Debugf ( "replacing root alias %s for %s, result: %s" , alias , target , ret )
262
+ slog . Debug ( fmt . Sprintf ( "replacing root alias %s for %s, result: %s" , alias , target , ret ) )
262
263
} else { // return relative path here
263
- log . Debugf ("no root alias found, trying to get the relative path for %s" , target )
264
+ slog . Debug ("no root alias found, trying to get the relative path" , slog . String ( "target" , target ) )
264
265
absSource , err := filepath .Abs (source )
265
266
if err != nil {
266
267
return "" , errors .Wrapf (err , "error looking up absolute directory with base dir: %s" , source )
@@ -272,14 +273,14 @@ func (r *Registry) getSourceFileForImport(source, target, root, alias string) (s
272
273
}
273
274
274
275
slashPath := filepath .ToSlash (ret )
275
- log . Debugf ( "got relative path %s for %s" , target , slashPath )
276
+ slog . Debug ( fmt . Sprintf ( "got relative path %s for %s" , target , slashPath ) )
276
277
277
278
// sub directory will not have relative path ./, if this happens, prepend one
278
279
if ! strings .HasPrefix (slashPath , "../" ) {
279
280
ret = filepath .FromSlash ("./" + slashPath )
280
281
}
281
282
282
- log . Debugf ( "no root alias found, trying to get the relative path for %s, result: %s" , target , ret )
283
+ slog . Debug ( fmt . Sprintf ( "no root alias found, trying to get the relative path for %s, result: %s" , target , ret ) )
283
284
}
284
285
285
286
// remove .ts suffix if there's any
@@ -293,7 +294,7 @@ func (r *Registry) getSourceFileForImport(source, target, root, alias string) (s
293
294
294
295
func (r * Registry ) collectExternalDependenciesFromData (filesData map [string ]* data.File ) error {
295
296
for _ , fileData := range filesData {
296
- log . Debugf ("collecting dependencies information for %s" , fileData .TSFileName )
297
+ slog . Debug ("collecting dependencies information" , slog . String ( "fileName" , fileData .TSFileName ) )
297
298
// dependency group up the dependency by package+file
298
299
dependencies := make (map [string ]* data.Dependency )
299
300
for _ , typeName := range fileData .ExternalDependingTypes {
@@ -318,7 +319,7 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat
318
319
target := data .GetTSFileName (typeInfo .File )
319
320
var sourceFile string
320
321
if pkg , ok := r .TSPackages [target ]; ok {
321
- log . Debugf ("package import override %s has been found for file %s" , pkg , target )
322
+ slog . Debug ("package import override has been found" , slog . String ( "pkg" , pkg ), slog . String ( " target" , target ) )
322
323
sourceFile = pkg
323
324
} else {
324
325
foundAtRoot , alias , err := r .findRootAliasForPath (func (absRoot string ) (bool , error ) {
0 commit comments