|
1 | 1 | package appconfig |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "strings" |
6 | | - |
7 | | - "github.com/cosmos/cosmos-proto/anyutil" |
8 | | - "google.golang.org/protobuf/encoding/protojson" |
9 | | - "google.golang.org/protobuf/proto" |
10 | | - protoreflect "google.golang.org/protobuf/reflect/protoreflect" |
11 | | - "google.golang.org/protobuf/reflect/protoregistry" |
12 | | - "google.golang.org/protobuf/types/known/anypb" |
13 | | - "sigs.k8s.io/yaml" |
14 | | - |
15 | | - appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1" |
16 | | - "cosmossdk.io/core/internal" |
17 | | - "cosmossdk.io/depinject" |
| 4 | + depinjectappconfig "cosmossdk.io/depinject/appconfig" |
18 | 5 | ) |
19 | 6 |
|
20 | 7 | // LoadJSON loads an app config in JSON format. |
21 | | -func LoadJSON(bz []byte) depinject.Config { |
22 | | - config := &appv1alpha1.Config{} |
23 | | - err := protojson.Unmarshal(bz, config) |
24 | | - if err != nil { |
25 | | - return depinject.Error(err) |
26 | | - } |
27 | | - |
28 | | - return Compose(config) |
29 | | -} |
| 8 | +var LoadJSON = depinjectappconfig.LoadJSON |
30 | 9 |
|
31 | 10 | // LoadYAML loads an app config in YAML format. |
32 | | -func LoadYAML(bz []byte) depinject.Config { |
33 | | - j, err := yaml.YAMLToJSON(bz) |
34 | | - if err != nil { |
35 | | - return depinject.Error(err) |
36 | | - } |
37 | | - |
38 | | - return LoadJSON(j) |
39 | | -} |
| 11 | +var LoadYAML = depinjectappconfig.LoadYAML |
40 | 12 |
|
41 | 13 | // WrapAny marshals a proto message into a proto Any instance |
42 | | -func WrapAny(config protoreflect.ProtoMessage) *anypb.Any { |
43 | | - cfg, err := anyutil.New(config) |
44 | | - if err != nil { |
45 | | - panic(err) |
46 | | - } |
47 | | - |
48 | | - return cfg |
49 | | -} |
| 14 | +var WrapAny = depinjectappconfig.WrapAny |
50 | 15 |
|
51 | 16 | // Compose composes a v1alpha1 app config into a container option by resolving |
52 | 17 | // the required modules and composing their options. |
53 | | -func Compose(appConfig *appv1alpha1.Config) depinject.Config { |
54 | | - opts := []depinject.Config{ |
55 | | - depinject.Supply(appConfig), |
56 | | - } |
57 | | - |
58 | | - for _, module := range appConfig.Modules { |
59 | | - if module.Name == "" { |
60 | | - return depinject.Error(fmt.Errorf("module is missing name")) |
61 | | - } |
62 | | - |
63 | | - if module.Config == nil { |
64 | | - return depinject.Error(fmt.Errorf("module %q is missing a config object", module.Name)) |
65 | | - } |
66 | | - |
67 | | - msgType, err := protoregistry.GlobalTypes.FindMessageByURL(module.Config.TypeUrl) |
68 | | - if err != nil { |
69 | | - return depinject.Error(err) |
70 | | - } |
71 | | - |
72 | | - modules, err := internal.ModulesByProtoMessageName() |
73 | | - if err != nil { |
74 | | - return depinject.Error(err) |
75 | | - } |
76 | | - |
77 | | - init, ok := modules[msgType.Descriptor().FullName()] |
78 | | - if !ok { |
79 | | - modDesc := proto.GetExtension(msgType.Descriptor().Options(), appv1alpha1.E_Module).(*appv1alpha1.ModuleDescriptor) |
80 | | - if modDesc == nil { |
81 | | - return depinject.Error(fmt.Errorf("no module registered for type URL %s and that protobuf type does not have the option %s\n\n%s", |
82 | | - module.Config.TypeUrl, appv1alpha1.E_Module.TypeDescriptor().FullName(), dumpRegisteredModules(modules))) |
83 | | - } |
84 | | - |
85 | | - return depinject.Error(fmt.Errorf("no module registered for type URL %s, did you forget to import %s: find more information on how to make a module ready for app wiring: https://docs.cosmos.network/main/building-modules/depinject\n\n%s", |
86 | | - module.Config.TypeUrl, modDesc.GoImport, dumpRegisteredModules(modules))) |
87 | | - } |
88 | | - |
89 | | - config := init.ConfigProtoMessage.ProtoReflect().Type().New().Interface() |
90 | | - err = anypb.UnmarshalTo(module.Config, config, proto.UnmarshalOptions{}) |
91 | | - if err != nil { |
92 | | - return depinject.Error(err) |
93 | | - } |
94 | | - |
95 | | - opts = append(opts, depinject.Supply(config)) |
96 | | - |
97 | | - for _, provider := range init.Providers { |
98 | | - opts = append(opts, depinject.ProvideInModule(module.Name, provider)) |
99 | | - } |
100 | | - |
101 | | - for _, invoker := range init.Invokers { |
102 | | - opts = append(opts, depinject.InvokeInModule(module.Name, invoker)) |
103 | | - } |
104 | | - |
105 | | - for _, binding := range module.GolangBindings { |
106 | | - opts = append(opts, depinject.BindInterfaceInModule(module.Name, binding.InterfaceType, binding.Implementation)) |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - for _, binding := range appConfig.GolangBindings { |
111 | | - opts = append(opts, depinject.BindInterface(binding.InterfaceType, binding.Implementation)) |
112 | | - } |
113 | | - |
114 | | - return depinject.Configs(opts...) |
115 | | -} |
116 | | - |
117 | | -func dumpRegisteredModules(modules map[protoreflect.FullName]*internal.ModuleInitializer) string { |
118 | | - var mods []string |
119 | | - for name := range modules { |
120 | | - mods = append(mods, " "+string(name)) |
121 | | - } |
122 | | - return fmt.Sprintf("registered modules are:\n%s", strings.Join(mods, "\n")) |
123 | | -} |
| 18 | +var Compose = depinjectappconfig.Compose |
0 commit comments