github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/qdrmanagement/entities/common/socketaddressfamily.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "strings" 7 ) 8 9 // SocketAddressFamilyType 10 type SocketAddressFamilyType int 11 12 const ( 13 SocketAddressFamilyIPv4 SocketAddressFamilyType = iota 14 SocketAddressFamilyIPv6 15 ) 16 17 // UnmarshalJSON returns the appropriate SocketAddressFamilyType for parsed string 18 func (a *SocketAddressFamilyType) UnmarshalJSON(b []byte) error { 19 var s string 20 21 if len(b) == 0 { 22 return nil 23 } 24 if b[0] != '"' { 25 b = []byte(strconv.Quote(string(b))) 26 } 27 if err := json.Unmarshal(b, &s); err != nil { 28 return err 29 } 30 switch strings.ToLower(s) { 31 case "IPv6": 32 *a = SocketAddressFamilyIPv6 33 default: 34 *a = SocketAddressFamilyIPv4 35 } 36 return nil 37 } 38 39 // MarshalJSON returns the string representation of SocketAddressFamilyType 40 func (a SocketAddressFamilyType) MarshalJSON() ([]byte, error) { 41 var s string 42 switch a { 43 case SocketAddressFamilyIPv6: 44 s = "IPv6" 45 default: 46 s = "IPv4" 47 } 48 return json.Marshal(s) 49 }