-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
46 lines (38 loc) · 776 Bytes
/
struct.go
File metadata and controls
46 lines (38 loc) · 776 Bytes
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
package scan
import (
"errors"
"fmt"
"net"
"reflect"
"time"
)
type Scanner interface {
Scan(src interface{}, tag reflect.StructTag) error
}
type Time time.Time
func (t *Time) Scan(x interface{}, tag reflect.StructTag) error {
bs, ok := x.(string)
if !ok {
return fmt.Errorf("expected string, got %T", x)
}
tt, err := time.Parse(tag.Get("layout"), bs)
if err != nil {
return err
}
*t = Time(tt)
return nil
}
type IP struct{ net.IP }
func (t *IP) Scan(src interface{}, tag reflect.StructTag) error {
if t == nil {
return errors.New("nil pointer")
}
switch src := src.(type) {
case string:
return t.UnmarshalText([]byte(src))
case []byte:
return t.UnmarshalText(src)
default:
return fmt.Errorf("cannot convert from %T to %T", src, t)
}
}