Skip to content

Commit d321ab6

Browse files
committed
Optimize
1 parent c48e202 commit d321ab6

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

encode.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func marshalEnv(prefix string, obj interface{}, idle bool) ([]string, error) {
6565

6666
// Get parameters from tags.
6767
// The name of the key.
68-
key := strings.Trim(field.Tag.Get(tagNameKey), " ")
68+
key := strings.TrimSpace(field.Tag.Get(tagNameKey))
6969
if key == "" {
7070
key = field.Name
7171
}
@@ -147,9 +147,8 @@ func marshalEnv(prefix string, obj interface{}, idle bool) ([]string, error) {
147147
// The getSequence get sequence as string.
148148
func getSequence(item *reflect.Value, sep string) (string, error) {
149149
var (
150-
result string
151-
kind reflect.Kind
152-
max int
150+
kind reflect.Kind
151+
max int
153152
)
154153

155154
// Type checking and instance adjustment.
@@ -165,12 +164,11 @@ func getSequence(item *reflect.Value, sep string) (string, error) {
165164
return "", fmt.Errorf("incorrect type: %s", item.Type())
166165
}
167166

168-
// Item list string display.
169-
result = strings.Replace(fmt.Sprint(*item), " ", sep, -1)
167+
// Use strings.Builder for efficient string concatenation.
168+
var sb strings.Builder
170169

171170
// For pointers and structures.
172171
if kind == reflect.Ptr || kind == reflect.Struct {
173-
tmp := []string{}
174172
for i := 0; i < max; i++ {
175173
elem := item.Index(i)
176174
if kind == reflect.Ptr {
@@ -182,15 +180,29 @@ func getSequence(item *reflect.Value, sep string) (string, error) {
182180
return "", err
183181
}
184182

185-
tmp = append(tmp, v)
183+
if i > 0 {
184+
sb.WriteString(sep)
185+
}
186+
sb.WriteString(v)
187+
}
188+
} else {
189+
for i := 0; i < max; i++ {
190+
v, err := toStr(item.Index(i))
191+
if err != nil {
192+
return "", err
193+
}
194+
195+
if i > 0 {
196+
sb.WriteString(sep)
197+
}
198+
sb.WriteString(v)
186199
}
187-
result = strings.Replace(fmt.Sprint(tmp), " ", sep, -1)
188200
}
189201

190-
return strings.Trim(result, "[]"+sep), nil
202+
return sb.String(), nil
191203
}
192204

193-
// The toStr converts item to string.
205+
// The toStr converts any item to string.
194206
func toStr(item reflect.Value) (string, error) {
195207
switch item.Kind() {
196208
case reflect.Int, reflect.Int8, reflect.Int16,

env.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package env
22

33
import (
44
"bytes"
5-
"math"
65
"os"
76
"regexp"
87
"runtime"
@@ -54,18 +53,29 @@ var (
5453
// Initializer.
5554
func init() {
5655
// Set the number of parallel parsing tasks.
57-
Together(runtime.NumCPU())
56+
ParallelTasks(runtime.NumCPU())
5857
}
5958

6059
// Together sets the number of parallel transliteration tasks.
61-
func Together(pt int) int {
60+
func ParallelTasks(pt int) int {
6261
// The maximum number of parallel tasks
6362
// is twice the number of CPU cores.
6463
max := runtime.NumCPU() * 2
6564

65+
// clamp returns the value limited to the given range [min, max].
66+
clamp := func(value, min, max int) int {
67+
if value < min {
68+
return min
69+
}
70+
if value > max {
71+
return max
72+
}
73+
return value
74+
}
75+
6676
// The minimum number of parallel tasks is 2.
6777
// And the maximum number is twice the number of CPU cores.
68-
parallelTasks = int(math.Min(float64(max), math.Max(float64(pt), 2)))
78+
parallelTasks = clamp(pt, 2, max)
6979
return parallelTasks
7080
}
7181

@@ -76,7 +86,7 @@ func Together(pt int) int {
7686
// Returns an error if the env-file contains incorrect data,
7787
// file is damaged or missing.
7888
//
79-
// # Examples
89+
// Examples:
8090
//
8191
// In this example, some variables are already set in the environment:
8292
//
@@ -318,26 +328,26 @@ func Save(filename, prefix string, obj interface{}) error {
318328
//
319329
// Check if a variable exists in the environment:
320330
//
321-
// fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
322-
// fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
323-
// fmt.Printf("KEY_0 and KEY_1 is %v\n\n", env.Exists("KEY_0", "KEY_1"))
324-
//
325-
// if err := env.Update("./cmd/.env"); err != nil {
326-
// log.Fatal(err)
327-
// }
328-
//
329-
// fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
330-
// fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
331-
// fmt.Printf("KEY_0 and KEY_1 is %v\n", env.Exists("KEY_0", "KEY_1"))
332-
//
333-
// // Output:
334-
// // KEY_0 is true
335-
// // KEY_1 is false
336-
// // KEY_0 and KEY_1 is false
337-
// //
338-
// // KEY_0 is true
339-
// // KEY_1 is true
340-
// // KEY_0 and KEY_1 is true
331+
// fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
332+
// fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
333+
// fmt.Printf("KEY_0 and KEY_1 is %v\n\n", env.Exists("KEY_0", "KEY_1"))
334+
//
335+
// if err := env.Update("./cmd/.env"); err != nil {
336+
// log.Fatal(err)
337+
// }
338+
//
339+
// fmt.Printf("KEY_0 is %v\n", env.Exists("KEY_0"))
340+
// fmt.Printf("KEY_1 is %v\n", env.Exists("KEY_1"))
341+
// fmt.Printf("KEY_0 and KEY_1 is %v\n", env.Exists("KEY_0", "KEY_1"))
342+
//
343+
// // Output:
344+
// // KEY_0 is true
345+
// // KEY_1 is false
346+
// // KEY_0 and KEY_1 is false
347+
// //
348+
// // KEY_0 is true
349+
// // KEY_1 is true
350+
// // KEY_0 and KEY_1 is true
341351
func Exists(keys ...string) bool {
342352
for _, key := range keys {
343353
if _, ok := os.LookupEnv(key); !ok {
@@ -443,10 +453,10 @@ func Unmarshal(prefix string, obj interface{}) error {
443453
// Use the following tags in the fields of structure to
444454
// set the marshing parameters:
445455
//
446-
// env matches the name of the key in the environment;
447-
// def default value (if empty, sets the default value
448-
// for the field type of structure);
449-
// sep sets the separator for lists/arrays (default ` ` - space).
456+
// - env matches the name of the key in the environment;
457+
// - def default value (if empty, sets the default value
458+
// for the field type of structure);
459+
// - sep sets the separator for lists/arrays (default ` ` - space).
450460
//
451461
// Structure example:
452462
//

0 commit comments

Comments
 (0)