-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
88 lines (72 loc) · 1.66 KB
/
builder.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
82
83
84
85
86
87
88
package xopconsole
import (
"encoding/json"
"time"
"github.com/xoplog/xop-go/xopat"
"github.com/xoplog/xop-go/xopbase"
"github.com/xoplog/xop-go/xoptrace"
"github.com/xoplog/xop-go/xoputil"
)
type Builder struct {
xoputil.JBuilder
encoder *json.Encoder
}
func (b *Builder) Init() {
b.encoder = json.NewEncoder(&b.JBuilder)
b.encoder.SetEscapeHTML(false)
}
func (b *Builder) AnyCommon(v xopbase.ModelArg) {
v.Encode()
b.AppendByte('(')
b.AddInt64(int64(len(v.Encoded)))
b.AppendByte(')')
b.AppendBytes(v.Encoded)
b.AppendString(v.Encoding.String())
b.AppendByte('/')
b.AddString(v.ModelType)
}
func (b *Builder) AttributeAny(v xopbase.ModelArg) {
b.AnyCommon(v)
}
func (b *Builder) AttributeEnum(v xopat.Enum) {
b.AddInt64(v.Int64())
b.B = append(b.B, '/')
b.AddConsoleString(v.String())
}
func (b *Builder) AttributeTime(t time.Time) {
b.B = append(b.B, '"')
b.B = DefaultTimeFormatter(b.B, t)
b.B = append(b.B, '"')
}
func (b *Builder) AttributeBool(v bool) {
if v {
b.B = append(b.B, 't')
} else {
b.B = append(b.B, 'f')
}
}
func (b *Builder) AttributeInt64(v int64) {
b.AddInt64(v)
}
func (b *Builder) AttributeString(v string) {
b.AddString(v)
}
func (b *Builder) AttributeFloat64(f float64) {
b.AddFloat64(f)
}
func (b *Builder) AttributeDuration(v time.Duration) {
b.AppendString(v.String())
}
func (b *Builder) AttributeLink(v xoptrace.Trace) {
b.AddSafeString(v.String())
}
/* TODO
func DefaultTimeFormatter2(b []byte, t time.Time) []byte {
b = fasttime.AppendStrftime(b, fasttime.RFC3339Nano, t)
return b
}
*/
func DefaultTimeFormatter(b []byte, t time.Time) []byte {
b = append(b, []byte(t.Format(time.RFC3339Nano))...)
return b
}