| 1 | package secrets |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | once sync.Once |
| 11 | detectorsFactory *detectorFactory |
| 12 | transformersFactory *transformerFactory |
| 13 | ) |
| 14 | |
| 15 | func GetDetectorFactory() DetectorFactory { |
| 16 | initFactories() |
| 17 | return detectorsFactory |
| 18 | } |
| 19 | |
| 20 | func GetTransformerFactory() TransformerFactory { |
| 21 | initFactories() |
| 22 | return transformersFactory |
| 23 | } |
| 24 | |
| 25 | func initFactories() { |
| 26 | once.Do(func() { |
| 27 | detectorsFactory = &detectorFactory{make(map[string]func(config ...string) Detector)} |
| 28 | transformersFactory = &transformerFactory{make(map[string]func() Transformer)} |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | type DetectorFactory interface { |
| 33 | Register(name string, initMethod func(config ...string) Detector) error |
| 34 | Create(names []string, detectorConfigs map[string][]string) (detectors []Detector, missing []string) |
| 35 | } |
| 36 | |
| 37 | type detectorFactory struct { |
| 38 | factoryMethodsMap map[string]func(config ...string) Detector |
| 39 | } |
| 40 | |
| 41 | func (f *detectorFactory) Register(name string, initMethod func(config ...string) Detector) error { |
| 42 | name = strings.ToLower(name) |
| 43 | if _, exist := f.factoryMethodsMap[name]; exist { |
| 44 | return fmt.Errorf("detecor '%s' already registered", name) |
| 45 | } |
| 46 | |
| 47 | f.factoryMethodsMap[name] = initMethod |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func (f *detectorFactory) Create(names []string, detectorConfigs map[string][]string) (detectors []Detector, missing []string) { |
| 52 | for _, name := range names { |
| 53 | config := detectorConfigs[name] |
| 54 | d := f.create(name, config...) |
| 55 | if d != nil { |
| 56 | detectors = append(detectors, d) |
| 57 | } else { |
| 58 | missing = append(missing, name) |
| 59 | } |
| 60 | } |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | func (f *detectorFactory) create(name string, config ...string) Detector { |
| 65 | name = strings.ToLower(name) |
| 66 | if factoryMethod, exist := f.factoryMethodsMap[name]; exist { |
| 67 | return factoryMethod(config...) |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | type TransformerFactory interface { |
| 73 | Register(name string, initMethod func() Transformer) error |
| 74 | Create(names []string) (transformers []Transformer, missing []string) |
| 75 | } |
| 76 | |
| 77 | type transformerFactory struct { |
| 78 | factoryMethodsMap map[string]func() Transformer |
| 79 | } |
| 80 | |
| 81 | func (f *transformerFactory) Register(name string, initMethod func() Transformer) error { |
| 82 | name = strings.ToLower(name) |
| 83 | if _, exist := f.factoryMethodsMap[name]; exist { |
| 84 | return fmt.Errorf("transformer '%s' already registered", name) |
| 85 | } |
| 86 | |
| 87 | f.factoryMethodsMap[name] = initMethod |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func (f *transformerFactory) Create(names []string) (transformers []Transformer, missing []string) { |
| 92 | for _, name := range names { |
| 93 | t := f.create(name) |
| 94 | if t != nil { |
| 95 | transformers = append(transformers, t) |
| 96 | } else { |
| 97 | missing = append(missing, name) |
| 98 | } |
| 99 | } |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | func (f *transformerFactory) create(name string) Transformer { |
| 104 | name = strings.ToLower(name) |
| 105 | if factoryMethod, exist := f.factoryMethodsMap[name]; exist { |
| 106 | return factoryMethod() |
| 107 | } |
| 108 | return nil |
| 109 | } |