cosmos-sdk/core/appconfig/config.go
Jacob Gadikian 78e2e35f80
chore: sdk.Int to math.Int (#12132)
## Description

When locally working with golangci-lint, we can see that there were many deprecation warnings about sdk.Int.  This PR resolves that and makes 1-2 other linting related changes.  Issue on linting coming next.

This also moves BitCurve to bitCurve.

I expect that this set of changes will require several pull requests, one of them to the settings for the linter. 

It also does a gofumpt, because we had various formatting-related linters fail, too. 


---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-06-03 12:17:02 +00:00

110 lines
3.2 KiB
Go

package appconfig
import (
"fmt"
"reflect"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/anypb"
"sigs.k8s.io/yaml"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"github.com/cosmos/cosmos-sdk/depinject"
"cosmossdk.io/core/internal"
)
// LoadJSON loads an app config in JSON format.
func LoadJSON(bz []byte) depinject.Config {
config := &appv1alpha1.Config{}
err := protojson.Unmarshal(bz, config)
if err != nil {
return depinject.Error(err)
}
return Compose(config)
}
// LoadYAML loads an app config in YAML format.
func LoadYAML(bz []byte) depinject.Config {
j, err := yaml.YAMLToJSON(bz)
if err != nil {
return depinject.Error(err)
}
return LoadJSON(j)
}
// Compose composes a v1alpha1 app config into a container option by resolving
// the required modules and composing their options.
func Compose(appConfig *appv1alpha1.Config) depinject.Config {
opts := []depinject.Config{
depinject.Supply(appConfig),
}
for _, module := range appConfig.Modules {
if module.Name == "" {
return depinject.Error(fmt.Errorf("module is missing name"))
}
if module.Config == nil {
return depinject.Error(fmt.Errorf("module %q is missing a config object", module.Name))
}
msgType, err := protoregistry.GlobalTypes.FindMessageByURL(module.Config.TypeUrl)
if err != nil {
return depinject.Error(err)
}
modules, err := internal.ModulesByProtoMessageName()
if err != nil {
return depinject.Error(err)
}
init, ok := modules[msgType.Descriptor().FullName()]
if !ok {
modDesc := proto.GetExtension(msgType.Descriptor().Options(), appv1alpha1.E_Module).(*appv1alpha1.ModuleDescriptor)
if modDesc == nil {
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",
module.Config.TypeUrl, appv1alpha1.E_Module.TypeDescriptor().FullName(), dumpRegisteredModules(modules)))
}
return depinject.Error(fmt.Errorf("no module registered for type URL %s, did you forget to import %s\n\n%s",
module.Config.TypeUrl, modDesc.GoImport, dumpRegisteredModules(modules)))
}
config := init.ConfigProtoMessage.ProtoReflect().Type().New().Interface()
err = anypb.UnmarshalTo(module.Config, config, proto.UnmarshalOptions{})
if err != nil {
return depinject.Error(err)
}
opts = append(opts, depinject.Provide(depinject.ProviderDescriptor{
Inputs: nil,
Outputs: []depinject.ProviderOutput{{Type: init.ConfigGoType}},
Fn: func(values []reflect.Value) ([]reflect.Value, error) {
return []reflect.Value{reflect.ValueOf(config)}, nil
},
Location: depinject.LocationFromCaller(0),
}))
for _, provider := range init.Providers {
opts = append(opts, depinject.ProvideInModule(module.Name, provider))
}
}
return depinject.Configs(opts...)
}
func dumpRegisteredModules(modules map[protoreflect.FullName]*internal.ModuleInitializer) string {
var mods []string
for name := range modules {
mods = append(mods, " "+string(name))
}
return fmt.Sprintf("registered modules are:\n%s", strings.Join(mods, "\n"))
}