Files
go-dhcp/pkg/types/json.go
2024-01-09 12:23:41 +01:00

47 lines
947 B
Go

package types
import (
"fmt"
"net"
"strconv"
util "github.com/adrianokf/go-dhcp/pkg/util"
)
func (txid *TxId) UnmarshalText(text []byte) (err error) {
result, err := strconv.ParseUint(string(text), 16, 32)
if err != nil {
return err
}
copy(txid[:], util.U32ToByte(uint32(result)))
return nil
}
func (id TxId) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%x", id)), nil
}
func (addr *Ipv4Addr) UnmarshalText(text []byte) (err error) {
ip := net.ParseIP(string(text))
fmt.Println(ip)
if ip == nil {
return fmt.Errorf("could not parse IP address: '%s'", text)
}
*addr = Ipv4AddrFromNetIP(ip)
return nil
}
func (addr Ipv4Addr) MarshalText() ([]byte, error) {
return addr.ToNetIPv4().MarshalText()
}
func (addr HwAddr) MarshalText() ([]byte, error) {
return []byte(addr.String()), nil
}
func (addr *HwAddr) UnmarshalText(text []byte) (err error) {
*addr, err = HwAddrFromString(string(text))
return err
}