1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-17 04:30:03 +02:00

internal/config: remove shorthand ship config (where it is possible to have "ship" key only contain a url string instead of struct)

It makes parsing harder and i don't think anyone uses it and its not that useful tbh.
This commit is contained in:
Henrik Hautakoski 2024-02-18 16:08:18 +01:00
parent 4a4489e2be
commit e54a4fa929
3 changed files with 6 additions and 78 deletions

View file

@ -109,7 +109,6 @@ func (b *Builder) Build() (*Config, error) {
mapstructure.TextUnmarshallerHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
decodeShorthandShipConfig,
)
err := v.Unmarshal(&conf, viper.DecodeHook(decoders))

View file

@ -7,8 +7,6 @@ import (
"github.com/eosswedenorg/thalos/internal/log"
"github.com/stretchr/testify/require"
shipclient "github.com/eosswedenorg-go/antelope-ship-client"
)
func TestBuilder(t *testing.T) {
@ -154,55 +152,6 @@ func TestBuilder_NilSource(t *testing.T) {
require.EqualError(t, err, "Config not set")
}
func TestBuilder_WithShorthandShipUrl(t *testing.T) {
expected := Config{
Name: "ship-reader-1",
Api: "http://127.0.0.1:8080",
MessageCodec: "json",
Log: log.Config{
MaxFileSize: 10 * 1000 * 1000, // 10 mb
MaxTime: time.Hour * 24,
},
Ship: ShipConfig{
Url: "127.0.0.1:8089",
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
Telegram: TelegramConfig{
Id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw",
Channel: -123456789,
},
Redis: RedisConfig{
Addr: "localhost:6379",
Password: "passwd",
DB: 4,
Prefix: "some::ship",
},
}
builder := NewBuilder()
builder.SetSource(bytes.NewBuffer([]byte(`
name: "ship-reader-1"
api: "http://127.0.0.1:8080"
ship: "127.0.0.1:8089"
telegram:
id: "110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw"
channel: -123456789
redis:
addr: "localhost:6379"
password: "passwd"
db: 4
prefix: "some::ship"
`)))
cfg, err := builder.Build()
require.NoError(t, err)
require.Equal(t, &expected, cfg)
}
func TestBuilder_Flags(t *testing.T) {
flags := GetFlags()

View file

@ -1,7 +1,6 @@
package config
import (
"reflect"
"time"
"github.com/eosswedenorg/thalos/internal/log"
@ -52,34 +51,15 @@ func New() Config {
MaxFileSize: 10 * 1000 * 1000, // 10 mb
MaxTime: time.Hour * 24,
},
Ship: defaultShipConfig(""),
Ship: ShipConfig{
StartBlockNum: shipclient.NULL_BLOCK_NUMBER,
EndBlockNum: shipclient.NULL_BLOCK_NUMBER,
MaxMessagesInFlight: 10,
IrreversibleOnly: false,
},
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
}