Debian Codegraph @

pkg/generators/router.go

raw symbols SCIP
1 // Package generators provides a router for creating generator instances by identifier.
2 package generatorsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/
3
4 import (
5 "errorsscip-go gomod github.com/golang/go/src . errors/"
6 "fmtscip-go gomod github.com/golang/go/src . fmt/"
7 "ioscip-go gomod github.com/golang/go/src . io/"
8 "stringsscip-go gomod github.com/golang/go/src . strings/"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/generators/basicscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/basic`/"
11 "github.com/nicholas-fedor/shoutrrr/pkg/generators/xoauth2scip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/xoauth2`/"
12 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegramscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/"
13 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
14 )
15
16 // generatorConfig holds configuration options for generator creation.
17 type generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig# struct {
18 inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#input. ioscip-go gomod github.com/golang/go/src . io/.Readerscip-go gomod github.com/golang/go/src . io/Reader#
19 }
20
21 // GeneratorOption configures how a generator is created.
22 type GeneratorOptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/GeneratorOption# func(*generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#)
23
24 // ErrUnknownGenerator is returned when an unknown generator identifier is provided.
25 var ErrUnknownGeneratorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/ErrUnknownGenerator. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("unknown generator")
26
27 // generatorMap is a registry that maps generator type names to their
28 // factory functions. It is used by NewGenerator to create instances of
29 // specific generator implementations based on the requested type.
30 var generatorMapscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap. = map[string]func(configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config. generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#) typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Generator#{
31 "basic": func(configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config. generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#) typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Generator# {
32 return &basicscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/basic`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/basic`/Generator#{
33 Inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/basic`/Generator#Input.: configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config..inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#input.,
34 }
35 },
36 "oauth2": func(configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config. generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#) typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Generator# {
37 return &xoauth2scip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/xoauth2`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators/xoauth2`/Generator#{}
38 },
39 "telegram": func(configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config. generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#) typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Generator# {
40 return &telegramscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Generator#{
41 Readerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Generator#Reader.: configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap:config..inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#input.,
42 Writerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram`/Generator#Writer.: nil,
43 }
44 },
45 }
46
47 // ListGenerators lists all available generators.
48 func ListGeneratorsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/ListGenerators().() []string {
49 generators := make([]string, len(generatorMapscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap.))
50
51 i := 0
52
53 for key := range generatorMapscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap. {
54 generators[i] = key
55 i++
56 }
57
58 return generators
59 }
60
61 // NewGenerator creates an instance of the generator that corresponds to the provided identifier.
62 // Optional GeneratorOption parameters can be used to configure the generator (e.g., WithInput).
63 func NewGeneratorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/NewGenerator().(identifier string, opts ...GeneratorOptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/GeneratorOption#) (typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Generatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Generator#, error) {
64 config := generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#{
65 inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#input.: nil,
66 }
67 for _, opt := range opts {
68 opt(&config)
69 }
70
71 generatorFactory, valid := generatorMapscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorMap.[stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(identifier)]
72 if !valid {
73 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrUnknownGeneratorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/ErrUnknownGenerator., identifier)
74 }
75
76 return generatorFactory(config), nil
77 }
78
79 // WithInput sets the input reader for generators that support it.
80 // This is useful for dependency injection in tests or for providing
81 // input from sources other than os.Stdin.
82 func WithInputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/WithInput().(reader ioscip-go gomod github.com/golang/go/src . io/.Readerscip-go gomod github.com/golang/go/src . io/Reader#) GeneratorOptionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/GeneratorOption# {
83 return func(c *generatorConfigscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#) {
84 c.inputscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/generators`/generatorConfig#input. = reader
85 }
86 }