this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: type safe MAC address parsing

Khue Doan 9b1b6475 25ed1cc0

+20 -1
+20 -1
internal/hosts/config.go
··· 3 3 import ( 4 4 "encoding/json" 5 5 "fmt" 6 + "net" 6 7 "os" 7 8 ) 8 9 9 10 type Host struct { 10 - MACAddress string `json:"mac_address"` 11 + MACAddress net.HardwareAddr `json:"mac_address"` 11 12 } 12 13 13 14 type HostsConfig map[string]Host 15 + 16 + func (h *Host) UnmarshalJSON(data []byte) error { 17 + var aux struct { 18 + MACAddress string `json:"mac_address"` 19 + } 20 + 21 + if err := json.Unmarshal(data, &aux); err != nil { 22 + return err 23 + } 24 + 25 + mac, err := net.ParseMAC(aux.MACAddress) 26 + if err != nil { 27 + return fmt.Errorf("invalid MAC address %q: %w", aux.MACAddress, err) 28 + } 29 + 30 + h.MACAddress = mac 31 + return nil 32 + } 14 33 15 34 func LoadHostsConfig(filename string) (HostsConfig, error) { 16 35 data, err := os.ReadFile(filename)