-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathconfig.go
696 lines (634 loc) · 22.4 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// package config defines the schemas and functionality of the .mockery.yml
// config files. This package is NOT meant to be used by external Go libraries.
// We expose the contents of this package purely for documentation purposes.
//
// Do NOT import this package. We cannot guarantee backwards-compatibility of
// the methods herein.
package config
import (
"bufio"
"bytes"
"context"
"fmt"
"go/ast"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/brunoga/deep"
"github.com/chigopher/pathlib"
"github.com/go-viper/mapstructure/v2"
koanfYAML "github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
internalConfig "github.com/vektra/mockery/v3/internal/config"
"github.com/vektra/mockery/v3/internal/logging"
"github.com/vektra/mockery/v3/internal/stackerr"
"github.com/vektra/mockery/v3/template_funcs"
"golang.org/x/tools/go/packages"
)
// TemplateData is the data sent to the template for the config file.
type TemplateData struct {
// ConfigDir is the directory of where the mockery config file is located.
ConfigDir string
// InterfaceDir is the directory of the interface being mocked.
InterfaceDir string
// InterfaceDirRelative is the same as InterfaceDir, but made relative to the ConfigDir.
InterfaceDirRelative string
// InterfaceFile is the filename of where the interface is defined.
InterfaceFile string
// InterfaceName is the name of the interface (duh).
InterfaceName string
// Mock is a parameter that takes the value of "Mock" if the interface is exported, and "mock" otherwise.
Mock string
// StructName is the configured name of the mock.
StructName string
// SrcPackageName is the name of the source package as defined by the `package [name]` in the source package.
SrcPackageName string
// SrcPackagePath is the fully qualified package path of the source package. e.g. "github.com/vektra/mockery/v3".
SrcPackagePath string
// Template is the value of the `template` parameter.
Template string
}
func addr[T any](v T) *T {
return &v
}
func NewDefaultKoanf(ctx context.Context) (*koanf.Koanf, error) {
c := Config{
All: addr(false),
Dir: addr("{{.InterfaceDir}}"),
FileName: addr("mocks_test.go"),
ForceFileWrite: addr(true),
Formatter: addr("goimports"),
LogLevel: addr("info"),
StructName: addr("{{.Mock}}{{.InterfaceName}}"),
PkgName: addr("{{.SrcPackageName}}"),
Recursive: addr(false),
RequireTemplateSchemaExists: addr(true),
Template: addr("testify"),
TemplateData: map[string]any{},
TemplateSchema: addr("{{.Template}}.schema.json"),
}
k := koanf.New("|")
if err := k.Load(structs.Provider(c, "koanf"), nil); err != nil {
return nil, stackerr.NewStackErr(err)
}
return k, nil
}
type RootConfig struct {
Config `koanf:",squash" yaml:",inline"`
Packages map[string]*PackageConfig `koanf:"packages" yaml:"packages"`
koanf *koanf.Koanf
configFile *pathlib.Path
}
func NewRootConfig(
ctx context.Context,
flags *pflag.FlagSet,
) (*RootConfig, *koanf.Koanf, error) {
var configFile *pathlib.Path
log := zerolog.Ctx(ctx)
var err error
conf := &Config{}
// Set all parameters to their respective zero-values. Need to use
// reflection for this sadly.
v := reflect.ValueOf(conf).Elem()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.Kind() != reflect.Pointer {
continue
}
if !field.IsNil() {
continue
}
field.Set(reflect.New(field.Type().Elem()))
}
k, err := NewDefaultKoanf(ctx)
if err != nil {
return nil, nil, err
}
var rootConfig RootConfig = RootConfig{
Config: *conf,
koanf: k,
}
configFileFromEnv := os.Getenv("MOCKERY_CONFIG")
if configFileFromEnv != "" {
configFile = pathlib.NewPath(configFileFromEnv)
}
if configFile == nil {
configFileFromFlags, err := flags.GetString("config")
if err != nil {
return nil, nil, fmt.Errorf("getting --config from flags: %w", err)
}
if configFileFromFlags != "" {
configFile = pathlib.NewPath(configFileFromFlags)
}
}
if configFile == nil {
log.Debug().Msg("config file not specified, searching")
configFile, err = internalConfig.FindConfig()
if err != nil {
return nil, k, fmt.Errorf("discovering mockery config: %w", err)
}
log.Debug().Str("config-file", configFile.String()).Msg("config file found")
}
rootConfig.configFile = configFile
koanfTags := conf.koanfTagNames()
if err := k.Load(
env.ProviderWithValue(
"MOCKERY_",
".",
func(key, value string) (string, any) {
normalizedKey := strings.Replace(strings.ToLower(strings.TrimPrefix(key, "MOCKERY_")), "_", "-", -1)
if _, exists := koanfTags[normalizedKey]; !exists {
log.Debug().Str("env-var", key).Msg("environment variable unknown, not including in config map.")
return "", nil
}
// Loading from environment variables is kind of weird. Koanf doesn't seem to
// have a good way to automatically convert values destined for boolean fields
// without resorting to reflection. We do something gross here by
// just checking to see if the string "looks" like it should be
// a boolean. The proper solution is to use reflection on the destination
// struct to see what the type actually should be cast to. I'm sure
// the koanf project would appreciate a PR to add an environment
// parser:
if strings.ToLower(value) == "true" || strings.ToLower(value) == "false" {
valueAsBool, err := strconv.ParseBool(value)
if err != nil {
panic(err)
}
return normalizedKey, valueAsBool
}
return normalizedKey, value
}),
nil,
); err != nil {
log.Err(err).Msg("failed to load environment provider")
return nil, nil, stackerr.NewStackErr(err)
}
if err := k.Load(file.Provider(configFile.String()), koanfYAML.Parser()); err != nil {
return nil, k, fmt.Errorf("loading config file: %w", err)
}
if flags != nil {
if err := k.Load(posflag.Provider(flags, ".", k), nil); err != nil {
return nil, k, fmt.Errorf("loading flags: %w", err)
}
}
// Second argument is nil because of a weird bug: https://github.com/knadh/koanf/issues/307
if err := k.UnmarshalWithConf("", nil, koanf.UnmarshalConf{
DecoderConfig: &mapstructure.DecoderConfig{
ErrorUnused: true,
Result: &rootConfig,
},
}); err != nil {
return nil, k, fmt.Errorf("unmarshalling config: %w", err)
}
if err := rootConfig.Initialize(ctx); err != nil {
return nil, k, fmt.Errorf("initializing root config: %w", err)
}
return &rootConfig, k, nil
}
func (c *RootConfig) ConfigFileUsed() *pathlib.Path {
return c.configFile
}
// mergreStringMaps merges two (possibly nested) maps.
func mergeStringMaps(src, dest map[string]any) {
for srcKey, srcValue := range src {
if destValue, exists := dest[srcKey]; exists {
// If the source value is a map, merge recursively
if destMap, ok := destValue.(map[string]any); ok {
if srcMap, ok := srcValue.(map[string]any); ok {
mergeStringMaps(srcMap, destMap)
continue
}
}
continue
}
// Otherwise, set the value directly
dest[srcKey] = srcValue
}
}
// mergeConfigs merges the values from c1 into c2.
func mergeConfigs(ctx context.Context, src Config, dest *Config) {
log := zerolog.Ctx(ctx)
// Merge root config with package config
srcValue := reflect.ValueOf(src)
destValue := reflect.ValueOf(dest)
for i := 0; i < srcValue.NumField(); i++ {
fieldLog := log.With().
Int("index", i).
Str("name", srcValue.Type().Field(i).Name).
Logger()
fieldLog.Debug().Msg("Iterating over field for merging")
srcFieldValue := srcValue.Field(i)
destFieldValue := destValue.Elem().Field(i)
if srcFieldValue.Kind() == reflect.Map {
srcMap, ok := srcFieldValue.Interface().(map[string]any)
if !ok {
log.Debug().Msg("field value is not `any`, skipping merge")
continue
}
destMap, ok := destFieldValue.Interface().(map[string]any)
if !ok {
log.Debug().Msg("dest map value is not `any`, skipping")
continue
}
if destMap == nil {
destFieldValue.Set(reflect.ValueOf(make(map[string]any)))
}
destMap = destFieldValue.Interface().(map[string]any)
mergeStringMaps(srcMap, destMap)
} else if srcFieldValue.Kind() == reflect.Pointer && destFieldValue.IsNil() {
// Attribute is a pointer. We need to allocate a new value of the
// same type as the type being pointed to.
newValue := reflect.New(srcFieldValue.Elem().Type())
// Then, set this new value to the same value as the src.
newValue.Elem().Set(srcFieldValue.Elem())
// newValue is already an address, so we can set destFieldValue
// to it as-is.
destFieldValue.Set(newValue)
} else if destFieldValue.CanSet() && destFieldValue.IsZero() {
destFieldValue.Set(srcFieldValue)
} else {
fieldLog.Debug().
Bool("can-set", destFieldValue.CanSet()).
Bool("is-zero", destFieldValue.IsZero()).
Msg("field not addressable, not merging.")
}
}
}
func (c *RootConfig) Initialize(ctx context.Context) error {
log := zerolog.Ctx(ctx)
recursivePackages := []string{}
for pkgName, pkgConfig := range c.Packages {
if pkgConfig == nil {
pkgConfig = NewPackageConfig()
c.Packages[pkgName] = pkgConfig
}
if pkgConfig.Config == nil {
pkgConfig.Config = &Config{}
}
if pkgConfig.Interfaces == nil {
pkgConfig.Interfaces = map[string]*InterfaceConfig{}
}
pkgLog := log.With().Str("package-path", pkgName).Logger()
pkgCtx := pkgLog.WithContext(ctx)
mergeConfigs(pkgCtx, c.Config, pkgConfig.Config)
if err := pkgConfig.Initialize(pkgCtx); err != nil {
return fmt.Errorf("initializing root config: %w", err)
}
if *pkgConfig.Config.Recursive {
recursivePackages = append(recursivePackages, pkgName)
}
}
for _, recursivePackageName := range recursivePackages {
pkgLog := log.With().Str(logging.LogKeyPackagePath, recursivePackageName).Logger()
pkgCtx := pkgLog.WithContext(ctx)
pkgLog.Debug().Msg("package marked as recursive")
subpkgs, err := c.subPackages(recursivePackageName)
if err != nil {
return fmt.Errorf("discovering sub packages of %s: %w", recursivePackageName, err)
}
parentPkgConfig := c.Packages[recursivePackageName]
for _, subpkg := range subpkgs {
recursivePkgConfig, err := c.GetPackageConfig(ctx, recursivePackageName)
if err != nil {
return fmt.Errorf("getting package config: %w", err)
}
if recursivePkgConfig.Config.ShouldExcludeSubpkg(subpkg) {
pkgLog.Debug().Msg("package was marked for exclusion")
continue
}
var subPkgConfig *PackageConfig
if existingSubPkg, exists := c.Packages[subpkg]; exists {
subPkgConfig = existingSubPkg
} else {
subPkgConfig = NewPackageConfig()
}
mergeConfigs(pkgCtx, *parentPkgConfig.Config, subPkgConfig.Config)
c.Packages[subpkg] = subPkgConfig
}
}
return nil
}
func (c *RootConfig) subPackages(pkgPath string) ([]string, error) {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles,
}, pkgPath+"/...")
if err != nil {
return nil, fmt.Errorf("failed to load packages: %w", err)
}
convertPkgPath := func(pkgs []*packages.Package) []string {
paths := make([]string, 0, len(pkgs))
for _, pkg := range pkgs {
if len(pkg.GoFiles) == 0 {
continue
}
paths = append(paths, pkg.PkgPath)
}
return paths
}
return convertPkgPath(pkgs), nil
}
func (c *RootConfig) GetPackageConfig(ctx context.Context, pkgPath string) (*PackageConfig, error) {
pkgConfig, ok := c.Packages[pkgPath]
if !ok {
return nil, stackerr.NewStackErr(fmt.Errorf("package %s does not exist in the config", pkgPath))
}
return pkgConfig, nil
}
// GetPackages returns a list of the packages that are defined in
// the `packages` config section.
func (c *RootConfig) GetPackages(ctx context.Context) ([]string, error) {
packages := []string{}
for key := range c.Packages {
packages = append(packages, key)
}
return packages, nil
}
type PackageConfig struct {
Config *Config `koanf:"config" yaml:"config,omitempty"`
Interfaces map[string]*InterfaceConfig `koanf:"interfaces" yaml:"interfaces,omitempty"`
}
func NewPackageConfig() *PackageConfig {
return &PackageConfig{
Config: &Config{},
Interfaces: map[string]*InterfaceConfig{},
}
}
func (c *PackageConfig) Initialize(ctx context.Context) error {
for idx, ifaceConfig := range c.Interfaces {
if ifaceConfig == nil {
ifaceConfig = NewInterfaceConfig()
c.Interfaces[idx] = ifaceConfig
}
if ifaceConfig.Config == nil {
ifaceConfig.Config = &Config{}
}
mergeConfigs(ctx, *c.Config, ifaceConfig.Config)
if err := ifaceConfig.Initialize(ctx); err != nil {
return fmt.Errorf("initializing package config: %w", err)
}
}
return nil
}
func (c PackageConfig) GetInterfaceConfig(ctx context.Context, interfaceName string) *InterfaceConfig {
log := zerolog.Ctx(ctx)
if ifaceConfig, ok := c.Interfaces[interfaceName]; ok {
return ifaceConfig
}
ifaceConfig := NewInterfaceConfig()
newConfig, err := deep.Copy(c.Config)
if err != nil {
log.Err(err).Msg("issue when deep-copying package config to interface config")
panic(err)
}
ifaceConfig.Config = newConfig
ifaceConfig.Configs = []*Config{newConfig}
return ifaceConfig
}
func (c PackageConfig) ShouldGenerateInterface(ctx context.Context, interfaceName string) (bool, error) {
log := zerolog.Ctx(ctx)
if *c.Config.All {
if *c.Config.IncludeInterfaceRegex != "" {
log.Warn().Msg("interface config has both `all` and `include-interface-regex` set: `include-interface-regex` will be ignored")
}
if *c.Config.ExcludeInterfaceRegex != "" {
log.Warn().Msg("interface config has both `all` and `exclude-interface-regex` set: `exclude-interface-regex` will be ignored")
}
log.Debug().Msg("`all: true` is set, interface should be generated")
return true, nil
}
if _, exists := c.Interfaces[interfaceName]; exists {
return true, nil
}
includeRegex := *c.Config.IncludeInterfaceRegex
excludeRegex := *c.Config.ExcludeInterfaceRegex
if includeRegex == "" {
if excludeRegex != "" {
log.Warn().Msg("interface config has `exclude-interface-regex` set but not `include-interface-regex`: `exclude-interface-regex` will be ignored")
}
return false, nil
}
includedByRegex, err := regexp.MatchString(includeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `include-interface-regex`: %w", err)
}
if !includedByRegex {
log.Debug().Msg("interface does not match include-interface-regex")
return false, nil
}
log.Debug().Msg("interface matches include-interface-regex")
if excludeRegex == "" {
return true, nil
}
excludedByRegex, err := regexp.MatchString(excludeRegex, interfaceName)
if err != nil {
return false, fmt.Errorf("evaluating `exclude-interface-regex`: %w", err)
}
if excludedByRegex {
log.Debug().Msg("interface matches exclude-interface-regex")
return false, nil
}
log.Debug().Msg("interface does not match exclude-interface-regex")
return true, nil
}
type InterfaceConfig struct {
Config *Config `koanf:"config" yaml:"config,omitempty"`
Configs []*Config `koanf:"configs" yaml:"configs,omitempty"`
}
func NewInterfaceConfig() *InterfaceConfig {
return &InterfaceConfig{
Config: &Config{},
Configs: []*Config{},
}
}
func (c *InterfaceConfig) Initialize(ctx context.Context) error {
if len(c.Configs) == 0 {
c.Configs = []*Config{c.Config}
} else {
for _, subCfg := range c.Configs {
mergeConfigs(ctx, *c.Config, subCfg)
}
}
return nil
}
type ReplaceType struct {
PkgPath string `koanf:"pkg-path" yaml:"pkg-path,omitempty"`
TypeName string `koanf:"type-name" yaml:"type-name,omitempty"`
}
type Config struct {
All *bool `koanf:"all" yaml:"all,omitempty"`
Anchors map[string]any `koanf:"_anchors" yaml:"_anchors,omitempty"`
BuildTags *string `koanf:"build-tags" yaml:"build-tags,omitempty"`
ConfigFile *string `koanf:"config" yaml:"config,omitempty"`
Dir *string `koanf:"dir" yaml:"dir,omitempty"`
ExcludeSubpkgRegex []string `koanf:"exclude-subpkg-regex" yaml:"exclude-subpkg-regex,omitempty"`
ExcludeInterfaceRegex *string `koanf:"exclude-interface-regex" yaml:"exclude-interface-regex,omitempty"`
FileName *string `koanf:"filename" yaml:"filename,omitempty"`
// ForceFileWrite controls whether mockery will overwrite existing files when generating mocks. This is by default set to false.
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
IncludeInterfaceRegex *string `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
LogLevel *string `koanf:"log-level" yaml:"log-level,omitempty"`
StructName *string `koanf:"structname" yaml:"structname,omitempty"`
PkgName *string `koanf:"pkgname" yaml:"pkgname,omitempty"`
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
// ReplaceType is a nested map of format map["package path"]["type name"]*ReplaceType
ReplaceType map[string]map[string]*ReplaceType `koanf:"replace-type" yaml:"replace-type,omitempty"`
// RequireTemplateSchemaExists sets whether mockery will fail if the specified
// template did not have an associated JSON schema.
RequireTemplateSchemaExists *bool `koanf:"require-template-schema-exists" yaml:"require-template-schema-exists,omitempty"`
Template *string `koanf:"template" yaml:"template,omitempty"`
TemplateData map[string]any `koanf:"template-data" yaml:"template-data,omitempty"`
// TemplateSchema is the URL of the template's JSON schema.
TemplateSchema *string `koanf:"template-schema" yaml:"template-schema,omitempty"`
}
func (c Config) koanfTagNames() map[string]struct{} {
tags := map[string]struct{}{}
t := reflect.TypeOf(c)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("koanf")
if tag != "" && tag != "-" {
tags[tag] = struct{}{}
}
}
return tags
}
func (c *Config) FilePath() *pathlib.Path {
return pathlib.NewPath(*c.Dir).Join(*c.FileName).Clean()
}
func (c *Config) ShouldExcludeSubpkg(pkgPath string) bool {
for _, regex := range c.ExcludeSubpkgRegex {
matched, err := regexp.MatchString(regex, pkgPath)
if err != nil {
panic(err)
}
if matched {
return true
}
}
return false
}
func IsAutoGenerated(path *pathlib.Path) (bool, error) {
file, err := path.OpenFile(os.O_RDONLY)
if err != nil {
return false, stackerr.NewStackErr(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if strings.Contains(text, "DO NOT EDIT") {
return true, nil
} else if strings.HasPrefix(text, "package ") {
break
}
}
return false, nil
}
var ErrInfiniteLoop = fmt.Errorf("infinite loop in template variables detected")
// ParseTemplates parses various templated strings
// in the config struct into their fully defined values. This mutates
// the config object passed. An *Interface object can be supplied to satisfy
// template variables that need information about the original
// interface being mocked. If this argument is nil, interface-specific template
// variables will be set to the empty string. The srcPkg is also needed to
// satisfy template variables regarding the source package.
func (c *Config) ParseTemplates(ctx context.Context, ifaceFileName string, ifaceName string, srcPkg *packages.Package) error {
log := zerolog.Ctx(ctx)
mock := "mock"
if ast.IsExported(ifaceName) {
mock = "Mock"
}
var (
interfaceDir string
interfaceDirRelative string
interfaceFile string
interfaceName string
)
interfaceFile = ifaceFileName
interfaceName = ifaceName
workingDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("get working directory: %w", err)
}
interfaceDirPath := pathlib.NewPath(ifaceFileName).Parent()
interfaceDir = interfaceDirPath.String()
interfaceDirRelativePath, err := interfaceDirPath.RelativeToStr(workingDir)
if err != nil {
log.Debug().Err(err).Msg("can't make path relative to working dir, setting to './'")
interfaceDirRelative = "."
} else {
interfaceDirRelative = interfaceDirRelativePath.String()
}
// data is the struct sent to the template parser
data := TemplateData{
ConfigDir: filepath.Dir(*c.ConfigFile),
InterfaceDir: interfaceDir,
InterfaceDirRelative: interfaceDirRelative,
InterfaceFile: interfaceFile,
InterfaceName: interfaceName,
Mock: mock,
StructName: *c.StructName,
SrcPackageName: srcPkg.Types.Name(),
SrcPackagePath: srcPkg.Types.Path(),
Template: *c.Template,
}
// These are the config options that we allow
// to be parsed by the templater. The keys are
// just labels we're using for logs/errors
templateMap := map[string]*string{
"dir": c.Dir,
"filename": c.FileName,
"pkgname": c.PkgName,
"structname": c.StructName,
"template-schema": c.TemplateSchema,
}
changesMade := true
for i := 0; changesMade; i++ {
if i >= 20 {
log.Error().Msg("infinite loop in template variables detected")
for key, val := range templateMap {
l := log.With().Str("variable-name", key).Str("variable-value", *val).Logger()
l.Error().Msg("config variable value")
}
return ErrInfiniteLoop
}
// Templated variables can refer to other templated variables,
// so we need to continue parsing the templates until it can't
// be parsed anymore.
changesMade = false
for name, attributePointer := range templateMap {
oldVal := *attributePointer
attributeTempl, err := template.New("config-template").Funcs(template_funcs.FuncMap).Parse(*attributePointer)
if err != nil {
return fmt.Errorf("failed to parse %s template: %w", name, err)
}
var parsedBuffer bytes.Buffer
if err := attributeTempl.Execute(&parsedBuffer, data); err != nil {
return fmt.Errorf("failed to execute %s template: %w", name, err)
}
*attributePointer = parsedBuffer.String()
if *attributePointer != oldVal {
changesMade = true
}
}
}
return nil
}
func (c *Config) GetReplacement(pkgPath string, typeName string) *ReplaceType {
pkgMap := c.ReplaceType[pkgPath]
if pkgMap == nil {
return nil
}
return pkgMap[typeName]
}