-
Notifications
You must be signed in to change notification settings - Fork 7
/
macaddr.go
53 lines (44 loc) · 1.13 KB
/
macaddr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
"net"
)
// MACaddr is an HW address (e.g., IEEE 802 MAC-48, EUI-48, EUI-64)
//
// Note: Since TextUnmarshal is not defined on net.HardwareAddr
// (see: https://github.com/golang/go/issues/29678)
// we need to create an alias type with a custom decoder.
type MACaddr net.HardwareAddr
// UnmarshalJSON deserialize a MAC address in textual form into the MACaddr
// target, e.g.:
//
// "mac-addr": "00:00:5e:00:53:01"
//
// or
//
// "mac-addr": "02:00:5e:10:00:00:00:01"
//
// Supported formats are IEEE 802 MAC-48, EUI-48, EUI-64, e.g.:
//
// 00:00:5e:00:53:01
// 00-00-5e-00-53-01
// 02:00:5e:10:00:00:00:01
// 02-00-5e-10-00-00-00-01
func (o *MACaddr) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
val, err := net.ParseMAC(s)
if err != nil {
return fmt.Errorf("bad MAC address %w", err)
}
*o = MACaddr(val)
return nil
}
func (o MACaddr) MarshalJSON() ([]byte, error) {
return json.Marshal(net.HardwareAddr(o).String())
}