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

View File

@ -84,9 +84,9 @@ type Token struct {
func NewRR(s string) (RR, error) { func NewRR(s string) (RR, error) {
t := make(chan Token) t := make(chan Token)
if s[len(s)-1] != '\n' { // We need a closing newline 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 { } else {
go ParseZone(strings.NewReader(s), t) t= ParseZone(strings.NewReader(s))
} }
r := <-t r := <-t
if r.Error != nil { 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 // 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. // on the returned channel. The channel t is closed by ParseZone when the end of r is reached.
// Basic usage pattern: func ParseZone(r io.Reader) (chan Token) {
// go ParseZone t := make(chan Token)
func ParseZone(r io.Reader, t chan Token) { go parseZone(r, t)
return t
}
func parseZone(r io.Reader, t chan Token) {
defer close(t) defer close(t)
var s scanner.Scanner var s scanner.Scanner
c := make(chan lex) c := make(chan lex)