mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
app/config/config.go: adding decodeShorhandShipConfig() for mapstructure decoding.
This commit is contained in:
parent
b517943fee
commit
dbaa520160
1 changed files with 26 additions and 6 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/eosswedenorg/thalos/app/log"
|
||||
|
|
@ -51,15 +52,34 @@ func New() Config {
|
|||
MaxFileSize: 10 * 1000 * 1000, // 10 mb
|
||||
MaxTime: time.Hour * 24,
|
||||
},
|
||||
Ship: ShipConfig{
|
||||
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||
MaxMessagesInFlight: 10,
|
||||
IrreversibleOnly: false,
|
||||
},
|
||||
Ship: defaultShipConfig(""),
|
||||
Redis: RedisConfig{
|
||||
Addr: "localhost:6379",
|
||||
Prefix: "ship",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func defaultShipConfig(url string) ShipConfig {
|
||||
return ShipConfig{
|
||||
Url: url,
|
||||
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
|
||||
MaxMessagesInFlight: 10,
|
||||
IrreversibleOnly: false,
|
||||
}
|
||||
}
|
||||
|
||||
// mapstructure DecodeHook that can parse a shorthand ship config (only string instead of struct.)
|
||||
func decodeShorthandShipConfig(from reflect.Value, to reflect.Value) (interface{}, error) {
|
||||
shipType := reflect.TypeOf(ShipConfig{})
|
||||
|
||||
// If to is a struct and is assignable to a ShipConfig and from is a string.
|
||||
// Then we treat the from value as ShipConfig.Url
|
||||
if to.Kind() == reflect.Struct && to.Type().AssignableTo(shipType) && from.Kind() == reflect.String {
|
||||
return defaultShipConfig(from.String()), nil
|
||||
}
|
||||
|
||||
// If not, decode as normal.
|
||||
return from.Interface(), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue