From 24591a93fbc0f2d9f4a024d53156c19d764e05af Mon Sep 17 00:00:00 2001 From: glorv Date: Thu, 15 Oct 2020 11:01:15 +0800 Subject: [PATCH] use UnmarshalTOML to unmarshal post restore op level --- lightning/config/config.go | 28 +++++++++++++++++++++------- lightning/config/config_test.go | 8 +++++++- lightning/config/global.go | 4 ++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lightning/config/config.go b/lightning/config/config.go index 1715f0cba..43084b5b9 100644 --- a/lightning/config/config.go +++ b/lightning/config/config.go @@ -135,9 +135,27 @@ const ( OpLevelRequired ) -func (t *PostOpLevel) UnmarshalText(text []byte) error { +func (t *PostOpLevel) UnmarshalTOML(v interface{}) error { + switch v.(type) { + case bool: + enabled := v.(bool) + if enabled { + *t = OpLevelRequired + } else { + *t = OpLevelOff + } + case string: + strValue := v.(string) + return t.FromStringValue(strValue) + default: + return errors.Errorf("invalid op level '%v', please choose valid option between ['off', 'optional', 'required']", v) + } + return nil +} - switch strings.ToLower(strings.Trim(string(text), `'"`)) { +// parser command line parameter +func (t *PostOpLevel) FromStringValue(s string) error { + switch strings.ToLower(s) { case "off", "false": *t = OpLevelOff case "required", "true": @@ -145,7 +163,7 @@ func (t *PostOpLevel) UnmarshalText(text []byte) error { case "optional": *t = OPLevelOptional default: - return errors.Errorf("invalid post process type '%s', please choose valid option between ['off', 'optional', 'required']", string(text)) + return errors.Errorf("invalid op level '%s', please choose valid option between ['off', 'optional', 'required']", s) } return nil } @@ -154,10 +172,6 @@ func (t *PostOpLevel) MarshalJSON() ([]byte, error) { return []byte(`"` + t.String() + `"`), nil } -func (t *PostOpLevel) UnmarshalJSON(data []byte) error { - return t.UnmarshalText(data) -} - func (t PostOpLevel) String() string { switch t { case OpLevelOff: diff --git a/lightning/config/config_test.go b/lightning/config/config_test.go index c2adb45d5..dab378753 100644 --- a/lightning/config/config_test.go +++ b/lightning/config/config_test.go @@ -550,7 +550,13 @@ func (s *configTestSuite) TestTomlPostRestore(c *C) { [post-restore] checksum = "req" `)) - c.Assert(err, ErrorMatches, regexp.QuoteMeta("invalid post process type 'req', please choose valid option between ['off', 'optional', 'required']")) + c.Assert(err, ErrorMatches, regexp.QuoteMeta("invalid op level 'req', please choose valid option between ['off', 'optional', 'required']")) + + err = cfg.LoadFromTOML([]byte(` + [post-restore] + analyze = 123 + `)) + c.Assert(err, ErrorMatches, regexp.QuoteMeta("invalid op level '123', please choose valid option between ['off', 'optional', 'required']")) kvMap := map[string]config.PostOpLevel{ `"off"`: config.OpLevelOff, diff --git a/lightning/config/global.go b/lightning/config/global.go index b8b1dd563..013c682c4 100644 --- a/lightning/config/global.go +++ b/lightning/config/global.go @@ -247,10 +247,10 @@ func LoadGlobalConfig(args []string, extraFlags func(*flag.FlagSet)) (*GlobalCon cfg.Mydumper.NoSchema = true } if *checksum != "" { - _ = cfg.PostRestore.Checksum.UnmarshalText([]byte(*checksum)) + _ = cfg.PostRestore.Checksum.FromStringValue(*checksum) } if *analyze != "" { - _ = cfg.PostRestore.Analyze.UnmarshalText([]byte(*checksum)) + _ = cfg.PostRestore.Analyze.FromStringValue(*analyze) } if cfg.App.StatusAddr == "" && cfg.App.PProfPort != 0 { cfg.App.StatusAddr = fmt.Sprintf(":%d", cfg.App.PProfPort)