Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
use UnmarshalTOML to unmarshal post restore op level
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Oct 15, 2020
1 parent fa6b183 commit 24591a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
28 changes: 21 additions & 7 deletions lightning/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,35 @@ 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":
*t = OpLevelRequired
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
}
Expand All @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion lightning/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lightning/config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 24591a9

Please sign in to comment.