Skip to content

Commit

Permalink
fix(config/parameter): add more tests and fix command line parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
incubator4 authored Jan 29, 2024
1 parent 421eede commit 4c4b1b8
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 421 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func runIndexer(ctx context.Context, config *config.File, databaseClient databas

for _, nodeConfig := range config.Node.Decentralized {
if nodeConfig.Network == network && nodeConfig.Worker == worker {
if nodeConfig.Parameters == nil && parameters == "{}" || nodeConfig.Parameters != nil && strings.EqualFold(nodeConfig.Parameters.String(), parameters) {
if nodeConfig.Parameters == nil && parameters == "{}" || *(nodeConfig.Parameters) != nil && strings.EqualFold(nodeConfig.Parameters.String(), parameters) {
server, err := indexer.NewServer(ctx, nodeConfig, databaseClient, streamClient)
if err != nil {
return fmt.Errorf("new server: %w", err)
Expand Down
41 changes: 30 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/rss3-network/serving-node/schema/filter"
"github.com/samber/lo"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -117,17 +116,9 @@ func (c *Module) ID() string {
return id
}

var _ fmt.Stringer = (*Options)(nil)
//var _ fmt.Stringer = (*Options)(nil)

type Options struct {
*yaml.Node
}

func (o *Options) UnmarshalYAML(node *yaml.Node) error {
o.Node = node

return nil
}
type Options map[string]any

func (o *Options) String() string {
var buffer map[string]any
Expand All @@ -147,6 +138,15 @@ func (o *Options) String() string {
return string(lo.Must(json.Marshal(buffer)))
}

func (o *Options) Decode(v interface{}) error {
jsonStr, err := json.Marshal(*o)
if err != nil {
return err
}

return json.Unmarshal(jsonStr, v)
}

func Setup(configName string) (*File, error) {
configType := path.Ext(configName)[1:]

Expand Down Expand Up @@ -201,6 +201,7 @@ func _Setup(configName, configType string, v *viper.Viper) (*File, error) {
if err := v.Unmarshal(&configFile, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
filter.NetworkHookFunc(),
filter.WorkerHookFunc(),
EvmAddressHookFunc(),
))); err != nil {
return nil, fmt.Errorf("unmarshal config file: %w", err)
}
Expand Down Expand Up @@ -256,3 +257,21 @@ func getAllKeys(iface interface{}, parts ...string) []string {

return keys
}

func EvmAddressHookFunc() mapstructure.DecodeHookFuncType {
return func(
f reflect.Type, // data type
t reflect.Type, // target data type
data interface{}, // raw data
) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}

if t.Kind() != reflect.TypeOf(common.Address{}).Kind() {
return data, nil
}

return common.HexToAddress(data.(string)), nil
}
}
Loading

0 comments on commit 4c4b1b8

Please sign in to comment.