Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions config/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,43 @@ type TargetConfig struct {
}

// UnmarshalYAML implements yaml.Unmarshaler interface.
func (d *TargetConfig) UnmarshalYAML(unmashal func(interface{}) error) error {
func (t *TargetConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// If the input is a string, treat it as the Addr
var s string
if err := unmashal(&s); err == nil {
d.Addr = s
if err := unmarshal(&s); err == nil {
t.Addr = s
t.Labels = nil
return nil
}

var x map[string]map[string]string
if err := unmashal(&x); err != nil {
// Temporary map to capture raw data
raw := make(map[string]string)
if err := unmarshal(&raw); err != nil {
return err
}

for addr, l := range x {
d.Addr = addr
d.Labels = l
// Extract "host" key into Addr
if addr, ok := raw["host"]; ok {
t.Addr = addr
delete(raw, "host") // Remove from labels
}

// Store remaining keys as labels
t.Labels = raw
return nil
}

func (d TargetConfig) MarshalYAML() (interface{}, error) {
if d.Labels == nil {
return d.Addr, nil
func (t TargetConfig) MarshalYAML() (interface{}, error) {
// If there are no labels, just return the address as a string
if len(t.Labels) == 0 {
return t.Addr, nil
}

// Otherwise, construct a map with "host" as Addr and other labels
m := make(map[string]string)
m["host"] = t.Addr
for k, v := range t.Labels {
m[k] = v
}
ret := make(map[string]map[string]string)
ret[d.Addr] = d.Labels
return ret, nil

return m, nil
}
4 changes: 2 additions & 2 deletions config/testdata/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ targets:
- 8.8.8.8
- 8.8.4.4
- 2001:4860:4860::8888
- "2001:4860:4860::8844":
foo: "bar"
- host: "2001:4860:4860::8844"
foo: "bar"

dns:
refresh: 2m15s
Expand Down