Skip to content

Commit 1a51195

Browse files
lvan100lianghuan
authored andcommitted
refactor(gs_conf): add support for ". yml" and ". yml" extensions
1 parent a40f791 commit 1a51195

15 files changed

+725
-1381
lines changed

gs/internal/gs_conf/conf.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,11 @@ func (p *PropertySources) getDefaultDir(resolver conf.Properties) (configDir str
265265
}
266266

267267
// getFiles returns the list of configuration files based on the configuration directory and active profiles.
268-
func (p *PropertySources) getFiles(dir string, resolver conf.Properties) (_ []string, err error) {
268+
func (p *PropertySources) getFiles(dir string, resolver conf.Properties) (files []string, err error) {
269+
extensions := []string{".properties", ".yaml", ".yml", ".toml", ".tml", ".json"}
269270

270-
files := []string{
271-
fmt.Sprintf("%s/%s.properties", dir, p.configName),
272-
fmt.Sprintf("%s/%s.yaml", dir, p.configName),
273-
fmt.Sprintf("%s/%s.toml", dir, p.configName),
274-
fmt.Sprintf("%s/%s.json", dir, p.configName),
271+
for _, ext := range extensions {
272+
files = append(files, fmt.Sprintf("%s/%s%s", dir, p.configName, ext))
275273
}
276274

277275
activeProfiles, err := resolver.Resolve("${spring.profiles.active:=}")
@@ -282,14 +280,9 @@ func (p *PropertySources) getFiles(dir string, resolver conf.Properties) (_ []st
282280
if activeProfiles = strings.TrimSpace(activeProfiles); activeProfiles != "" {
283281
for s := range strings.SplitSeq(activeProfiles, ",") {
284282
if s = strings.TrimSpace(s); s != "" {
285-
files = append(files, []string{
286-
fmt.Sprintf("%s/%s-%s.properties", dir, p.configName, s),
287-
fmt.Sprintf("%s/%s-%s.yaml", dir, p.configName, s),
288-
fmt.Sprintf("%s/%s-%s.yml", dir, p.configName, s),
289-
fmt.Sprintf("%s/%s-%s.toml", dir, p.configName, s),
290-
fmt.Sprintf("%s/%s-%s.tml", dir, p.configName, s),
291-
fmt.Sprintf("%s/%s-%s.json", dir, p.configName, s),
292-
}...)
283+
for _, ext := range extensions {
284+
files = append(files, fmt.Sprintf("%s/%s-%s%s", dir, p.configName, s, ext))
285+
}
293286
}
294287
}
295288
}

gs/internal/gs_conf/conf_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ func TestPropertySources(t *testing.T) {
212212
assert.That(t, files).Equal([]string{
213213
"./conf/app.properties",
214214
"./conf/app.yaml",
215+
"./conf/app.yml",
215216
"./conf/app.toml",
217+
"./conf/app.tml",
216218
"./conf/app.json",
217219
})
218220
})
@@ -228,15 +230,21 @@ func TestPropertySources(t *testing.T) {
228230
assert.That(t, files).Equal([]string{
229231
"./conf/app.properties",
230232
"./conf/app.yaml",
233+
"./conf/app.yml",
231234
"./conf/app.toml",
235+
"./conf/app.tml",
232236
"./conf/app.json",
233237
"./conf/app-dev.properties",
234238
"./conf/app-dev.yaml",
239+
"./conf/app-dev.yml",
235240
"./conf/app-dev.toml",
241+
"./conf/app-dev.tml",
236242
"./conf/app-dev.json",
237243
"./conf/app-test.properties",
238244
"./conf/app-test.yaml",
245+
"./conf/app-test.yml",
239246
"./conf/app-test.toml",
247+
"./conf/app-test.tml",
240248
"./conf/app-test.json",
241249
})
242250
})

log/field_encoder.go

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ import (
2525

2626
// Encoder is an interface that defines methods for appending structured data elements.
2727
type Encoder interface {
28-
AppendEncoderBegin() error
29-
AppendEncoderEnd() error
30-
AppendObjectBegin() error
31-
AppendObjectEnd() error
32-
AppendArrayBegin() error
33-
AppendArrayEnd() error
34-
AppendKey(key string) error
35-
AppendBool(v bool) error
36-
AppendInt64(v int64) error
37-
AppendUint64(v uint64) error
38-
AppendFloat64(v float64) error
39-
AppendString(v string) error
40-
AppendReflect(v interface{}) error
28+
AppendEncoderBegin()
29+
AppendEncoderEnd()
30+
AppendObjectBegin()
31+
AppendObjectEnd()
32+
AppendArrayBegin()
33+
AppendArrayEnd()
34+
AppendKey(key string)
35+
AppendBool(v bool)
36+
AppendInt64(v int64)
37+
AppendUint64(v uint64)
38+
AppendFloat64(v float64)
39+
AppendString(v string)
40+
AppendReflect(v interface{})
4141
}
4242

4343
var (
@@ -78,45 +78,39 @@ func (enc *JSONEncoder) Reset() {
7878
}
7979

8080
// AppendEncoderBegin writes the start of an encoder section, represented as a JSON object.
81-
func (enc *JSONEncoder) AppendEncoderBegin() error {
81+
func (enc *JSONEncoder) AppendEncoderBegin() {
8282
enc.last = jsonTokenObjectBegin
8383
enc.buf.WriteByte('{')
84-
return nil
8584
}
8685

8786
// AppendEncoderEnd writes the end of an encoder section (closes a JSON object).
88-
func (enc *JSONEncoder) AppendEncoderEnd() error {
87+
func (enc *JSONEncoder) AppendEncoderEnd() {
8988
enc.last = jsonTokenObjectEnd
9089
enc.buf.WriteByte('}')
91-
return nil
9290
}
9391

9492
// AppendObjectBegin starts a new JSON object.
95-
func (enc *JSONEncoder) AppendObjectBegin() error {
93+
func (enc *JSONEncoder) AppendObjectBegin() {
9694
enc.last = jsonTokenObjectBegin
9795
enc.buf.WriteByte('{')
98-
return nil
9996
}
10097

10198
// AppendObjectEnd ends a JSON object.
102-
func (enc *JSONEncoder) AppendObjectEnd() error {
99+
func (enc *JSONEncoder) AppendObjectEnd() {
103100
enc.last = jsonTokenObjectEnd
104101
enc.buf.WriteByte('}')
105-
return nil
106102
}
107103

108104
// AppendArrayBegin starts a new JSON array.
109-
func (enc *JSONEncoder) AppendArrayBegin() error {
105+
func (enc *JSONEncoder) AppendArrayBegin() {
110106
enc.last = jsonTokenArrayBegin
111107
enc.buf.WriteByte('[')
112-
return nil
113108
}
114109

115110
// AppendArrayEnd ends a JSON array.
116-
func (enc *JSONEncoder) AppendArrayEnd() error {
111+
func (enc *JSONEncoder) AppendArrayEnd() {
117112
enc.last = jsonTokenArrayEnd
118113
enc.buf.WriteByte(']')
119-
return nil
120114
}
121115

122116
// appendSeparator inserts a comma if necessary before a key or value.
@@ -136,68 +130,61 @@ func (enc *JSONEncoder) appendSeparator(curr jsonToken) {
136130
}
137131

138132
// AppendKey writes a JSON key (as a string followed by a colon).
139-
func (enc *JSONEncoder) AppendKey(key string) error {
133+
func (enc *JSONEncoder) AppendKey(key string) {
140134
enc.appendSeparator(jsonTokenKey)
141135
enc.last = jsonTokenKey
142136
enc.buf.WriteByte('"')
143137
enc.safeAddString(key)
144138
enc.buf.WriteByte('"')
145139
enc.buf.WriteByte(':')
146-
return nil
147140
}
148141

149142
// AppendBool writes a boolean value.
150-
func (enc *JSONEncoder) AppendBool(v bool) error {
143+
func (enc *JSONEncoder) AppendBool(v bool) {
151144
enc.appendSeparator(jsonTokenValue)
152145
enc.last = jsonTokenValue
153146
enc.buf.WriteString(strconv.FormatBool(v))
154-
return nil
155147
}
156148

157149
// AppendInt64 writes a signed 64-bit integer.
158-
func (enc *JSONEncoder) AppendInt64(v int64) error {
150+
func (enc *JSONEncoder) AppendInt64(v int64) {
159151
enc.appendSeparator(jsonTokenValue)
160152
enc.last = jsonTokenValue
161153
enc.buf.WriteString(strconv.FormatInt(v, 10))
162-
return nil
163154
}
164155

165156
// AppendUint64 writes an unsigned 64-bit integer.
166-
func (enc *JSONEncoder) AppendUint64(u uint64) error {
157+
func (enc *JSONEncoder) AppendUint64(u uint64) {
167158
enc.appendSeparator(jsonTokenValue)
168159
enc.last = jsonTokenValue
169160
enc.buf.WriteString(strconv.FormatUint(u, 10))
170-
return nil
171161
}
172162

173163
// AppendFloat64 writes a floating-point number.
174-
func (enc *JSONEncoder) AppendFloat64(v float64) error {
164+
func (enc *JSONEncoder) AppendFloat64(v float64) {
175165
enc.appendSeparator(jsonTokenValue)
176166
enc.last = jsonTokenValue
177167
enc.buf.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
178-
return nil
179168
}
180169

181170
// AppendString writes a string value (properly escaped).
182-
func (enc *JSONEncoder) AppendString(v string) error {
171+
func (enc *JSONEncoder) AppendString(v string) {
183172
enc.appendSeparator(jsonTokenValue)
184173
enc.last = jsonTokenValue
185174
enc.buf.WriteByte('"')
186175
enc.safeAddString(v)
187176
enc.buf.WriteByte('"')
188-
return nil
189177
}
190178

191179
// AppendReflect marshals any Go value into JSON and appends it.
192-
func (enc *JSONEncoder) AppendReflect(v interface{}) error {
180+
func (enc *JSONEncoder) AppendReflect(v interface{}) {
193181
enc.appendSeparator(jsonTokenValue)
194182
enc.last = jsonTokenValue
195183
b, err := json.Marshal(v)
196184
if err != nil {
197-
return err
185+
b = []byte(err.Error())
198186
}
199187
enc.buf.Write(b)
200-
return nil
201188
}
202189

203190
// safeAddString escapes and writes a string according to JSON rules.
@@ -283,117 +270,111 @@ func NewTextEncoder(buf *bytes.Buffer, separator string) *TextEncoder {
283270
}
284271

285272
// AppendEncoderBegin is a no-op for TextEncoder (no special start token).
286-
func (enc *TextEncoder) AppendEncoderBegin() error {
287-
return nil
288-
}
273+
func (enc *TextEncoder) AppendEncoderBegin() {}
289274

290275
// AppendEncoderEnd is a no-op for TextEncoder (no special end token).
291-
func (enc *TextEncoder) AppendEncoderEnd() error {
292-
return nil
293-
}
276+
func (enc *TextEncoder) AppendEncoderEnd() {}
294277

295278
// AppendObjectBegin delegates to JSONEncoder and increases JSON depth.
296-
func (enc *TextEncoder) AppendObjectBegin() error {
279+
func (enc *TextEncoder) AppendObjectBegin() {
297280
enc.jsonDepth++
298-
return enc.jsonEncoder.AppendObjectBegin()
281+
enc.jsonEncoder.AppendObjectBegin()
299282
}
300283

301284
// AppendObjectEnd delegates to JSONEncoder, decreases depth, and resets JSON encoder when top-level ends.
302-
func (enc *TextEncoder) AppendObjectEnd() error {
285+
func (enc *TextEncoder) AppendObjectEnd() {
303286
enc.jsonDepth--
304-
err := enc.jsonEncoder.AppendObjectEnd()
287+
enc.jsonEncoder.AppendObjectEnd()
305288
if enc.jsonDepth == 0 {
306289
enc.jsonEncoder.Reset()
307290
}
308-
return err
309291
}
310292

311293
// AppendArrayBegin delegates to JSONEncoder and increases JSON depth.
312-
func (enc *TextEncoder) AppendArrayBegin() error {
294+
func (enc *TextEncoder) AppendArrayBegin() {
313295
enc.jsonDepth++
314-
return enc.jsonEncoder.AppendArrayBegin()
296+
enc.jsonEncoder.AppendArrayBegin()
315297
}
316298

317299
// AppendArrayEnd delegates to JSONEncoder, decreases depth, and resets when array ends at top-level.
318-
func (enc *TextEncoder) AppendArrayEnd() error {
300+
func (enc *TextEncoder) AppendArrayEnd() {
319301
enc.jsonDepth--
320-
err := enc.jsonEncoder.AppendArrayEnd()
302+
enc.jsonEncoder.AppendArrayEnd()
321303
if enc.jsonDepth == 0 {
322304
enc.jsonEncoder.Reset()
323305
}
324-
return err
325306
}
326307

327308
// AppendKey writes a key for a key-value pair.
328309
// If inside a JSON object, it delegates to JSONEncoder.
329310
// Otherwise, it writes key= format and handles separator.
330-
func (enc *TextEncoder) AppendKey(key string) error {
311+
func (enc *TextEncoder) AppendKey(key string) {
331312
if enc.jsonDepth > 0 {
332-
return enc.jsonEncoder.AppendKey(key)
313+
enc.jsonEncoder.AppendKey(key)
314+
return
333315
}
334316
if enc.init {
335317
enc.buf.WriteString(enc.separator)
336318
}
337319
enc.init = true
338320
enc.buf.WriteString(key)
339321
enc.buf.WriteByte('=')
340-
return nil
341322
}
342323

343324
// AppendBool writes a boolean value, delegating to JSONEncoder if inside nested structure.
344-
func (enc *TextEncoder) AppendBool(v bool) error {
325+
func (enc *TextEncoder) AppendBool(v bool) {
345326
if enc.jsonDepth > 0 {
346-
return enc.jsonEncoder.AppendBool(v)
327+
enc.jsonEncoder.AppendBool(v)
328+
return
347329
}
348330
enc.buf.WriteString(strconv.FormatBool(v))
349-
return nil
350331
}
351332

352333
// AppendInt64 writes an int64 value, or delegates to JSONEncoder if in nested structure.
353-
func (enc *TextEncoder) AppendInt64(v int64) error {
334+
func (enc *TextEncoder) AppendInt64(v int64) {
354335
if enc.jsonDepth > 0 {
355-
return enc.jsonEncoder.AppendInt64(v)
336+
enc.jsonEncoder.AppendInt64(v)
337+
return
356338
}
357339
enc.buf.WriteString(strconv.FormatInt(v, 10))
358-
return nil
359340
}
360341

361342
// AppendUint64 writes a uint64 value, or delegates to JSONEncoder if in nested structure.
362-
func (enc *TextEncoder) AppendUint64(v uint64) error {
343+
func (enc *TextEncoder) AppendUint64(v uint64) {
363344
if enc.jsonDepth > 0 {
364-
return enc.jsonEncoder.AppendUint64(v)
345+
enc.jsonEncoder.AppendUint64(v)
346+
return
365347
}
366348
enc.buf.WriteString(strconv.FormatUint(v, 10))
367-
return nil
368349
}
369350

370351
// AppendFloat64 writes a float64 value, or delegates to JSONEncoder if in nested structure.
371-
func (enc *TextEncoder) AppendFloat64(v float64) error {
352+
func (enc *TextEncoder) AppendFloat64(v float64) {
372353
if enc.jsonDepth > 0 {
373-
return enc.jsonEncoder.AppendFloat64(v)
354+
enc.jsonEncoder.AppendFloat64(v)
355+
return
374356
}
375357
enc.buf.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
376-
return nil
377358
}
378359

379360
// AppendString writes a raw string, or delegates to JSONEncoder if in nested structure.
380-
func (enc *TextEncoder) AppendString(v string) error {
361+
func (enc *TextEncoder) AppendString(v string) {
381362
if enc.jsonDepth > 0 {
382-
return enc.jsonEncoder.AppendString(v)
363+
enc.jsonEncoder.AppendString(v)
364+
return
383365
}
384366
enc.buf.WriteString(v)
385-
return nil
386367
}
387368

388369
// AppendReflect marshals and writes a value using JSON if not nested; otherwise, uses JSONEncoder.
389-
func (enc *TextEncoder) AppendReflect(v interface{}) error {
370+
func (enc *TextEncoder) AppendReflect(v interface{}) {
390371
if enc.jsonDepth > 0 {
391-
return enc.jsonEncoder.AppendReflect(v)
372+
enc.jsonEncoder.AppendReflect(v)
373+
return
392374
}
393375
b, err := json.Marshal(v)
394376
if err != nil {
395-
return err
377+
b = []byte(err.Error())
396378
}
397379
enc.buf.Write(b)
398-
return nil
399380
}

0 commit comments

Comments
 (0)