Skip to content

Commit

Permalink
Refactor DNSConfig.unmarshal(), return error on unknown fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Sep 10, 2020
1 parent 53c94b9 commit acdf402
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,14 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
Strategy: lib.NullDNSStrategy{DNSStrategy: lib.DNSRandom, Valid: true},
}, c.Options.DNS)
}},
// This is functionally invalid, but will error out in validation done in js.parseTTL().
{opts{cli: []string{"--dns", "ttl=-1"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
TTL: null.StringFrom("-1"),
Strategy: lib.NullDNSStrategy{DNSStrategy: lib.DNSFirst, Valid: false},
}, c.Options.DNS)
}},
{opts{cli: []string{"--dns", "ttl=0,strat=nope"}}, exp{cliReadError: true}, nil},
{opts{cli: []string{"--dns", "ttl=0"}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, lib.DNSConfig{
TTL: null.StringFrom("0"),
Expand Down
28 changes: 19 additions & 9 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,25 @@ func (c *DNSConfig) UnmarshalText(text []byte) error {
}

func (c *DNSConfig) unmarshal(params map[string]interface{}) error {
if ttl, ok := params["ttl"]; ok && ttl != "" {
c.TTL = null.StringFrom(fmt.Sprintf("%v", ttl))
}
if strat, ok := params["strategy"]; ok && strat != "" {
if s, err := DNSStrategyString(strat.(string)); err != nil {
return err
} else {
c.Strategy.DNSStrategy = s
c.Strategy.Valid = true
for k, v := range params {
switch k {
case "strategy":
if v == "" {
continue
}
if s, err := DNSStrategyString(v.(string)); err != nil {
return err
} else {
c.Strategy.DNSStrategy = s
c.Strategy.Valid = true
}
case "ttl":
ttlv := fmt.Sprintf("%v", v)
if ttlv != "" {
c.TTL = null.StringFrom(ttlv)
}
default:
return fmt.Errorf("unknown DNS configuration field: %s", k)
}
}
return nil
Expand Down

0 comments on commit acdf402

Please sign in to comment.