From e54a4fa929f5a31963701915a21b1aeefa105365 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 18 Feb 2024 16:08:18 +0100 Subject: [PATCH] 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. --- internal/config/builder.go | 1 - internal/config/builder_test.go | 51 --------------------------------- internal/config/config.go | 32 ++++----------------- 3 files changed, 6 insertions(+), 78 deletions(-) diff --git a/internal/config/builder.go b/internal/config/builder.go index 32ded36..d2ab2bf 100644 --- a/internal/config/builder.go +++ b/internal/config/builder.go @@ -109,7 +109,6 @@ func (b *Builder) Build() (*Config, error) { mapstructure.TextUnmarshallerHookFunc(), mapstructure.StringToTimeDurationHookFunc(), mapstructure.StringToSliceHookFunc(","), - decodeShorthandShipConfig, ) err := v.Unmarshal(&conf, viper.DecodeHook(decoders)) diff --git a/internal/config/builder_test.go b/internal/config/builder_test.go index 377c457..3f52548 100644 --- a/internal/config/builder_test.go +++ b/internal/config/builder_test.go @@ -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() diff --git a/internal/config/config.go b/internal/config/config.go index e137352..8edacdb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 -}