Make ParseZone more go like

This commit is contained in:
Miek Gieben 2011-12-19 19:20:55 +01:00
parent e2b080d1b1
commit 362e606bf2
2 changed files with 15 additions and 16 deletions

View File

@ -101,8 +101,7 @@ z2.miek.nl. 86400 IN NSEC miek.nl. TXT RRSIG NSEC
$TTL 100
z3.miek.nl. IN NSEC miek.nl. TXT RRSIG NSEC`
// Need to implementen owner substitution in the lexer.
to := make(chan Token)
go ParseZone(strings.NewReader(zone), to)
to := ParseZone(strings.NewReader(zone))
i := 0
for x := range to {
if x.Error == nil {
@ -197,8 +196,7 @@ func BenchmarkZoneParsing(b *testing.B) {
return
}
defer f.Close()
to := make(chan Token)
go ParseZone(f, to)
to := ParseZone(f)
for x := range to {
x = x
}
@ -210,9 +208,8 @@ func TestZoneParsing(t *testing.T) {
return
}
defer f.Close()
to := make(chan Token)
start := time.Now().UnixNano()
go ParseZone(f, to)
to := ParseZone(f)
var i int
for x := range to {
t.Logf("%s\n", x.Rr)
@ -229,9 +226,8 @@ func TestZoneParsingBigZonePrint(t *testing.T) {
return
}
defer f.Close()
to := make(chan Token)
start := time.Now().UnixNano()
go ParseZone(f, to)
to := ParseZone(f)
var i int
for x := range to {
if x.Rr != nil {
@ -250,9 +246,8 @@ func TestZoneParsingBigZone(t *testing.T) {
return
}
defer f.Close()
to := make(chan Token)
start := time.Now().UnixNano()
go ParseZone(f, to)
to := ParseZone(f)
var i int
for x := range to {
x = x

View File

@ -84,9 +84,9 @@ type Token struct {
func NewRR(s string) (RR, error) {
t := make(chan Token)
if s[len(s)-1] != '\n' { // We need a closing newline
go ParseZone(strings.NewReader(s+"\n"), t)
t = ParseZone(strings.NewReader(s+"\n"))
} else {
go ParseZone(strings.NewReader(s), t)
t= ParseZone(strings.NewReader(s))
}
r := <-t
if r.Error != nil {
@ -96,10 +96,14 @@ func NewRR(s string) (RR, error) {
}
// ParseZone reads a RFC 1035 zone from r. It returns each parsed RR or on error
// on the channel t. The channel t is closed by ParseZone when the end of r is reached.
// Basic usage pattern:
// go ParseZone
func ParseZone(r io.Reader, t chan Token) {
// on the returned channel. The channel t is closed by ParseZone when the end of r is reached.
func ParseZone(r io.Reader) (chan Token) {
t := make(chan Token)
go parseZone(r, t)
return t
}
func parseZone(r io.Reader, t chan Token) {
defer close(t)
var s scanner.Scanner
c := make(chan lex)