1package zerolog
2
3import (
4	"net"
5	"sort"
6	"time"
7)
8
9func appendFields(dst []byte, fields map[string]interface{}) []byte {
10	keys := make([]string, 0, len(fields))
11	for key := range fields {
12		keys = append(keys, key)
13	}
14	sort.Strings(keys)
15	for _, key := range keys {
16		dst = enc.AppendKey(dst, key)
17		val := fields[key]
18		if val, ok := val.(LogObjectMarshaler); ok {
19			e := newEvent(nil, 0)
20			e.buf = e.buf[:0]
21			e.appendObject(val)
22			dst = append(dst, e.buf...)
23			putEvent(e)
24			continue
25		}
26		switch val := val.(type) {
27		case string:
28			dst = enc.AppendString(dst, val)
29		case []byte:
30			dst = enc.AppendBytes(dst, val)
31		case error:
32			marshaled := ErrorMarshalFunc(val)
33			switch m := marshaled.(type) {
34			case LogObjectMarshaler:
35				e := newEvent(nil, 0)
36				e.buf = e.buf[:0]
37				e.appendObject(m)
38				dst = append(dst, e.buf...)
39				putEvent(e)
40			case error:
41				dst = enc.AppendString(dst, m.Error())
42			case string:
43				dst = enc.AppendString(dst, m)
44			default:
45				dst = enc.AppendInterface(dst, m)
46			}
47		case []error:
48			dst = enc.AppendArrayStart(dst)
49			for i, err := range val {
50				marshaled := ErrorMarshalFunc(err)
51				switch m := marshaled.(type) {
52				case LogObjectMarshaler:
53					e := newEvent(nil, 0)
54					e.buf = e.buf[:0]
55					e.appendObject(m)
56					dst = append(dst, e.buf...)
57					putEvent(e)
58				case error:
59					dst = enc.AppendString(dst, m.Error())
60				case string:
61					dst = enc.AppendString(dst, m)
62				default:
63					dst = enc.AppendInterface(dst, m)
64				}
65
66				if i < (len(val) - 1) {
67					enc.AppendArrayDelim(dst)
68				}
69			}
70			dst = enc.AppendArrayEnd(dst)
71		case bool:
72			dst = enc.AppendBool(dst, val)
73		case int:
74			dst = enc.AppendInt(dst, val)
75		case int8:
76			dst = enc.AppendInt8(dst, val)
77		case int16:
78			dst = enc.AppendInt16(dst, val)
79		case int32:
80			dst = enc.AppendInt32(dst, val)
81		case int64:
82			dst = enc.AppendInt64(dst, val)
83		case uint:
84			dst = enc.AppendUint(dst, val)
85		case uint8:
86			dst = enc.AppendUint8(dst, val)
87		case uint16:
88			dst = enc.AppendUint16(dst, val)
89		case uint32:
90			dst = enc.AppendUint32(dst, val)
91		case uint64:
92			dst = enc.AppendUint64(dst, val)
93		case float32:
94			dst = enc.AppendFloat32(dst, val)
95		case float64:
96			dst = enc.AppendFloat64(dst, val)
97		case time.Time:
98			dst = enc.AppendTime(dst, val, TimeFieldFormat)
99		case time.Duration:
100			dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
101		case *string:
102			if val != nil {
103				dst = enc.AppendString(dst, *val)
104			} else {
105				dst = enc.AppendNil(dst)
106			}
107		case *bool:
108			if val != nil {
109				dst = enc.AppendBool(dst, *val)
110			} else {
111				dst = enc.AppendNil(dst)
112			}
113		case *int:
114			if val != nil {
115				dst = enc.AppendInt(dst, *val)
116			} else {
117				dst = enc.AppendNil(dst)
118			}
119		case *int8:
120			if val != nil {
121				dst = enc.AppendInt8(dst, *val)
122			} else {
123				dst = enc.AppendNil(dst)
124			}
125		case *int16:
126			if val != nil {
127				dst = enc.AppendInt16(dst, *val)
128			} else {
129				dst = enc.AppendNil(dst)
130			}
131		case *int32:
132			if val != nil {
133				dst = enc.AppendInt32(dst, *val)
134			} else {
135				dst = enc.AppendNil(dst)
136			}
137		case *int64:
138			if val != nil {
139				dst = enc.AppendInt64(dst, *val)
140			} else {
141				dst = enc.AppendNil(dst)
142			}
143		case *uint:
144			if val != nil {
145				dst = enc.AppendUint(dst, *val)
146			} else {
147				dst = enc.AppendNil(dst)
148			}
149		case *uint8:
150			if val != nil {
151				dst = enc.AppendUint8(dst, *val)
152			} else {
153				dst = enc.AppendNil(dst)
154			}
155		case *uint16:
156			if val != nil {
157				dst = enc.AppendUint16(dst, *val)
158			} else {
159				dst = enc.AppendNil(dst)
160			}
161		case *uint32:
162			if val != nil {
163				dst = enc.AppendUint32(dst, *val)
164			} else {
165				dst = enc.AppendNil(dst)
166			}
167		case *uint64:
168			if val != nil {
169				dst = enc.AppendUint64(dst, *val)
170			} else {
171				dst = enc.AppendNil(dst)
172			}
173		case *float32:
174			if val != nil {
175				dst = enc.AppendFloat32(dst, *val)
176			} else {
177				dst = enc.AppendNil(dst)
178			}
179		case *float64:
180			if val != nil {
181				dst = enc.AppendFloat64(dst, *val)
182			} else {
183				dst = enc.AppendNil(dst)
184			}
185		case *time.Time:
186			if val != nil {
187				dst = enc.AppendTime(dst, *val, TimeFieldFormat)
188			} else {
189				dst = enc.AppendNil(dst)
190			}
191		case *time.Duration:
192			if val != nil {
193				dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
194			} else {
195				dst = enc.AppendNil(dst)
196			}
197		case []string:
198			dst = enc.AppendStrings(dst, val)
199		case []bool:
200			dst = enc.AppendBools(dst, val)
201		case []int:
202			dst = enc.AppendInts(dst, val)
203		case []int8:
204			dst = enc.AppendInts8(dst, val)
205		case []int16:
206			dst = enc.AppendInts16(dst, val)
207		case []int32:
208			dst = enc.AppendInts32(dst, val)
209		case []int64:
210			dst = enc.AppendInts64(dst, val)
211		case []uint:
212			dst = enc.AppendUints(dst, val)
213		// case []uint8:
214		// 	dst = enc.AppendUints8(dst, val)
215		case []uint16:
216			dst = enc.AppendUints16(dst, val)
217		case []uint32:
218			dst = enc.AppendUints32(dst, val)
219		case []uint64:
220			dst = enc.AppendUints64(dst, val)
221		case []float32:
222			dst = enc.AppendFloats32(dst, val)
223		case []float64:
224			dst = enc.AppendFloats64(dst, val)
225		case []time.Time:
226			dst = enc.AppendTimes(dst, val, TimeFieldFormat)
227		case []time.Duration:
228			dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
229		case nil:
230			dst = enc.AppendNil(dst)
231		case net.IP:
232			dst = enc.AppendIPAddr(dst, val)
233		case net.IPNet:
234			dst = enc.AppendIPPrefix(dst, val)
235		case net.HardwareAddr:
236			dst = enc.AppendMACAddr(dst, val)
237		default:
238			dst = enc.AppendInterface(dst, val)
239		}
240	}
241	return dst
242}
243