-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_avrox.go
76 lines (69 loc) · 1.59 KB
/
do_avrox.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
package main
import (
"fmt"
"io"
"math/big"
"os"
"strconv"
"strings"
"github.com/metatexx/avrox"
"github.com/metatexx/avrox/rawdate"
must "github.com/metatexx/mxx/mustfatal"
)
func doAvroX(r io.Reader, basicSchema string, unQuote, stripLF, quote bool, compressionType string) int {
v := string(must.OkOne(io.ReadAll(r)))
// Unquote the data if asked for
if unQuote {
if len(v) > 1 && v[0:1] != `"` {
v = `"` + v + `"`
}
var errUnquote error
v, errUnquote = strconv.Unquote(v)
if errUnquote != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error while unquoting: %s\n", errUnquote)
return 5
}
}
// we strip a lf if asked for
if stripLF && len(v) > 1 && v[len(v)-1:] == "\n" {
v = v[:len(v)-1]
}
//fmt.Fprintf(os.Stderr, "%q", v)
var data any
switch basicSchema {
case "bytes":
data = []byte(v)
case "string":
data = v
case "rawdate":
data = must.OkOne(rawdate.Parse(rawdate.ISODate, v))
case "decimal":
var ok bool
data, ok = (&big.Rat{}).SetString(strings.TrimSpace(v))
if !ok {
_, _ = fmt.Fprintf(os.Stderr, "(defective)")
}
case "int":
var errAtoi error
data, errAtoi = strconv.Atoi(v)
if errAtoi != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error while reading string as int: %s\n", errAtoi)
return 5
}
}
cID := avrox.CompNone
switch compressionType {
case "snappy", "snappy-block":
cID = avrox.CompSnappy
case "flate":
cID = avrox.CompFlate
case "gzip":
cID = avrox.CompGZip
}
if quote {
fmt.Printf("%q", must.OkOne(avrox.MarshalBasic(data, cID)))
} else {
fmt.Printf("%s", must.OkOne(avrox.MarshalBasic(data, cID)))
}
return 0
}