-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
81 lines (72 loc) · 1.32 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2014, J. Salvador Arias <jsalarias@csnat.unt.edu.ar>
// All rights reserved.
// Distributed under BSD2 license that can be found in LICENSE file.
package gbif
import (
"io"
"github.com/js-arias/jdh/pkg/jdh"
)
// GetScanner scans a single value.
type getScanner struct {
val interface{}
err error
}
func (g *getScanner) Scan(dest interface{}) error {
if g.err != nil {
return g.err
}
switch v := dest.(type) {
case *jdh.Dataset:
*v = *g.val.(*jdh.Dataset)
case *jdh.Specimen:
*v = *g.val.(*jdh.Specimen)
case *jdh.Taxon:
*v = *g.val.(*jdh.Taxon)
}
g.err = io.EOF
return nil
}
// ListScanner scans a list of values.
type listScanner struct {
c chan interface{}
end chan struct{}
err error
}
func (l *listScanner) Scan(dest interface{}) error {
if l.err != nil {
return l.err
}
var val interface{}
select {
case <-l.end:
return l.err
case val = <-l.c:
if val == nil {
l.Close()
return io.EOF
}
switch v := dest.(type) {
case *jdh.Dataset:
*v = *val.(*jdh.Dataset)
case *jdh.Specimen:
*v = *val.(*jdh.Specimen)
case *jdh.Taxon:
*v = *val.(*jdh.Taxon)
}
}
return nil
}
func (l *listScanner) Close() {
if l.err != nil {
return
}
close(l.end)
l.err = io.EOF
}
func (l *listScanner) setErr(err error) {
if l.err != nil {
return
}
close(l.end)
l.err = err
}