-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
63 lines (58 loc) · 1.59 KB
/
utils.go
File metadata and controls
63 lines (58 loc) · 1.59 KB
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
// quotes processes a raw string value from an environment variable line.
package dotenv
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// It performs the following cleanup steps:
// 1. If the value starts with a single (') or double (") quote, it extracts
// everything until the matching closing quote.
// 2. If no matching quote is found, it strips the leading quote.
// 3. It removes any trailing comments starting with "#" (only for unquoted
// content or after the closing quote).
// 4. It trims leading and trailing whitespace from the final result.
func quotes(value string) string {
if len(value) == 0 {
return ""
}
quote := value[0]
if quote == '"' || quote == '\'' {
content, _, found := strings.Cut(value[1:], string(quote))
if found {
return content
}
value = value[1:]
}
value, _, _ = strings.Cut(value, "#")
return strings.TrimSpace(value)
}
// setField helps convert string values to basic Go types supported by the struct fields.
func setField(field reflect.Value, value string) error {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
field.SetBool(b)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
field.SetInt(i)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
field.SetFloat(f)
default:
return fmt.Errorf("unsupported type: %s", field.Kind())
}
return nil
}