1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17
18 package conv
19
20 import (
21 "bytes"
22 "errors"
23 "fmt"
24
25 "perkeep.org/pkg/blob"
26
27 "go4.org/strutil"
28 )
29
30 func ParseFields(v []byte, dst ...interface{}) error {
31 for i, dv := range dst {
32 thisv := v
33 if i < len(dst)-1 {
34 sp := bytes.IndexByte(v, ' ')
35 if sp == -1 {
36 return fmt.Errorf("missing space following field index %d", i)
37 }
38 thisv = v[:sp]
39 v = v[sp+1:]
40 }
41 switch dv := dv.(type) {
42 case *blob.Ref:
43 br, ok := blob.ParseBytes(thisv)
44 if !ok {
45
46 }
47 *dv = br
48 case *uint32:
49 n, err := strutil.ParseUintBytes(thisv, 10, 32)
50 if err != nil {
51 return err
52 }
53 *dv = uint32(n)
54 case *uint64:
55 n, err := strutil.ParseUintBytes(thisv, 10, 64)
56 if err != nil {
57 return err
58 }
59 *dv = n
60 case *int64:
61 n, err := strutil.ParseUintBytes(thisv, 10, 64)
62 if err != nil {
63 return err
64 }
65 if int64(n) < 0 {
66 return errors.New("conv: negative numbers not accepted with int64 dest type")
67 }
68 *dv = int64(n)
69 default:
70 return fmt.Errorf("conv: unsupported target pointer type %T", dv)
71 }
72 }
73 return nil
74 }