From 9c3a117004213e347403f476dd97dc03cd460f9a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 12 Jan 2015 22:33:49 +0000 Subject: [PATCH] Add some more string conversion --- format.go | 41 +++++++++++++++++++++++++++++++++++++++-- parse_test.go | 5 +++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/format.go b/format.go index fcc61b67..2219618c 100644 --- a/format.go +++ b/format.go @@ -22,6 +22,7 @@ package dns import ( "fmt" + "net" "reflect" "strconv" ) @@ -33,15 +34,51 @@ func Field(r RR, i int) string { return "" } d := reflect.ValueOf(r).Elem().Field(i) - switch d.Kind() { + switch k := d.Kind(); k { case reflect.String: return d.String() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return strconv.FormatInt(d.Int(), 10) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: return strconv.FormatUint(d.Uint(), 10) - // more to be added + case reflect.Slice: + switch reflect.ValueOf(r).Elem().Type().Field(i).Tag { + case `dns:"a"`: + // println(d.Len()), Hmm store this as 16 bytes + if d.Len() < net.IPv6len { + return "" + } + ip := net.IPv4(byte(d.Index(12).Uint()), + byte(d.Index(13).Uint()), + byte(d.Index(14).Uint()), + byte(d.Index(15).Uint())) + return ip.String() + case `dns:"aaaa"`: + return net.IP{ + byte(d.Index(0).Uint()), + byte(d.Index(1).Uint()), + byte(d.Index(2).Uint()), + byte(d.Index(3).Uint()), + byte(d.Index(4).Uint()), + byte(d.Index(5).Uint()), + byte(d.Index(6).Uint()), + byte(d.Index(7).Uint()), + byte(d.Index(8).Uint()), + byte(d.Index(9).Uint()), + byte(d.Index(10).Uint()), + byte(d.Index(11).Uint()), + byte(d.Index(12).Uint()), + byte(d.Index(13).Uint()), + byte(d.Index(14).Uint()), + byte(d.Index(15).Uint()), + }.String() + // []uint16 (nsec,wks) + // []string + } + default: + println("dns: unknown field kind", k.String()) } + return "" } diff --git a/parse_test.go b/parse_test.go index e1240f2c..0f9bbae6 100644 --- a/parse_test.go +++ b/parse_test.go @@ -1375,4 +1375,9 @@ func TestPrintfVerbsRdata(t *testing.T) { if Field(x, 2) != "mx.miek.nl." { t.Errorf("should be mx.miek.nl.") } + x, _ = NewRR("www.miek.nl. IN A 127.0.0.1") + println(Field(x, 1)) + + x, _ = NewRR("www.miek.nl. IN AAAA ::1") + println(Field(x, 1)) }