1package zerolog
2
3import (
4	"sync"
5	"time"
6
7	"github.com/rs/zerolog/internal/json"
8)
9
10var arrayPool = &sync.Pool{
11	New: func() interface{} {
12		return &Array{
13			buf: make([]byte, 0, 500),
14		}
15	},
16}
17
18type Array struct {
19	buf []byte
20}
21
22// Arr creates an array to be added to an Event or Context.
23func Arr() *Array {
24	a := arrayPool.Get().(*Array)
25	a.buf = a.buf[:0]
26	return a
27}
28
29func (*Array) MarshalZerologArray(*Array) {
30}
31
32func (a *Array) write(dst []byte) []byte {
33	if len(a.buf) == 0 {
34		dst = append(dst, `[]`...)
35	} else {
36		a.buf[0] = '['
37		dst = append(append(dst, a.buf...), ']')
38	}
39	arrayPool.Put(a)
40	return dst
41}
42
43// Object marshals an object that implement the LogObjectMarshaler
44// interface and append it to the array.
45func (a *Array) Object(obj LogObjectMarshaler) *Array {
46	a.buf = append(a.buf, ',')
47	e := Dict()
48	obj.MarshalZerologObject(e)
49	e.buf = append(e.buf, '}')
50	a.buf = append(a.buf, e.buf...)
51	return a
52}
53
54// Str append the val as a string to the array.
55func (a *Array) Str(val string) *Array {
56	a.buf = json.AppendString(append(a.buf, ','), val)
57	return a
58}
59
60// Bytes append the val as a string to the array.
61func (a *Array) Bytes(val []byte) *Array {
62	a.buf = json.AppendBytes(append(a.buf, ','), val)
63	return a
64}
65
66// Err append the err as a string to the array.
67func (a *Array) Err(err error) *Array {
68	a.buf = json.AppendError(append(a.buf, ','), err)
69	return a
70}
71
72// Bool append the val as a bool to the array.
73func (a *Array) Bool(b bool) *Array {
74	a.buf = json.AppendBool(append(a.buf, ','), b)
75	return a
76}
77
78// Int append i as a int to the array.
79func (a *Array) Int(i int) *Array {
80	a.buf = json.AppendInt(append(a.buf, ','), i)
81	return a
82}
83
84// Int8 append i as a int8 to the array.
85func (a *Array) Int8(i int8) *Array {
86	a.buf = json.AppendInt8(append(a.buf, ','), i)
87	return a
88}
89
90// Int16 append i as a int16 to the array.
91func (a *Array) Int16(i int16) *Array {
92	a.buf = json.AppendInt16(append(a.buf, ','), i)
93	return a
94}
95
96// Int32 append i as a int32 to the array.
97func (a *Array) Int32(i int32) *Array {
98	a.buf = json.AppendInt32(append(a.buf, ','), i)
99	return a
100}
101
102// Int64 append i as a int64 to the array.
103func (a *Array) Int64(i int64) *Array {
104	a.buf = json.AppendInt64(append(a.buf, ','), i)
105	return a
106}
107
108// Uint append i as a uint to the array.
109func (a *Array) Uint(i uint) *Array {
110	a.buf = json.AppendUint(append(a.buf, ','), i)
111	return a
112}
113
114// Uint8 append i as a uint8 to the array.
115func (a *Array) Uint8(i uint8) *Array {
116	a.buf = json.AppendUint8(append(a.buf, ','), i)
117	return a
118}
119
120// Uint16 append i as a uint16 to the array.
121func (a *Array) Uint16(i uint16) *Array {
122	a.buf = json.AppendUint16(append(a.buf, ','), i)
123	return a
124}
125
126// Uint32 append i as a uint32 to the array.
127func (a *Array) Uint32(i uint32) *Array {
128	a.buf = json.AppendUint32(append(a.buf, ','), i)
129	return a
130}
131
132// Uint64 append i as a uint64 to the array.
133func (a *Array) Uint64(i uint64) *Array {
134	a.buf = json.AppendUint64(append(a.buf, ','), i)
135	return a
136}
137
138// Float32 append f as a float32 to the array.
139func (a *Array) Float32(f float32) *Array {
140	a.buf = json.AppendFloat32(append(a.buf, ','), f)
141	return a
142}
143
144// Float64 append f as a float64 to the array.
145func (a *Array) Float64(f float64) *Array {
146	a.buf = json.AppendFloat64(append(a.buf, ','), f)
147	return a
148}
149
150// Time append t formated as string using zerolog.TimeFieldFormat.
151func (a *Array) Time(t time.Time) *Array {
152	a.buf = json.AppendTime(append(a.buf, ','), t, TimeFieldFormat)
153	return a
154}
155
156// Dur append d to the array.
157func (a *Array) Dur(d time.Duration) *Array {
158	a.buf = json.AppendDuration(append(a.buf, ','), d, DurationFieldUnit, DurationFieldInteger)
159	return a
160}
161
162// Interface append i marshaled using reflection.
163func (a *Array) Interface(i interface{}) *Array {
164	if obj, ok := i.(LogObjectMarshaler); ok {
165		return a.Object(obj)
166	}
167	a.buf = json.AppendInterface(append(a.buf, ','), i)
168	return a
169}
170